|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么chrome://tracing的两个函数重叠在一起,有没有大佬帮我看看
代码如下:
- // #include "HeaderFolder\Timer.h"
- #include <iostream>
- #include <string>
- #include <chrono>
- #include <algorithm>
- #include <fstream>
- #include <cmath>
- #include <thread>
- struct ProfileResult
- {
- std::string Name;
- long long Start, End;
- uint32_t ThreadID;
- };
- struct InstrumentationSession
- {
- std::string Name;
- };
- struct Instrumentor
- {
- private:
- InstrumentationSession* m_CurrentSession;
- std::ofstream m_OutpuyStream;
- int m_ProfileCount;
- public:
- Instrumentor()
- : m_CurrentSession(nullptr), m_ProfileCount(0)
- {
- }
- void BeginSession(const std::string& name, const std::string& filepath = "results.json")
- {
- m_OutpuyStream.open(filepath);
- WriteHeader();
- m_CurrentSession = new InstrumentationSession{ name };
- }
- void EndSession()
- {
- WriteFooter();
- m_OutpuyStream.close();
- delete m_CurrentSession;
- m_CurrentSession = nullptr;
- m_ProfileCount = 0;
- }
- void WriteProfile(const ProfileResult& result)
- {
- if (m_ProfileCount++ > 0)
- m_OutpuyStream << ",";
- std::string name = result.Name;
- std::replace(name.begin(), name.end(), '"', '\'');
- m_OutpuyStream << "{";
- m_OutpuyStream << ""cat":"function",";
- m_OutpuyStream << ""dur":" << (result.End - result.Start) << ",";
- m_OutpuyStream << ""name":"" << name << "",";
- m_OutpuyStream << ""ph":"X",";
- m_OutpuyStream << ""pid":0,";
- m_OutpuyStream << ""tid":" << result.ThreadID << ",";
- m_OutpuyStream << ""ts":" << result.Start;
- m_OutpuyStream << "}";
- m_OutpuyStream.flush();
- }
- void WriteHeader()
- {
- m_OutpuyStream << "{"otherData": {},"traceEvents":[";
- m_OutpuyStream.flush();
- }
- void WriteFooter()
- {
- m_OutpuyStream << "]}";
- m_OutpuyStream.flush();
- }
- static Instrumentor& Get()
- {
- static Instrumentor* instance = new Instrumentor();
- return *instance;
- }
- };
- class InstrumentationTimer
- {
- public:
- InstrumentationTimer(const char* name)
- : m_Name(name), m_Stopped(false)
- {
- m_StartTimepoint = std::chrono::high_resolution_clock::now();
- }
- ~InstrumentationTimer()
- {
- if (!m_Stopped)
- Stop();
- }
- void Stop()
- {
- auto endTimepoint = std::chrono::high_resolution_clock::now();
- long long start = std::chrono::time_point_cast<std::chrono::milliseconds>(m_StartTimepoint).time_since_epoch().count();
- long long end = std::chrono::time_point_cast<std::chrono::milliseconds>(endTimepoint).time_since_epoch().count();
- uint32_t threadID = std::hash<std::thread::id>{}(std::this_thread::get_id());
- Instrumentor::Get().WriteProfile({ m_Name, start, end, threadID });
- m_Stopped = true;
- }
- private:
- const char* m_Name;
- std::chrono::high_resolution_clock::time_point m_StartTimepoint; // std::chrono::high_resolution_clock::time_point | std::chrono::time_point<std::chrono::steady_clock>
- bool m_Stopped;
- };
- #define PROFING true
- #if PROFING
- #define PROFILE_SCOPE(name) InstrumentationTimer timer##__LINE__(name)
- #define PROFILE_FUNCTION() PROFILE_SCOPE(__FUNCSIG__)
- #else
- #define PROFILE_SCOPE(name)
- #endif
- namespace Benchmark {
- void PrintFunction(int value)
- {
- PROFILE_FUNCTION();
- for (int i = 0; i < 1000; i++)
- std::cout << "Hello World! #" << (i + value) << std::endl;
- }
- void PrintFunction()
- {
- PROFILE_FUNCTION();
- for (int i = 0; i < 1000; i++)
- std::cout << "Hello World! #" << sqrt(i) << std::endl;
- }
- void RunBenchmarks()
- {
- PROFILE_FUNCTION();
- std::cout << "Running Banchmarks...\n";
- PrintFunction(2);
- PrintFunction();
- }
- }
- int main()
- {
- Instrumentor::Get().BeginSession("Profile");
- Benchmark::RunBenchmarks();
- Instrumentor::Get().EndSession();
- std::cin.get();
- }
复制代码 还有这个生成的json文件:
- {
- "otherData": {},
- "traceEvents": [
- {
- "cat": "function",
- "dur": 86,
- "name": "void __cdecl Benchmark::PrintFunction(int)",
- "ph": "X",
- "pid": 0,
- "tid": 3404007327,
- "ts": 14619163
- },
- {
- "cat": "function",
- "dur": 123,
- "name": "void __cdecl Benchmark::PrintFunction(void)",
- "ph": "X",
- "pid": 0,
- "tid": 3404007327,
- "ts": 14619249
- },
- {
- "cat": "function",
- "dur": 209,
- "name": "void __cdecl Benchmark::RunBenchmarks(void)",
- "ph": "X",
- "pid": 0,
- "tid": 3404007327,
- "ts": 14619163
- }
- ]
- }
复制代码
这个json文件中有两个的“ts”时一样的!!!
怎么解决这个问题?急!
file:///D:/lixxl/Pictures/Screenshots/Screenshot%202024-07-24%20145010.png
|
|