谷歌浏览器开发人员工具 - 分析结果文件格式

Google Chrome developer tools - Profiling results file format

本文关键字:结果 文件 格式 开发 工具 谷歌浏览器      更新时间:2023-09-26

我想知道是否有任何(非)官方规范的.cpuprofile文件格式,这是在基于Chromium的浏览器的开发人员工具中使用JavaScript分析时生成的。

它是纯文本JSON,因此很容易获取调用树数据,但我不明白如何获取每个函数的计时信息。

我也对每个函数的命中计数感兴趣。

根据 @artm 提供的文档,可以使用 kcachegrind 分析输出。 为了将Chrome .cpuprofile文件加载到其中,您需要做的是将其转换为callgrind格式。

你没有提到你的开发环境,所以我不能说你最简单的方法是什么。

文档提到了Google的性能工具和kcachegrind。 这些工具需要手动构建,我没有一个方便的环境来做到这一点。

以下是我在安装了Node的Windows 8.1机器上的做法。

  1. 为 Node 安装 chrome2calltree。 此命令实用程序会将您的 .cpuprofile 转换为调用格林格式。

  2. 安装 QCacheGrind。 这是一个Windows预构建的kcachegrind端口,可让您可视化调用研磨格式的文件。

  3. 转换您的 .cpuprofile: chrome2calltree -i test.cpuprofile -o callgrind.profile

  4. 使用QCacheGrind打开您的callgrind.profile。

这可能不是对你的问题的直接回答,但这真的很棒。

用法非常简单:

fireunit.profile(function(){
  document.getElementsByClassName("foo");
});

你会得到从fireunit.getProfile()返回的以下JavaScript对象:

{
  "time": 8.443,
  "calls": 611,
  "data":[
  {
    "name":"makeArray()",
    "calls":1,
    "percent":23.58,
    "ownTime":1.991,
    "time":1.991,
    "avgTime":1.991,
    "minTime":1.991,
    "maxTime":1.991,
    "fileName":"jquery.js (line 2059)"
  },
  // etc.
]}

也许值得注意,但 cpuprofile 的 JSON 格式最近可能发生了变化,因此它可能不是一种稳定的格式。例如,在 Chromium 44(其构建存档中的旧版本)中,它看起来像这样

jq keys < chromium_44.cpuprofile
[
  "endTime",
  "head",
  "samples",
  "startTime",
  "timestamps"
]

在最新的铬 55 中,它是

jq keys < chrome_55.cpuprofile
[
  "endTime",
  "nodes",
  "samples",
  "startTime",
  "timeDeltas"
]

只是需要警惕的事情。不过,我没有看到太多关于该格式的文档