browser/modules/TelemetryTimestamps.jsm
author Jeff Muizelaar <jmuizelaar@mozilla.com>
Mon, 16 Apr 2012 14:43:06 -0400
changeset 91803 0d0ac6a97737209b487ba276b97f3d102363603d
parent 86155 62d54744798702e3a98c2267f5aae9dc9d40c5a3
child 111771 1545e91c658ec9cd25d9750a43bbe7afd8057cf9
permissions -rw-r--r--
Bug 726352. Move SAMPLE_LABEL from nsEventListenerManager::HandleEventInternal r=smaug This moves it to nsEventDispatcher::Dispatch which is not as hot.

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

let EXPORTED_SYMBOLS = ["TelemetryTimestamps"];

/**
 * This module's purpose is to collect timestamps for important
 * application-specific events.
 *
 * The TelemetryPing component attaches the timestamps stored by this module to
 * the telemetry submission, substracting the process lifetime so that the times
 * are relative to process startup. The overall goal is to produce a basic
 * timeline of the startup process.
 */
let timeStamps = {};

let TelemetryTimestamps = {
  /**
   * Adds a timestamp to the list. The addition of TimeStamps that already have
   * a value stored is ignored.
   *
   * @param name must be a unique, generally "camelCase" descriptor of what the
   *             timestamp represents. e.g.: "delayedStartupStarted"
   * @param value is a timeStamp in milliseconds since the epoch. If omitted,
   *              defaults to Date.now().
   */
  add: function TT_add(name, value) {
    // Default to "now" if not specified
    if (value == null)
      value = Date.now();

    if (isNaN(value))
      throw new Error("Value must be a timestamp");

    // If there's an existing value, just ignore the new value.
    if (timeStamps.hasOwnProperty(name))
      return;

    timeStamps[name] = value;
  },

  /**
   * Returns a JS object containing all of the timeStamps as properties (can be
   * easily serialized to JSON). Used by TelemetryPing to retrieve the data
   * to attach to the telemetry submission.
   */
  get: function TT_get() {
    // Return a copy of the object by passing it through JSON.
    return JSON.parse(JSON.stringify(timeStamps));
  }
};