Bug 741281 - Add Telemetry timers in Java. r=blassey
authorGian-Carlo Pascutto <gpascutto@mozilla.com>
Thu, 12 Apr 2012 23:19:57 +0200
changeset 91541 845bd13a5ebec41086571fcae956ec8e04203232
parent 91540 c2eb46478efccf4d009a49382a02f24405388c81
child 91542 89e69819fa98621f398f8af831f0a6275cd01bb6
push id8267
push usergpascutto@mozilla.com
push dateThu, 12 Apr 2012 21:20:21 +0000
treeherdermozilla-inbound@89e69819fa98 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersblassey
bugs741281
milestone14.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 741281 - Add Telemetry timers in Java. r=blassey
mobile/android/base/Telemetry.java
--- a/mobile/android/base/Telemetry.java
+++ b/mobile/android/base/Telemetry.java
@@ -1,18 +1,18 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * 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/. */
 
 package org.mozilla.gecko;
 
+import android.os.SystemClock;
 import android.util.Log;
 
-import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
 public class Telemetry {
     private static final String LOGTAG = "Telemetry";
 
     // Define new histograms in:
     // toolkit/components/telemetry/TelemetryHistograms.h
@@ -28,9 +28,28 @@ public class Telemetry {
                 GeckoEvent.createBroadcastEvent("Telemetry:Add", jsonData.toString());
             GeckoAppShell.sendEventToGecko(event);
 
             Log.v(LOGTAG, "Sending telemetry: " + jsonData.toString());
         } catch (JSONException e) {
             Log.e(LOGTAG, "JSON exception: ", e);
         }
     }
+
+    public static class Timer {
+        private long mStartTime;
+        private String mName;
+
+        public Timer(String name) {
+            mName = name;
+            mStartTime = SystemClock.uptimeMillis();
+        }
+
+        public void stop() {
+            long elapsed = SystemClock.uptimeMillis() - mStartTime;
+            if (elapsed < Integer.MAX_VALUE) {
+                HistogramAdd(mName, (int)(elapsed));
+            } else {
+                Log.e(LOGTAG, "Duration of " + elapsed + " ms is too long.");
+            }
+        }
+    }
 }