Last week, I added a small module to make it easier to track time deltas measurements that are meant for telemetry. The module is called TelemetryStopwatch and it has two functions, start and finish, which do what their name imply. When you call start it takes a timestamp, and when you call finish it calculates the delta and adds the data to the chosen histogram. It’s useful to avoid sticking start timestamps as enclosed variables or private members in objects, and in cases where the start and stop measurements are disjoint and there’s not a good place to store the stamp or add code to calculate the diff.
Cu.import("resource:///modules/TelemetryStopwatch.jsm"); TelemetryStopwatch.start("MY_TELEMETRY_HISTOGRAM"); // and later TelemetryStopwatch.finish("MY_TELEMETRY_HISTOGRAM"); // or, with the optional object parameter: TelemetryStopwatch.start("MY_TELEMETRY_HISTOGRAM", refObj); TelemetryStopwatch.finish("MY_TELEMETRY_HISTOGRAM", refObj);
You can have multiple counters at the same time. Each counter is associated with a telemetry histogram, or with a histogram + a reference object, for cases where you might need more than 1 timer running at the same time for the same histogram (e.g. some measurements related to a specific tab, where more than 1 tab might have it running at the same time). The objects are tracked on a WeakMap so you don’t have to worry about leaks. The module is in toolkit to make it widely available.