• 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.test.util;
6 
7 import org.chromium.base.metrics.RecordHistogram;
8 
9 /**
10  * Helpers for testing UMA metrics.
11  */
12 public class MetricsUtils {
13     /**
14      * Helper class that snapshots the given bucket of the given UMA histogram on its creation,
15      * allowing to inspect the number of samples recorded during its lifetime.
16      */
17     public static class HistogramDelta {
18         private final String mHistogram;
19         private final int mSampleValue;
20 
21         private final int mInitialCount;
22 
get()23         private int get() {
24             return RecordHistogram.getHistogramValueCountForTesting(mHistogram, mSampleValue);
25         }
26 
27         /**
28          * Snapshots the given bucket of the given histogram.
29          * @param histogram name of the histogram to snapshot
30          * @param sampleValue the bucket that contains this value will be snapshot
31          */
HistogramDelta(String histogram, int sampleValue)32         public HistogramDelta(String histogram, int sampleValue) {
33             mHistogram = histogram;
34             mSampleValue = sampleValue;
35             mInitialCount = get();
36         }
37 
38         /** Returns the number of samples of the snapshot bucket recorded since creation */
getDelta()39         public int getDelta() {
40             return get() - mInitialCount;
41         }
42     }
43 }
44