mozdev.org

hrtimer

resources:

hrtimer is a project that includes hrprofiler, a new profiler for Javascript in Mozilla, hrcov, a tool for Javascript code coverage, and hrtimer proper, a facility for script to directly access various system timers and metrics.

hrcov | hrprofiler | hrtimer


hrcov

[screenshot]

hrcov is a simple code coverage tool for Javascript. It will list all scripts Firefox knows about, and the lines of which that were not executed in a given time period. No other similar tool exists for Javascript currently. As of January 2008, the only tools available are a Firebug plugin that only gives function coverage, and a preprocessor. hrcov is the only Javascript code coverage tool that provides line-by-line coverage information.

The main use case for hrcov is unit testing: we want to be able to ensure that a unit test covers all aspects of a given piece of functionality.

Like all hrtimer tools, hrcov works equally well with content and chrome code.

outline

hrcov can divide a source file into chunks and report on the coverage of each chunk separately. It divides files the same way Emacs' outline-mode does, and understands the same format of file variables. For example, look at ffhrcov_result.js. At the end of the file, we have:

        // Local Variables:
        // outline-regexp: "// ==+\\s-+"
        // End:
      

This block means that consecutive sections of the file are separated by lines beginning with "// ==", a variable number of '=' signs, and more space. hrcov will automatically figure out file sectioning based on these lines and display the results as a tree in the coverage report.


hrprofiler

[screenshot]

hrprofiler is a new profiler for content and chrome Javascript. Unlike Venkman and the Firebug profiler, hrprofiler will:

  • Determine function names accurately and quickly, giving meaningful results for anonymous functions on object prototypes. For example,
      MyObject.prototype.foo = function() { return 42 }
    will be reported as MyObject.foo (or MyObject.prototype.foo, depending on the options selected).
  • Generate a call graph, allowing hrprofiler to report callers and callees for every function. This allows you to figure out not only which functions are expensive, but why they're being called.
  • Profile using all of the hrtimer time sources below, including user CPU time, kernel CPU time, wall time, page faults, and memory allocation.

hrtimer API

The hrtimer API is exposed to all scripts (even unprivileged ones running on the web) via a window-level object called hrtimer. hrtimer implements IHRTimerFactory.

Get an Array of available timers like this:

        // The {} is a dummy argument required by xpconnect
        var available_timers = hrtimer.getAvailableTimers({});
      

Each entry in the array is a string, the name of a timer. To get corresponding timer object, simply call:

        var my_timer = hrtimer.getTimer(name_of_timer)
      

Timer objects are documented in the linked-to IDL file above. Simply put, get the current value of the timer by calling:

        var time_in_ns = my_timer.getTime()
      

Timers for POSIX systems (including OS X)

Timers for Windows

What's New

  • 2009-01-10: Version 1.3 released, with code coverage support
  • mozdev.org

The hrtimer project can be contacted through the mailing list or the member list.
Copyright © 2000-2014. All rights reserved. Terms of Use & Privacy Policy.