御坂19090 发表于 2023-3-19 23:46:37

为什么,chrome://tracing/这个网址使用不了呀?

本帖最后由 御坂19090 于 2023-3-20 13:07 编辑

拖进去。什么反应都没有开始打开是这样,"C:\Users\misaka\Desktop\QQ录屏20230319233349.mp4"。运行这个得到的#include <iostream>
#include <string>
#include <chrono>
#include <algorithm>
#include <fstream>

#include <cmath>

//配置文件结果
struct ProfileResult
{
        std::string Name;
        long long Start, End;
};

//检测会话
struct InstrumentationSession
{
        std::string Name;
};

//格式化一个json文件,并将其写入一个文件
class Instrumentor
{
private:
        //当前会话
        InstrumentationSession* m_CurrentSession;
        //输出流
        std::ofstream m_OutputStream;
        //配置文件计数
        int m_ProfileCount;
public:
        Instrumentor()
                : m_CurrentSession(nullptr), m_ProfileCount(0)
        {}

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

        //写了一个简单的页脚,关闭文件等等事情
        void EndSession()
        {
                WriteFooter();
                m_OutputStream.close();
                delete m_CurrentSession;
                m_CurrentSession = nullptr;
                m_ProfileCount = 0;
        }

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

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

                m_OutputStream << "{";
                m_OutputStream << "\"cat\": \"function\",";
                m_OutputStream << "\"dur\":" << (result.End - result.Start) << ',';
                m_OutputStream << "\"name\":\"" << name << "\",";
                m_OutputStream << "\"ph\":\"X\",";
                m_OutputStream << "\"pid\":0,";
                m_OutputStream << "\"tid\":0,";
                m_OutputStream << "\"ts\":" << result.Start;
                m_OutputStream << "}";

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

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

        //写一个简单的页脚
        void WriteFooter()
        {
                m_OutputStream << "]}";
                m_OutputStream.flush();
        }

        //返回一个Instrumentor类。(静态)
        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::microseconds>(m_StartTimepoint).time_since_epoch().count();
                long long end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();

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


private:
        const char* m_Name;
        std::chrono::time_point<std::chrono::steady_clock> m_StartTimepoint;
        bool m_Stopped;
};

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


void Function1()
{
        RPOFILE_FUNCTION();

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

void Function2()
{
        RPOFILE_FUNCTION();

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

void RunBenchmarks()
{
        RPOFILE_FUNCTION();

        std::cout << "Running Benchmarks\n";
        Function1();
        Function2();
}

void main()
{
        Instrumentor::Get().BeginSession("Profile");
        RunBenchmarks();
        Instrumentor::Get().EndSession();

        std::cin.get();
}
}
json文件内容
{"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}]}

isdkz 发表于 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

#include <iostream>
#include <string>
#include <chrono>
#include <algorithm>
#include <fstream>

#include <cmath>

//配置文件结果
struct ProfileResult
{
      std::string Name;
      long long Start, End;
};

//检测会话
struct InstrumentationSession
{
      std::string Name;
};

//格式化一个json文件,并将其写入一个文件
class Instrumentor
{
private:
      //当前会话
      InstrumentationSession* m_CurrentSession;
      //输出流
      std::ofstream m_OutputStream;
      //配置文件计数
      int m_ProfileCount;
public:
      Instrumentor()
                : m_CurrentSession(nullptr), m_ProfileCount(0)
      {}

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

      //写了一个简单的页脚,关闭文件等等事情
      void EndSession()
      {
                WriteFooter();
                m_OutputStream.close();
                delete m_CurrentSession;
                m_CurrentSession = nullptr;
                m_ProfileCount = 0;
      }

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

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

                m_OutputStream << "{";
                m_OutputStream << ""cat": "function",";
                m_OutputStream << ""dur":" << (result.End - result.Start) << ',';
                m_OutputStream << ""name":"" << name << "",";
                m_OutputStream << ""ph":"X",";
                m_OutputStream << ""pid":0,";
                m_OutputStream << ""tid":0,";
                m_OutputStream << ""ts":" << result.Start;
                m_OutputStream << "}";

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

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

      //写一个简单的页脚
      void WriteFooter()
      {
                m_OutputStream << "]}";
                m_OutputStream.flush();
      }

      //返回一个Instrumentor类。(静态)
      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::microseconds>(m_StartTimepoint).time_since_epoch().count();
                long long end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();

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


private:
      const char* m_Name;
      std::chrono::time_point<std::chrono::steady_clock> m_StartTimepoint;
      bool m_Stopped;
};

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


void Function1()
{
      RPOFILE_FUNCTION();

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

void Function2()
{
      RPOFILE_FUNCTION();

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

void RunBenchmarks()
{
      RPOFILE_FUNCTION();

      std::cout << "Running Benchmarks\n";
      Function1();
      Function2();
}

void main()
{
      Instrumentor::Get().BeginSession("Profile");
      RunBenchmarks();
      Instrumentor::Get().EndSession();

      std::cin.get();
}
}

御坂19090 发表于 2023-3-19 23:47:28

本帖最后由 御坂19090 于 2023-3-19 23:50 编辑

视频好像发不了。拖进去后,加载了一会然后就这样了

isdkz 发表于 2023-3-20 10:19:55

御坂19090 发表于 2023-3-19 23:47
视频好像发不了。拖进去后,加载了一会然后就这样了

你想用 chrome://tracing/ 来干什么?

御坂19090 发表于 2023-3-20 11:56:49

isdkz 发表于 2023-3-20 10:19
你想用 chrome://tracing/ 来干什么?

尝试可视化呀

isdkz 发表于 2023-3-20 12:04:54

御坂19090 发表于 2023-3-20 11:56
尝试可视化呀

这个是用来调试 web 的吧,你怎么放了一个C++代码?

御坂19090 发表于 2023-3-20 12:22:18

isdkz 发表于 2023-3-20 12:04
这个是用来调试 web 的吧,你怎么放了一个C++代码?

这个代码不是生成一个json文件吗。这个文件是符号它的格式的文件。按理应该可以转换成图像的。但是他没有反应。下面json文件内容都发出来了

isdkz 发表于 2023-3-20 12:26:01

本帖最后由 isdkz 于 2023-3-20 12:27 编辑

御坂19090 发表于 2023-3-20 12:22
这个代码不是生成一个json文件吗。这个文件是符号它的格式的文件。按理应该可以转换成图像的。但是他没 ...

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

御坂19090 发表于 2023-3-20 12:45:30

isdkz 发表于 2023-3-20 12:26
你说的是这种?Chrome Trace 是用来做性能分析的吧

看教程的时候,他直接把文件拖上去,就是这样。但是自己试的时候,拖上去,加载了一下,就没有反应了

isdkz 发表于 2023-3-20 12:46:41

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

你把教程链接发上来看看

御坂19090 发表于 2023-3-20 13:07:42

isdkz 发表于 2023-3-20 12:46
你把教程链接发上来看看

https://www.bilibili.com/video/BV1gZ4y1R7SG/?spm_id_from=333.788&vd_source=5be5f69ad04dd6b43272a646aff9eafb

isdkz 发表于 2023-3-20 13:33:44

isdkz 发表于 2023-3-19 23:46
因为你的第 83 行写错了,你把 trace 写成了 traxe

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

发现视频中的json文件里面是 traceEvents,你的json文件里面是 traxeEvents,然后我再回到你的代码中搜 traxeEvents,找到后改回来

御坂19090 发表于 2023-3-20 13:44:52

谢谢,这种。啊,自己找了好久。找不到。。。
页: [1]
查看完整版本: 为什么,chrome://tracing/这个网址使用不了呀?