/base/android/javatests/src/org/chromium/base/metrics/RecordHistogramTest.java

http://github.com/chromium/chromium · Java · 196 lines · 144 code · 31 blank · 21 comment · 0 complexity · 5316c782a47059e1f4d5691901f4cfdc MD5 · raw file

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