Advertisement
Ginsutime

Visual Benchmarking Cherno Example

Feb 15th, 2022
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include <string>
  2. #include <chrono>
  3. #include <algorithm>
  4. #include <fstream>
  5. #include <thread>
  6. #include <iostream>
  7.  
  8. struct ProfileResult
  9. {
  10.     std::string Name;
  11.     long long Start, End;
  12.     uint32_t ThreadID;
  13. };
  14.  
  15. struct InstrumentationSession
  16. {
  17.     std::string Name;
  18. };
  19.  
  20. class Instrumentor
  21. {
  22. private:
  23.     InstrumentationSession* m_CurrentSession;
  24.     std::ofstream m_OutputStream;
  25.     int m_ProfileCount;
  26. public:
  27.     Instrumentor()
  28.         : m_CurrentSession(nullptr), m_ProfileCount(0)
  29.     {
  30.     }
  31.  
  32.     void BeginSession(const std::string& name, const std::string& filepath = "results.json")
  33.     {
  34.         m_OutputStream.open(filepath);
  35.         WriteHeader();
  36.         m_CurrentSession = new InstrumentationSession{ name };
  37.     }
  38.  
  39.     void EndSession()
  40.     {
  41.         WriteFooter();
  42.         m_OutputStream.close();
  43.         delete m_CurrentSession;
  44.         m_CurrentSession = nullptr;
  45.         m_ProfileCount = 0;
  46.     }
  47.  
  48.     void WriteProfile(const ProfileResult& result)
  49.     {
  50.         if (m_ProfileCount++ > 0)
  51.             m_OutputStream << ",";
  52.  
  53.         std::string name = result.Name;
  54.         std::replace(name.begin(), name.end(), '"', '\'');
  55.  
  56.         m_OutputStream << "{";
  57.         m_OutputStream << "\"cat\":\"function\",";
  58.         m_OutputStream << "\"dur\":" << (result.End - result.Start) << ',';
  59.         m_OutputStream << "\"name\":\"" << name << "\",";
  60.         m_OutputStream << "\"ph\":\"X\",";
  61.         m_OutputStream << "\"pid\":0,";
  62.         m_OutputStream << "\"tid\":" << result.ThreadID << ",";
  63.         m_OutputStream << "\"ts\":" << result.Start;
  64.         m_OutputStream << "}";
  65.  
  66.         m_OutputStream.flush();
  67.     }
  68.  
  69.     void WriteHeader()
  70.     {
  71.         m_OutputStream << "{\"otherData\": {},\"traceEvents\":[";
  72.         m_OutputStream.flush();
  73.     }
  74.  
  75.     void WriteFooter()
  76.     {
  77.         m_OutputStream << "]}";
  78.         m_OutputStream.flush();
  79.     }
  80.  
  81.     static Instrumentor& Get()
  82.     {
  83.         static Instrumentor instance;
  84.         return instance;
  85.     }
  86. };
  87.  
  88. class InstrumentationTimer
  89. {
  90. public:
  91.     InstrumentationTimer(const char* name)
  92.         : m_Name(name), m_Stopped(false)
  93.     {
  94.         m_StartTimepoint = std::chrono::high_resolution_clock::now();
  95.     }
  96.  
  97.     ~InstrumentationTimer()
  98.     {
  99.         if (!m_Stopped)
  100.             Stop();
  101.     }
  102.  
  103.     void Stop()
  104.     {
  105.         auto endTimepoint = std::chrono::high_resolution_clock::now();
  106.  
  107.         long long start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch().count();
  108.         long long end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();
  109.  
  110.         uint32_t threadID = std::hash<std::thread::id>{}(std::this_thread::get_id());
  111.         Instrumentor::Get().WriteProfile({ m_Name, start, end, threadID });
  112.  
  113.         m_Stopped = true;
  114.     }
  115. private:
  116.     const char* m_Name;
  117.     std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimepoint;
  118.     bool m_Stopped;
  119. };
  120.  
  121. #define PROFILING 1
  122. #if PROFILING
  123. #define PROFILE_SCOPE(name) InstrumentationTimer timer##__LINE__(name)
  124. #define PROFILE_FUNCTION() PROFILE_SCOPE(__FUNCSIG__)
  125. #else
  126. #define PROFILE_SCOPE(name)
  127. #endif
  128.  
  129. namespace Benchmark
  130. {
  131.     void PrintFunction(int value)
  132.     {
  133.         PROFILE_FUNCTION();
  134.  
  135.         for (int i = 0; i < 1000; i++)
  136.             std::cout << "Hello World #" << i << std::endl;
  137.     }
  138.  
  139.     void PrintFunction()
  140.     {
  141.         PROFILE_FUNCTION();
  142.  
  143.         for (int i = 0; i < 1000; i++)
  144.             std::cout << "Hello World #" << sqrt(i) << std::endl;
  145.     }
  146.  
  147.     void RunBenchmarks()
  148.     {
  149.         PROFILE_FUNCTION();
  150.  
  151.         std::cout << "Running Benchmarks....\n";
  152.         std::thread a([]() {PrintFunction(2); });
  153.         PrintFunction();
  154.  
  155.         a.join();
  156.     }
  157. }
  158.  
  159. int main()
  160. {
  161.     Instrumentor::Get().BeginSession("Profile");
  162.     Benchmark::RunBenchmarks();
  163.     Instrumentor::Get().EndSession();
  164.  
  165.     std::cin.get();
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement