• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.base.metrics;
6 
7 import android.test.InstrumentationTestCase;
8 import android.test.suitebuilder.annotation.SmallTest;
9 
10 import org.chromium.base.library_loader.LibraryLoader;
11 import org.chromium.base.library_loader.LibraryProcessType;
12 import org.chromium.base.test.util.MetricsUtils.HistogramDelta;
13 
14 import java.util.concurrent.TimeUnit;
15 
16 /**
17  * Tests for the Java API for recording UMA histograms.
18  */
19 public class RecordHistogramTest extends InstrumentationTestCase {
20     @Override
setUp()21     protected void setUp() throws Exception {
22         super.setUp();
23         LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER)
24                 .ensureInitialized(getInstrumentation().getTargetContext());
25         RecordHistogram.initialize();
26     }
27 
28     /**
29      * Tests recording of boolean histograms.
30      */
31     @SmallTest
testRecordBooleanHistogram()32     public void testRecordBooleanHistogram() {
33         String histogram = "HelloWorld.BooleanMetric";
34         HistogramDelta falseCount = new HistogramDelta(histogram, 0);
35         HistogramDelta trueCount = new HistogramDelta(histogram, 1);
36         assertEquals(0, trueCount.getDelta());
37         assertEquals(0, falseCount.getDelta());
38 
39         RecordHistogram.recordBooleanHistogram(histogram, true);
40         assertEquals(1, trueCount.getDelta());
41         assertEquals(0, falseCount.getDelta());
42 
43         RecordHistogram.recordBooleanHistogram(histogram, true);
44         assertEquals(2, trueCount.getDelta());
45         assertEquals(0, falseCount.getDelta());
46 
47         RecordHistogram.recordBooleanHistogram(histogram, false);
48         assertEquals(2, trueCount.getDelta());
49         assertEquals(1, falseCount.getDelta());
50     }
51 
52     /**
53      * Tests recording of enumerated histograms.
54      */
55     @SmallTest
testRecordEnumeratedHistogram()56     public void testRecordEnumeratedHistogram() {
57         String histogram = "HelloWorld.EnumeratedMetric";
58         HistogramDelta zeroCount = new HistogramDelta(histogram, 0);
59         HistogramDelta oneCount = new HistogramDelta(histogram, 1);
60         HistogramDelta twoCount = new HistogramDelta(histogram, 2);
61         final int boundary = 3;
62 
63         assertEquals(0, zeroCount.getDelta());
64         assertEquals(0, oneCount.getDelta());
65         assertEquals(0, twoCount.getDelta());
66 
67         RecordHistogram.recordEnumeratedHistogram(histogram, 0, boundary);
68         assertEquals(1, zeroCount.getDelta());
69         assertEquals(0, oneCount.getDelta());
70         assertEquals(0, twoCount.getDelta());
71 
72         RecordHistogram.recordEnumeratedHistogram(histogram, 0, boundary);
73         assertEquals(2, zeroCount.getDelta());
74         assertEquals(0, oneCount.getDelta());
75         assertEquals(0, twoCount.getDelta());
76 
77         RecordHistogram.recordEnumeratedHistogram(histogram, 2, boundary);
78         assertEquals(2, zeroCount.getDelta());
79         assertEquals(0, oneCount.getDelta());
80         assertEquals(1, twoCount.getDelta());
81     }
82 
83     /**
84      * Tests recording of count histograms.
85      */
86     @SmallTest
testRecordCountHistogram()87     public void testRecordCountHistogram() {
88         String histogram = "HelloWorld.CountMetric";
89         HistogramDelta zeroCount = new HistogramDelta(histogram, 0);
90         HistogramDelta oneCount = new HistogramDelta(histogram, 1);
91         HistogramDelta twoCount = new HistogramDelta(histogram, 2);
92         HistogramDelta eightThousandCount = new HistogramDelta(histogram, 8000);
93 
94         assertEquals(0, zeroCount.getDelta());
95         assertEquals(0, oneCount.getDelta());
96         assertEquals(0, twoCount.getDelta());
97         assertEquals(0, eightThousandCount.getDelta());
98 
99         RecordHistogram.recordCountHistogram(histogram, 0);
100         assertEquals(1, zeroCount.getDelta());
101         assertEquals(0, oneCount.getDelta());
102         assertEquals(0, twoCount.getDelta());
103         assertEquals(0, eightThousandCount.getDelta());
104 
105         RecordHistogram.recordCountHistogram(histogram, 0);
106         assertEquals(2, zeroCount.getDelta());
107         assertEquals(0, oneCount.getDelta());
108         assertEquals(0, twoCount.getDelta());
109         assertEquals(0, eightThousandCount.getDelta());
110 
111         RecordHistogram.recordCountHistogram(histogram, 2);
112         assertEquals(2, zeroCount.getDelta());
113         assertEquals(0, oneCount.getDelta());
114         assertEquals(1, twoCount.getDelta());
115         assertEquals(0, eightThousandCount.getDelta());
116 
117         RecordHistogram.recordCountHistogram(histogram, 8000);
118         assertEquals(2, zeroCount.getDelta());
119         assertEquals(0, oneCount.getDelta());
120         assertEquals(1, twoCount.getDelta());
121         assertEquals(1, eightThousandCount.getDelta());
122     }
123 
124     /**
125      * Tests recording of custom times histograms.
126      */
127     @SmallTest
testRecordCustomTimesHistogram()128     public void testRecordCustomTimesHistogram() {
129         String histogram = "HelloWorld.CustomTimesMetric";
130         HistogramDelta zeroCount = new HistogramDelta(histogram, 0);
131         HistogramDelta oneCount = new HistogramDelta(histogram, 1);
132         HistogramDelta twoCount = new HistogramDelta(histogram, 100);
133 
134         assertEquals(0, zeroCount.getDelta());
135         assertEquals(0, oneCount.getDelta());
136         assertEquals(0, twoCount.getDelta());
137 
138         TimeUnit milli = TimeUnit.MILLISECONDS;
139 
140         RecordHistogram.recordCustomTimesHistogram(histogram, 0, 1, 100, milli, 3);
141         assertEquals(1, zeroCount.getDelta());
142         assertEquals(0, oneCount.getDelta());
143         assertEquals(0, twoCount.getDelta());
144 
145         RecordHistogram.recordCustomTimesHistogram(histogram, 0, 1, 100, milli, 3);
146         assertEquals(2, zeroCount.getDelta());
147         assertEquals(0, oneCount.getDelta());
148         assertEquals(0, twoCount.getDelta());
149 
150         RecordHistogram.recordCustomTimesHistogram(histogram, 95, 1, 100, milli, 3);
151         assertEquals(2, zeroCount.getDelta());
152         assertEquals(1, oneCount.getDelta());
153         assertEquals(0, twoCount.getDelta());
154 
155         RecordHistogram.recordCustomTimesHistogram(histogram, 200, 1, 100, milli, 3);
156         assertEquals(2, zeroCount.getDelta());
157         assertEquals(1, oneCount.getDelta());
158         assertEquals(1, twoCount.getDelta());
159     }
160 
161     /**
162      * Tests recording of linear count histograms.
163      */
164     @SmallTest
testRecordLinearCountHistogram()165     public void testRecordLinearCountHistogram() {
166         String histogram = "HelloWorld.LinearCountMetric";
167         HistogramDelta zeroCount = new HistogramDelta(histogram, 0);
168         HistogramDelta oneCount = new HistogramDelta(histogram, 1);
169         HistogramDelta twoCount = new HistogramDelta(histogram, 2);
170         final int min = 1;
171         final int max = 3;
172         final int numBuckets = 4;
173 
174         assertEquals(0, zeroCount.getDelta());
175         assertEquals(0, oneCount.getDelta());
176         assertEquals(0, twoCount.getDelta());
177 
178         RecordHistogram.recordLinearCountHistogram(histogram, 0, min, max, numBuckets);
179         assertEquals(1, zeroCount.getDelta());
180         assertEquals(0, oneCount.getDelta());
181         assertEquals(0, twoCount.getDelta());
182 
183         RecordHistogram.recordLinearCountHistogram(histogram, 0, min, max, numBuckets);
184         assertEquals(2, zeroCount.getDelta());
185         assertEquals(0, oneCount.getDelta());
186         assertEquals(0, twoCount.getDelta());
187 
188         RecordHistogram.recordLinearCountHistogram(histogram, 2, min, max, numBuckets);
189         assertEquals(2, zeroCount.getDelta());
190         assertEquals(0, oneCount.getDelta());
191         assertEquals(1, twoCount.getDelta());
192     }
193 }
194