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