toolkit/components/telemetry/TelemetryStopwatch.jsm

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 const Cc = Components.classes;
     6 const Ci = Components.interfaces;
     7 const Cu = Components.utils;
     9 this.EXPORTED_SYMBOLS = ["TelemetryStopwatch"];
    11 let Telemetry = Cc["@mozilla.org/base/telemetry;1"]
    12                   .getService(Ci.nsITelemetry);
    14 // simpleTimers are directly associated with a histogram
    15 // name. objectTimers are associated with an object _and_
    16 // a histogram name.
    17 let simpleTimers = {};
    18 let objectTimers = new WeakMap();
    20 this.TelemetryStopwatch = {
    21   /**
    22    * Starts a timer associated with a telemetry histogram. The timer can be
    23    * directly associated with a histogram, or with a pair of a histogram and
    24    * an object.
    25    *
    26    * @param aHistogram a string which must be a valid histogram name
    27    *                   from TelemetryHistograms.h
    28    *
    29    * @param aObj Optional parameter. If specified, the timer is associated
    30    *             with this object, meaning that multiple timers for a same
    31    *             histogram may be run concurrently, as long as they are
    32    *             associated with different objects.
    33    *
    34    * @return true if the timer was successfully started, false otherwise. If a
    35    *         timer already exists, it can't be started again, and the existing
    36    *         one will be cleared in order to avoid measurements errors.
    37    */
    38   start: function(aHistogram, aObj) {
    39     if (!validTypes(aHistogram, aObj))
    40       return false;
    42     let timers;
    43     if (aObj) {
    44       timers = objectTimers.get(aObj, {});
    45       objectTimers.set(aObj, timers);
    46     } else {
    47       timers = simpleTimers;
    48     }
    50     if (timers.hasOwnProperty(aHistogram)) {
    51       delete timers[aHistogram];
    52       Cu.reportError("TelemetryStopwatch: key \"" +
    53                      aHistogram + "\" was already initialized");
    54       return false;
    55     }
    57     timers[aHistogram] = Date.now();
    58     return true;
    59   },
    61   /**
    62    * Deletes the timer associated with a telemetry histogram. The timer can be
    63    * directly associated with a histogram, or with a pair of a histogram and
    64    * an object. Important: Only use this method when a legitimate cancellation
    65    * should be done.
    66    *
    67    * @param aHistogram a string which must be a valid histogram name
    68    *                   from TelemetryHistograms.json
    69    *
    70    * @param aObj Optional parameter. If specified, the timer is associated
    71    *             with this object, meaning that multiple timers for a same
    72    *             histogram may be run concurrently, as long as they are
    73    *             associated with different objects.
    74    *
    75    * @return true if the timer exist and it was cleared, false otherwise.
    76    */
    77   cancel: function ts_cancel(aHistogram, aObj) {
    78     if (!validTypes(aHistogram, aObj))
    79       return false;
    81     let timers = aObj
    82                  ? objectTimers.get(aObj, {})
    83                  : simpleTimers;
    85     if (timers.hasOwnProperty(aHistogram)) {
    86       delete timers[aHistogram];
    87       return true;
    88     }
    90     return false;
    91   },
    93   /**
    94    * Stops the timer associated with the given histogram (and object),
    95    * calculates the time delta between start and finish, and adds the value
    96    * to the histogram.
    97    *
    98    * @param aHistogram a string which must be a valid histogram name
    99    *                   from TelemetryHistograms.h. If an invalid name
   100    *                   is given, the function will throw.
   101    *
   102    * @param aObj Optional parameter which associates the histogram timer with
   103    *             the given object.
   104    *
   105    * @return true if the timer was succesfully stopped and the data was
   106    *         added to the histogram, false otherwise.
   107    */
   108   finish: function(aHistogram, aObj) {
   109     if (!validTypes(aHistogram, aObj))
   110       return false;
   112     let timers = aObj
   113                  ? objectTimers.get(aObj, {})
   114                  : simpleTimers;
   116     let start = timers[aHistogram];
   117     delete timers[aHistogram];
   119     if (start) {
   120       let delta = Date.now() - start;
   121       let histogram = Telemetry.getHistogramById(aHistogram);
   122       histogram.add(delta);
   123       return true;
   124     }
   126     return false;
   127   }
   128 }
   130 function validTypes(aKey, aObj) {
   131   return (typeof aKey == "string") &&
   132          (aObj === undefined || typeof aObj == "object");
   133 }

mercurial