diff options
author | hartmanng <hartmanng@chromium.org> | 2015-03-12 15:13:50 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-12 22:14:16 +0000 |
commit | 39be88c7e1260c1de756f2f52e567b13196cf299 (patch) | |
tree | c94bbba1efcd6ae48ee40e582b4f8499f120acc3 | |
parent | c9b440e3abd6af75bcc630712a4f11e8acb3ba24 (diff) | |
download | chromium_src-39be88c7e1260c1de756f2f52e567b13196cf299.zip chromium_src-39be88c7e1260c1de756f2f52e567b13196cf299.tar.gz chromium_src-39be88c7e1260c1de756f2f52e567b13196cf299.tar.bz2 |
Add count histograms to RecordHistogram.
This adds the Java equivalent of the following C++ macro:
UMA_HISTOGRAM_COUNTS.
BUG=442300,463125
Review URL: https://codereview.chromium.org/1004763003
Cr-Commit-Position: refs/heads/master@{#320388}
3 files changed, 89 insertions, 0 deletions
diff --git a/base/android/java/src/org/chromium/base/metrics/RecordHistogram.java b/base/android/java/src/org/chromium/base/metrics/RecordHistogram.java index 08c314f..c1f6d36 100644 --- a/base/android/java/src/org/chromium/base/metrics/RecordHistogram.java +++ b/base/android/java/src/org/chromium/base/metrics/RecordHistogram.java @@ -43,6 +43,16 @@ public class RecordHistogram { } /** + * Records a sample in a count histogram of the given name. This is the Java equivalent of the + * UMA_HISTOGRAM_COUNTS C++ macro. + * @param name name of the histogram + * @param sample sample to be recorded, at least 1 and at most 999999 + */ + public static void recordCountHistogram(String name, int sample) { + nativeRecordCountHistogram(name, System.identityHashCode(name), sample); + } + + /** * Records a sample in a histogram of times. Useful for recording short durations. This is the * Java equivalent of the UMA_HISTOGRAM_TIMES C++ macro. * @param name name of the histogram @@ -123,6 +133,7 @@ public class RecordHistogram { private static native void nativeRecordBooleanHistogram(String name, int key, boolean sample); private static native void nativeRecordEnumeratedHistogram( String name, int key, int sample, int boundary); + private static native void nativeRecordCountHistogram(String name, int key, int sample); private static native int nativeGetHistogramValueCountForTesting(String name, int sample); private static native void nativeInitialize(); diff --git a/base/android/javatests/src/org/chromium/base/metrics/RecordHistogramTest.java b/base/android/javatests/src/org/chromium/base/metrics/RecordHistogramTest.java index f0489d3..24af056 100644 --- a/base/android/javatests/src/org/chromium/base/metrics/RecordHistogramTest.java +++ b/base/android/javatests/src/org/chromium/base/metrics/RecordHistogramTest.java @@ -80,6 +80,47 @@ public class RecordHistogramTest extends InstrumentationTestCase { } /** + * Tests recording of count histograms. + */ + @SmallTest + public void testRecordCountHistogram() { + String histogram = "HelloWorld.CountMetric"; + HistogramDelta zeroCount = new HistogramDelta(histogram, 0); + HistogramDelta oneCount = new HistogramDelta(histogram, 1); + HistogramDelta twoCount = new HistogramDelta(histogram, 2); + HistogramDelta eightThousandCount = new HistogramDelta(histogram, 8000); + + assertEquals(0, zeroCount.getDelta()); + assertEquals(0, oneCount.getDelta()); + assertEquals(0, twoCount.getDelta()); + assertEquals(0, eightThousandCount.getDelta()); + + RecordHistogram.recordCountHistogram(histogram, 0); + assertEquals(1, zeroCount.getDelta()); + assertEquals(0, oneCount.getDelta()); + assertEquals(0, twoCount.getDelta()); + assertEquals(0, eightThousandCount.getDelta()); + + RecordHistogram.recordCountHistogram(histogram, 0); + assertEquals(2, zeroCount.getDelta()); + assertEquals(0, oneCount.getDelta()); + assertEquals(0, twoCount.getDelta()); + assertEquals(0, eightThousandCount.getDelta()); + + RecordHistogram.recordCountHistogram(histogram, 2); + assertEquals(2, zeroCount.getDelta()); + assertEquals(0, oneCount.getDelta()); + assertEquals(1, twoCount.getDelta()); + assertEquals(0, eightThousandCount.getDelta()); + + RecordHistogram.recordCountHistogram(histogram, 8000); + assertEquals(2, zeroCount.getDelta()); + assertEquals(0, oneCount.getDelta()); + assertEquals(1, twoCount.getDelta()); + assertEquals(1, eightThousandCount.getDelta()); + } + + /** * Tests recording of custom times histograms. */ @SmallTest diff --git a/base/android/record_histogram.cc b/base/android/record_histogram.cc index 0df0487..8b7f7bd 100644 --- a/base/android/record_histogram.cc +++ b/base/android/record_histogram.cc @@ -60,6 +60,31 @@ class HistogramCache { return InsertLocked(j_histogram_key, histogram); } + HistogramBase* CountHistogram(JNIEnv* env, + jstring j_histogram_name, + jint j_histogram_key) { + // These values are based on the hard-coded constants in the + // UMA_HISTOGRAM_COUNTS macro from base/metrics/histogram_macros.h. + const int histogram_min = 1; + const int histogram_max = 1000000; + const int histogram_num_buckets = 50; + + DCHECK(j_histogram_name); + DCHECK(j_histogram_key); + HistogramBase* histogram = FindLocked(j_histogram_key); + if (histogram) { + DCHECK(histogram->HasConstructionArguments(histogram_min, histogram_max, + histogram_num_buckets)); + return histogram; + } + + std::string histogram_name = ConvertJavaStringToUTF8(env, j_histogram_name); + histogram = Histogram::FactoryGet(histogram_name, histogram_min, + histogram_max, histogram_num_buckets, + HistogramBase::kUmaTargetedHistogramFlag); + return InsertLocked(j_histogram_key, histogram); + } + HistogramBase* CustomTimesHistogram(JNIEnv* env, jstring j_histogram_name, jint j_histogram_key, @@ -133,6 +158,18 @@ void RecordEnumeratedHistogram(JNIEnv* env, ->Add(sample); } +void RecordCountHistogram(JNIEnv* env, + jclass clazz, + jstring j_histogram_name, + jint j_histogram_key, + jint j_sample) { + int sample = static_cast<int>(j_sample); + + g_histograms.Get() + .CountHistogram(env, j_histogram_name, j_histogram_key) + ->Add(sample); +} + void RecordCustomTimesHistogramMilliseconds(JNIEnv* env, jclass clazz, jstring j_histogram_name, |