测量由Javascript启动的渲染变更时间

时间:2013-12-18 10:20:01

标签: javascript browser svg performance-testing

我想测量显示javascript完成DOM更改所需的时间。

考虑这个示例svg文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   xmlns="http://www.w3.org/2000/svg"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   width="1600"
   height="1000"
   version="1.1">
  <script xlink:href="clicktest.js" />
  <defs>
    <filter id="filterBlur" x="-0.6" width="2.2" y="-0.6" height="2.2">
      <feGaussianBlur stdDeviation="120" />
    </filter>
  </defs>
  <rect y="50" x="50" height="100" width="200" style="fill:#ff0000" onmousedown="red()" />
  <rect y="50" x="300" height="100" width="200" style="fill:#0000ff" onmousedown="blue()" />
  <circle cx="800" cy="600" r="300"  id="circle"
          style="fill:#888888;filter:url(#filterBlur)" />
  <text id="time" x="1250" y="50" style="font-size:40px">time...</text>
  <rect x="900" y="300" width="600" height="600"
        style="fill:#650088;fill-opacity:0.5;filter:url(#filterBlur)" />
  <rect x="100" y="400" width="300" height="400"
        style="fill:#999900;fill-opacity:0.5;filter:url(#filterBlur)" />
</svg>

这会显示两个rect,用作更改圆圈颜色的“按钮”。额外的rect s以及模糊和不透明度会使其变得更慢。

剧本:

function blue()
{
  var startTime = performance.now();
  document.getElementById('circle').style.fill = '#0000ff';
  var endTime = performance.now();
  document.getElementById('time').textContent = (endTime - startTime).toString();
}

function red()
{
  var startTime = performance.now();
  document.getElementById('circle').style.fill = '#ff0000';
  var endTime = performance.now();
  document.getElementById('time').textContent = (endTime - startTime).toString();
}

现在点击时,会显示小于1毫秒的时间,然而,显然需要将近一秒钟(在我的计算机上),直到实际显示更改的颜色(其中,顺便说一下,Chrome似乎比Firefox更快) )。

如何衡量从点击开始到渲染结束时的结束时间?

1 个答案:

答案 0 :(得分:1)

1.打开Chrome(空白)
2.打开DevTools(F12)
3.移至“时间轴”标签
4.点击记录。
5.在Chrome中粘贴你的URL并点击输入。
6.等待页面完全加载。
7.停止录制。
我想你会在那里找到答案。

以下是chrome时间轴上的源结果示例:

enter image description here

编辑:有关JavaScript执行和结果呈现时间的更多详细信息,您可以使用'DynaTrace'或'SpeedTracer'(使用这些工具之一看起来如何HERE

相关问题