mozilla

Revision 97464 of PerfMeasurement.jsm

  • Revision slug: JavaScript_code_modules/PerfMeasurement.jsm
  • Revision title: PerfMeasurement.jsm
  • Revision id: 97464
  • Created:
  • Creator: Zwol
  • Is current revision? No
  • Comment 4 words added

Revision Content

{{ gecko_minversion_header("2") }}

The PerfMeasurement.jsm JavaScript code module lets you take detailed performance measurements of your code.

{{ note("The PerfMeasurement.jsm JavaScript code module can only be used from chrome -- that is, from within the application itself or an add-on.") }}

To use PerfMeasurement, first import the module:

Components.utils.import("resource://gre/modules/PerfMeasurement.jsm")

You can then create a PerfMeasurement object. You give the constructor a bit-mask of events you are interested in. For instance, let's measure instructions executed, cache references, and cache misses:

let monitor = new PerfMeasurement(PerfMeasurement.CPU_CYCLES | PerfMeasurement.CACHE_REFERENCES | PerfMeasurement.CACHE_MISSES);

This just sets up to measure stuff, it doesn't start timing anything.  Now, we want to benchmark a function that is pretty fast (but not fast enough), so we run it several thousand times:

for (let i = 0; i < 10000; i++) {
  set_up_some_state();
  monitor.start();
  code_to_be_benchmarked();
  monitor.stop();
  clean_up_afterward();
}

Only the work done by code_to_be_benchmarked(), not set_up_some_state() or clean_up_afterward(), will be measured.  The monitor object automatically accumulates counts over start/stop cycles.  When you're done benchmarking, you can read out each of the counters you asked for as properties:

let report = "CPU cycles:   " + monitor.cpu_cycles + "\n" +
             "Cache refs:   " + monitor.cache_references + "\n" +
             "Cache misses: " + monitor.cache_misses + "\n";
monitor.reset();
alert(report);

The reset method clears all of the enabled counters, as you might expect.  When you're done with your measurements, just let the monitor object go out of scope.

For more details, including a complete list of events that can be measured, see the C++ documentation.

{{ note("At present, PerfMeasurement.jsm is only functional on Linux, but it is planned to add support for Windows ({{template.Bug(583322)}}) and OSX ({{template.Bug(583323)}}) as well, and we welcome patches for other operating systems.") }}

Revision Source

<p>{{ gecko_minversion_header("2") }}</p>
<p><code>The PerfMeasurement.jsm</code> JavaScript code module lets you take detailed performance measurements of your code.</p>
<p>{{ note("The <code>PerfMeasurement.jsm</code> JavaScript code module can only be used from chrome -- that is, from within the application itself or an add-on.") }}</p>
<p>To use PerfMeasurement, first import the module:</p>
<pre class="brush: js"><span class="nowiki">Components.utils.import("resource://gre/modules/PerfMeasurement.jsm")</span></pre>
<p>You can then create a <code>PerfMeasurement</code> object. You give the constructor a bit-mask of events you are interested in. For instance, let's measure instructions executed, cache references, and cache misses:</p>
<pre class="brush: js"><span class="nowiki">let monitor = new PerfMeasurement(PerfMeasurement.CPU_CYCLES | PerfMeasurement.CACHE_REFERENCES | PerfMeasurement.CACHE_MISSES);</span></pre>
<p>This just sets up to measure stuff, it doesn't start timing anything.  Now, we want to benchmark a function that is pretty fast (but not fast enough), so we run it several thousand times:</p>
<pre class="brush: js">for (let i = 0; i &lt; 10000; i++) {
  set_up_some_state();
  monitor.start();
  code_to_be_benchmarked();
  monitor.stop();
  clean_up_afterward();
}
</pre>
<p>Only the work done by <code>code_to_be_benchmarked()</code>, not <code>set_up_some_state()</code> or <code>clean_up_afterward()</code>, will be measured.  The <code>monitor</code> object automatically accumulates counts over start/stop cycles.  When you're done benchmarking, you can read out each of the counters you asked for as properties:</p>
<pre class="brush: js">let report = "CPU cycles:   " + monitor.cpu_cycles + "\n" +
             "Cache refs:   " + monitor.cache_references + "\n" +
             "Cache misses: " + monitor.cache_misses + "\n";
monitor.reset();
alert(report);
</pre>
<p>The <code>reset</code> method clears all of the enabled counters, as you might expect.  When you're done with your measurements, just let the <code>monitor</code> object go out of scope.</p>
<p>For more details, including a complete list of events that can be measured, see the <a href="/en/JS::PerfMeasurement" title="en/JS::PerfMeasurement">C++ documentation</a>.</p>
<p>{{ note("At present, <code>PerfMeasurement.jsm</code> is only functional on Linux, but it is planned to add support for Windows ({{template.Bug(583322)}}) and OSX ({{template.Bug(583323)}}) as well, and we welcome patches for other operating systems.") }}</p>
Revert to this revision