鱼C论坛

 找回密码
 立即注册
查看: 1250|回复: 12

[已解决]为什么,chrome://tracing/这个网址使用不了呀?

[复制链接]
发表于 2023-3-19 23:46:37 | 显示全部楼层 |阅读模式
60鱼币
本帖最后由 御坂19090 于 2023-3-20 13:07 编辑

拖进去。什么反应都没有 WDOROA(AQPPL6F212D9%X]N.png 开始打开是这样,"C:\\Users\\misaka\\Desktop\\QQ录屏20230319233349.mp4"。运行这个得到的
  1. #include <iostream>
  2. #include <string>
  3. #include <chrono>
  4. #include <algorithm>
  5. #include <fstream>

  6. #include <cmath>

  7. //配置文件结果
  8. struct ProfileResult
  9. {
  10.         std::string Name;
  11.         long long Start, End;
  12. };

  13. //检测会话
  14. struct InstrumentationSession
  15. {
  16.         std::string Name;
  17. };

  18. //格式化一个json文件,并将其写入一个文件
  19. class Instrumentor
  20. {
  21. private:
  22.         //当前会话
  23.         InstrumentationSession* m_CurrentSession;
  24.         //输出流
  25.         std::ofstream m_OutputStream;
  26.         //配置文件计数
  27.         int m_ProfileCount;
  28. public:
  29.         Instrumentor()
  30.                 : m_CurrentSession(nullptr), m_ProfileCount(0)
  31.         {}

  32.         //创建一个有给定文件名的新文件;默认results.json
  33.         void BeginSession(const std::string& name, const std::string filepath = "results.json")
  34.         {
  35.                 //打开一个文件
  36.                 m_OutputStream.open(filepath);
  37.                 WriteHeader();
  38.                 m_CurrentSession = new InstrumentationSession{ name };
  39.         }

  40.         //写了一个简单的页脚,关闭文件等等事情
  41.         void EndSession()
  42.         {
  43.                 WriteFooter();
  44.                 m_OutputStream.close();
  45.                 delete m_CurrentSession;
  46.                 m_CurrentSession = nullptr;
  47.                 m_ProfileCount = 0;
  48.         }

  49.         //写时间分析数据
  50.         void WriteProfile(const ProfileResult& result)
  51.         {
  52.                 if (m_ProfileCount++ > 0)
  53.                         m_OutputStream << ",";

  54.                 std::string name = result.Name;
  55.                 std::replace(name.begin(), name.end(), '"', ',');

  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":0,";
  63.                 m_OutputStream << ""ts":" << result.Start;
  64.                 m_OutputStream << "}";

  65.                 //这个末尾,每输出完一部分json进入输出流,就进行刷新,把这些数据流到文件中
  66.                 //要这样做的原因是,在程序崩溃或终止,不会丢失所有提供的数据
  67.                 m_OutputStream.flush();
  68.         }

  69.         //写一个文件头,是json文件的开头,是Chrome tracing需要的一个特定的格式
  70.         void WriteHeader()
  71.         {
  72.                 m_OutputStream << "{"otherData": {},"traxeEvents":[";
  73.                 m_OutputStream.flush();
  74.         }

  75.         //写一个简单的页脚
  76.         void WriteFooter()
  77.         {
  78.                 m_OutputStream << "]}";
  79.                 m_OutputStream.flush();
  80.         }

  81.         //返回一个Instrumentor类。(静态)
  82.         static Instrumentor& Get()
  83.         {
  84.                 static Instrumentor* instance = new Instrumentor();
  85.                 return *instance;
  86.         }
  87. };



  88. //计时器;需要为它构建一个对象,让它开始计时,然后在作用域结束时自动停止
  89. class InstrumentationTimer
  90. {
  91. public:
  92.         InstrumentationTimer(const char* name)
  93.                 : m_Name(name), m_Stopped(false)
  94.         {
  95.                 m_StartTimepoint = std::chrono::high_resolution_clock::now();
  96.         }

  97.         ~InstrumentationTimer()
  98.         {
  99.                 if (!m_Stopped)
  100.                         Stop();
  101.         }

  102.         void Stop()
  103.         {
  104.                 auto endTimepoint = std::chrono::high_resolution_clock::now();

  105.                 long long start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch().count();
  106.                 long long end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();

  107.                 Instrumentor::Get().WriteProfile({m_Name, start, end});
  108.                
  109.                 m_Stopped = true;
  110.         }


  111. private:
  112.         const char* m_Name;
  113.         std::chrono::time_point<std::chrono::steady_clock> m_StartTimepoint;
  114.         bool m_Stopped;
  115. };

  116. #define RPOFLING 1
  117. #if RPOFLING
  118. //##__LINE__;为变量取一个唯一的名字,以防我们有很多这样的东西
  119. #define RPOFILE_SCOPE(name) InstrumentationTimer timer##__LINE__(name)
  120. //调用PROFILESCOPE宏,但对于name,它会接受函数的名学,我们可以用这个编译宏FUNCTION二来做
  121. #define RPOFILE_FUNCTION() RPOFILE_SCOPE(__FUNCTION__)
  122. #else
  123. #define RPOFLING_SCOPE(name)
  124. #endif


  125. void Function1()
  126. {
  127.         RPOFILE_FUNCTION();

  128.         for (int i = 0; i < 1000; i ++)
  129.                 std::cout << "Hello Word #" << i << std::endl;
  130. }

  131. void Function2()
  132. {
  133.         RPOFILE_FUNCTION();

  134.         for (int i = 0; i < 1000; i++)
  135.                 std::cout << "Hello Word #" << sqrt(i) << std::endl;
  136. }

  137. void RunBenchmarks()
  138. {
  139.         RPOFILE_FUNCTION();

  140.         std::cout << "Running Benchmarks\n";
  141.         Function1();
  142.         Function2();
  143. }

  144. void main()
  145. {
  146.         Instrumentor::Get().BeginSession("Profile");
  147.         RunBenchmarks();
  148.         Instrumentor::Get().EndSession();

  149.         std::cin.get();
  150. }
  151. }
复制代码

json文件内容
  1. {"otherData": {},"traxeEvents":[{"cat": "function","dur":98540,"name":"Function1","ph":"X","pid":0,"tid":0,"ts":268614872780},{"cat": "function","dur":171829,"name":"Function2","ph":"X","pid":0,"tid":0,"ts":268614971414},{"cat": "function","dur":270628,"name":"RunBenchmarks","ph":"X","pid":0,"tid":0,"ts":268614872687}]}
复制代码
最佳答案
2023-3-19 23:46:38
御坂19090 发表于 2023-3-20 13:07
https://www.bilibili.com/video/BV1gZ4y1R7SG/?spm_id_from=333.788&vd_source=5be5f69ad04dd6b43272a64 ...

因为你的第 83 行写错了,你把 trace 写成了 traxe

  1. #include <iostream>
  2. #include <string>
  3. #include <chrono>
  4. #include <algorithm>
  5. #include <fstream>

  6. #include <cmath>

  7. //配置文件结果
  8. struct ProfileResult
  9. {
  10.         std::string Name;
  11.         long long Start, End;
  12. };

  13. //检测会话
  14. struct InstrumentationSession
  15. {
  16.         std::string Name;
  17. };

  18. //格式化一个json文件,并将其写入一个文件
  19. class Instrumentor
  20. {
  21. private:
  22.         //当前会话
  23.         InstrumentationSession* m_CurrentSession;
  24.         //输出流
  25.         std::ofstream m_OutputStream;
  26.         //配置文件计数
  27.         int m_ProfileCount;
  28. public:
  29.         Instrumentor()
  30.                 : m_CurrentSession(nullptr), m_ProfileCount(0)
  31.         {}

  32.         //创建一个有给定文件名的新文件;默认results.json
  33.         void BeginSession(const std::string& name, const std::string filepath = "results.json")
  34.         {
  35.                 //打开一个文件
  36.                 m_OutputStream.open(filepath);
  37.                 WriteHeader();
  38.                 m_CurrentSession = new InstrumentationSession{ name };
  39.         }

  40.         //写了一个简单的页脚,关闭文件等等事情
  41.         void EndSession()
  42.         {
  43.                 WriteFooter();
  44.                 m_OutputStream.close();
  45.                 delete m_CurrentSession;
  46.                 m_CurrentSession = nullptr;
  47.                 m_ProfileCount = 0;
  48.         }

  49.         //写时间分析数据
  50.         void WriteProfile(const ProfileResult& result)
  51.         {
  52.                 if (m_ProfileCount++ > 0)
  53.                         m_OutputStream << ",";

  54.                 std::string name = result.Name;
  55.                 std::replace(name.begin(), name.end(), '"', ',');

  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":0,";
  63.                 m_OutputStream << ""ts":" << result.Start;
  64.                 m_OutputStream << "}";

  65.                 //这个末尾,每输出完一部分json进入输出流,就进行刷新,把这些数据流到文件中
  66.                 //要这样做的原因是,在程序崩溃或终止,不会丢失所有提供的数据
  67.                 m_OutputStream.flush();
  68.         }

  69.         //写一个文件头,是json文件的开头,是Chrome tracing需要的一个特定的格式
  70.         void WriteHeader()
  71.         {
  72.                 m_OutputStream << "{"otherData": {},"traceEvents":[";                               // 这一行写错了,trace 写成了 traxe
  73.                 m_OutputStream.flush();
  74.         }

  75.         //写一个简单的页脚
  76.         void WriteFooter()
  77.         {
  78.                 m_OutputStream << "]}";
  79.                 m_OutputStream.flush();
  80.         }

  81.         //返回一个Instrumentor类。(静态)
  82.         static Instrumentor& Get()
  83.         {
  84.                 static Instrumentor* instance = new Instrumentor();
  85.                 return *instance;
  86.         }
  87. };



  88. //计时器;需要为它构建一个对象,让它开始计时,然后在作用域结束时自动停止
  89. class InstrumentationTimer
  90. {
  91. public:
  92.         InstrumentationTimer(const char* name)
  93.                 : m_Name(name), m_Stopped(false)
  94.         {
  95.                 m_StartTimepoint = std::chrono::high_resolution_clock::now();
  96.         }

  97.         ~InstrumentationTimer()
  98.         {
  99.                 if (!m_Stopped)
  100.                         Stop();
  101.         }

  102.         void Stop()
  103.         {
  104.                 auto endTimepoint = std::chrono::high_resolution_clock::now();

  105.                 long long start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch().count();
  106.                 long long end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();

  107.                 Instrumentor::Get().WriteProfile({m_Name, start, end});
  108.                
  109.                 m_Stopped = true;
  110.         }


  111. private:
  112.         const char* m_Name;
  113.         std::chrono::time_point<std::chrono::steady_clock> m_StartTimepoint;
  114.         bool m_Stopped;
  115. };

  116. #define RPOFLING 1
  117. #if RPOFLING
  118. //##__LINE__;为变量取一个唯一的名字,以防我们有很多这样的东西
  119. #define RPOFILE_SCOPE(name) InstrumentationTimer timer##__LINE__(name)
  120. //调用PROFILESCOPE宏,但对于name,它会接受函数的名学,我们可以用这个编译宏FUNCTION二来做
  121. #define RPOFILE_FUNCTION() RPOFILE_SCOPE(__FUNCTION__)
  122. #else
  123. #define RPOFLING_SCOPE(name)
  124. #endif


  125. void Function1()
  126. {
  127.         RPOFILE_FUNCTION();

  128.         for (int i = 0; i < 1000; i ++)
  129.                 std::cout << "Hello Word #" << i << std::endl;
  130. }

  131. void Function2()
  132. {
  133.         RPOFILE_FUNCTION();

  134.         for (int i = 0; i < 1000; i++)
  135.                 std::cout << "Hello Word #" << sqrt(i) << std::endl;
  136. }

  137. void RunBenchmarks()
  138. {
  139.         RPOFILE_FUNCTION();

  140.         std::cout << "Running Benchmarks\n";
  141.         Function1();
  142.         Function2();
  143. }

  144. void main()
  145. {
  146.         Instrumentor::Get().BeginSession("Profile");
  147.         RunBenchmarks();
  148.         Instrumentor::Get().EndSession();

  149.         std::cin.get();
  150. }
  151. }
复制代码

最佳答案

查看完整内容

因为你的第 83 行写错了,你把 trace 写成了 traxe
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-3-19 23:46:38 | 显示全部楼层    本楼为最佳答案   
御坂19090 发表于 2023-3-20 13:07
https://www.bilibili.com/video/BV1gZ4y1R7SG/?spm_id_from=333.788&vd_source=5be5f69ad04dd6b43272a64 ...

因为你的第 83 行写错了,你把 trace 写成了 traxe

  1. #include <iostream>
  2. #include <string>
  3. #include <chrono>
  4. #include <algorithm>
  5. #include <fstream>

  6. #include <cmath>

  7. //配置文件结果
  8. struct ProfileResult
  9. {
  10.         std::string Name;
  11.         long long Start, End;
  12. };

  13. //检测会话
  14. struct InstrumentationSession
  15. {
  16.         std::string Name;
  17. };

  18. //格式化一个json文件,并将其写入一个文件
  19. class Instrumentor
  20. {
  21. private:
  22.         //当前会话
  23.         InstrumentationSession* m_CurrentSession;
  24.         //输出流
  25.         std::ofstream m_OutputStream;
  26.         //配置文件计数
  27.         int m_ProfileCount;
  28. public:
  29.         Instrumentor()
  30.                 : m_CurrentSession(nullptr), m_ProfileCount(0)
  31.         {}

  32.         //创建一个有给定文件名的新文件;默认results.json
  33.         void BeginSession(const std::string& name, const std::string filepath = "results.json")
  34.         {
  35.                 //打开一个文件
  36.                 m_OutputStream.open(filepath);
  37.                 WriteHeader();
  38.                 m_CurrentSession = new InstrumentationSession{ name };
  39.         }

  40.         //写了一个简单的页脚,关闭文件等等事情
  41.         void EndSession()
  42.         {
  43.                 WriteFooter();
  44.                 m_OutputStream.close();
  45.                 delete m_CurrentSession;
  46.                 m_CurrentSession = nullptr;
  47.                 m_ProfileCount = 0;
  48.         }

  49.         //写时间分析数据
  50.         void WriteProfile(const ProfileResult& result)
  51.         {
  52.                 if (m_ProfileCount++ > 0)
  53.                         m_OutputStream << ",";

  54.                 std::string name = result.Name;
  55.                 std::replace(name.begin(), name.end(), '"', ',');

  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":0,";
  63.                 m_OutputStream << ""ts":" << result.Start;
  64.                 m_OutputStream << "}";

  65.                 //这个末尾,每输出完一部分json进入输出流,就进行刷新,把这些数据流到文件中
  66.                 //要这样做的原因是,在程序崩溃或终止,不会丢失所有提供的数据
  67.                 m_OutputStream.flush();
  68.         }

  69.         //写一个文件头,是json文件的开头,是Chrome tracing需要的一个特定的格式
  70.         void WriteHeader()
  71.         {
  72.                 m_OutputStream << "{"otherData": {},"traceEvents":[";                               // 这一行写错了,trace 写成了 traxe
  73.                 m_OutputStream.flush();
  74.         }

  75.         //写一个简单的页脚
  76.         void WriteFooter()
  77.         {
  78.                 m_OutputStream << "]}";
  79.                 m_OutputStream.flush();
  80.         }

  81.         //返回一个Instrumentor类。(静态)
  82.         static Instrumentor& Get()
  83.         {
  84.                 static Instrumentor* instance = new Instrumentor();
  85.                 return *instance;
  86.         }
  87. };



  88. //计时器;需要为它构建一个对象,让它开始计时,然后在作用域结束时自动停止
  89. class InstrumentationTimer
  90. {
  91. public:
  92.         InstrumentationTimer(const char* name)
  93.                 : m_Name(name), m_Stopped(false)
  94.         {
  95.                 m_StartTimepoint = std::chrono::high_resolution_clock::now();
  96.         }

  97.         ~InstrumentationTimer()
  98.         {
  99.                 if (!m_Stopped)
  100.                         Stop();
  101.         }

  102.         void Stop()
  103.         {
  104.                 auto endTimepoint = std::chrono::high_resolution_clock::now();

  105.                 long long start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch().count();
  106.                 long long end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();

  107.                 Instrumentor::Get().WriteProfile({m_Name, start, end});
  108.                
  109.                 m_Stopped = true;
  110.         }


  111. private:
  112.         const char* m_Name;
  113.         std::chrono::time_point<std::chrono::steady_clock> m_StartTimepoint;
  114.         bool m_Stopped;
  115. };

  116. #define RPOFLING 1
  117. #if RPOFLING
  118. //##__LINE__;为变量取一个唯一的名字,以防我们有很多这样的东西
  119. #define RPOFILE_SCOPE(name) InstrumentationTimer timer##__LINE__(name)
  120. //调用PROFILESCOPE宏,但对于name,它会接受函数的名学,我们可以用这个编译宏FUNCTION二来做
  121. #define RPOFILE_FUNCTION() RPOFILE_SCOPE(__FUNCTION__)
  122. #else
  123. #define RPOFLING_SCOPE(name)
  124. #endif


  125. void Function1()
  126. {
  127.         RPOFILE_FUNCTION();

  128.         for (int i = 0; i < 1000; i ++)
  129.                 std::cout << "Hello Word #" << i << std::endl;
  130. }

  131. void Function2()
  132. {
  133.         RPOFILE_FUNCTION();

  134.         for (int i = 0; i < 1000; i++)
  135.                 std::cout << "Hello Word #" << sqrt(i) << std::endl;
  136. }

  137. void RunBenchmarks()
  138. {
  139.         RPOFILE_FUNCTION();

  140.         std::cout << "Running Benchmarks\n";
  141.         Function1();
  142.         Function2();
  143. }

  144. void main()
  145. {
  146.         Instrumentor::Get().BeginSession("Profile");
  147.         RunBenchmarks();
  148.         Instrumentor::Get().EndSession();

  149.         std::cin.get();
  150. }
  151. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-3-19 23:47:28 | 显示全部楼层
本帖最后由 御坂19090 于 2023-3-19 23:50 编辑

视频好像发不了。拖进去后,加载了一会然后就这样了 )O[%IQPA]TGR24%X2B3Y[UV.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-3-20 10:19:55 | 显示全部楼层
御坂19090 发表于 2023-3-19 23:47
视频好像发不了。拖进去后,加载了一会然后就这样了

你想用 chrome://tracing/ 来干什么?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-3-20 11:56:49 | 显示全部楼层
isdkz 发表于 2023-3-20 10:19
你想用 chrome://tracing/ 来干什么?

尝试可视化呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-3-20 12:04:54 | 显示全部楼层

这个是用来调试 web 的吧,你怎么放了一个C++代码?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-3-20 12:22:18 | 显示全部楼层
isdkz 发表于 2023-3-20 12:04
这个是用来调试 web 的吧,你怎么放了一个C++代码?


这个代码不是生成一个json文件吗。这个文件是符号它的格式的文件。按理应该可以转换成图像的。但是他没有反应。下面json文件内容都发出来了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-3-20 12:26:01 | 显示全部楼层
本帖最后由 isdkz 于 2023-3-20 12:27 编辑
御坂19090 发表于 2023-3-20 12:22
这个代码不是生成一个json文件吗。这个文件是符号它的格式的文件。按理应该可以转换成图像的。但是他没 ...


你说的是这种?Chrome Trace 是用来做性能分析的吧

截图20230320122340.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-3-20 12:45:30 | 显示全部楼层
isdkz 发表于 2023-3-20 12:26
你说的是这种?Chrome Trace 是用来做性能分析的吧

_H{[`}(@3`)C[U0FTXOCM{Y.png 看教程的时候,他直接把文件拖上去,就是这样。但是自己试的时候,拖上去,加载了一下,就没有反应了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-3-20 12:46:41 | 显示全部楼层
御坂19090 发表于 2023-3-20 12:45
看教程的时候,他直接把文件拖上去,就是这样。但是自己试的时候,拖上去,加载了一下,就没有反应了

你把教程链接发上来看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-3-20 13:07:42 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-3-20 13:33:44 | 显示全部楼层
isdkz 发表于 2023-3-19 23:46
因为你的第 83 行写错了,你把 trace 写成了 traxe

我的排查思路是既然你的 json 文件不显示函数信息,那我就对比你的和视频中的那个函数信息的字段,

发现视频中的json文件里面是 traceEvents,你的json文件里面是 traxeEvents,然后我再回到你的代码中搜 traxeEvents,找到后改回来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-3-20 13:44:52 | 显示全部楼层
谢谢,这种。啊,自己找了好久。找不到。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-24 10:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表