• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Chromium Authors
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 androidx.annotation.IntDef;
8 import androidx.annotation.NonNull;
9 
10 import org.chromium.base.TimeUtils;
11 
12 import java.lang.annotation.Retention;
13 import java.lang.annotation.RetentionPolicy;
14 
15 /**
16  * A class to be used with a try-with-resources to record the elapsed time within the try
17  * block. Measures time elapsed between instantiation and the call to close using supplied time
18  * source.
19  */
20 public class TimingMetric implements AutoCloseable {
21     @IntDef({TimerType.SHORT_UPTIME, TimerType.MEDIUM_UPTIME, TimerType.SHORT_THREAD_TIME})
22     @Retention(RetentionPolicy.SOURCE)
23     private @interface TimerType {
24         int SHORT_UPTIME = 0;
25         int MEDIUM_UPTIME = 1;
26         int SHORT_THREAD_TIME = 2;
27     }
28 
29     private final String mMetricName;
30     private final @TimerType int mTimerType;
31     private long mStartTime;
32 
33     /**
34      * Create a new TimingMetric measuring wall time (ie. time experienced by the user) of
35      * up to 10 seconds.
36      *
37      * @param metric The name of the histogram to record.
38      */
shortUptime(@onNull String metricName)39     public static TimingMetric shortUptime(@NonNull String metricName) {
40         TimingMetric ret = new TimingMetric(metricName, TimerType.SHORT_UPTIME);
41         ret.mStartTime = TimeUtils.uptimeMillis();
42         return ret;
43     }
44 
45     /**
46      * Create a new TimingMetric measuring wall time (ie. time experienced by the user) of up to 3
47      * minutes.
48      *
49      * @param metricName The name of the histogram to record.
50      */
mediumUptime(@onNull String metricName)51     public static TimingMetric mediumUptime(@NonNull String metricName) {
52         TimingMetric ret = new TimingMetric(metricName, TimerType.MEDIUM_UPTIME);
53         ret.mStartTime = TimeUtils.uptimeMillis();
54         return ret;
55     }
56 
57     /**
58      * Create a new TimingMetric measuring thread time (ie. actual time spent executing the code) of
59      * up to 10 seconds.
60      *
61      * @param metricName The name of the histogram to record.
62      */
shortThreadTime(@onNull String metricName)63     public static TimingMetric shortThreadTime(@NonNull String metricName) {
64         TimingMetric ret = new TimingMetric(metricName, TimerType.SHORT_THREAD_TIME);
65         ret.mStartTime = TimeUtils.currentThreadTimeMillis();
66         return ret;
67     }
68 
TimingMetric(String metricName, @TimerType int timerType)69     private TimingMetric(String metricName, @TimerType int timerType) {
70         mMetricName = metricName;
71         mTimerType = timerType;
72     }
73 
74     @Override
close()75     public void close() {
76         String metricName = mMetricName;
77         long startTime = mStartTime;
78         if (startTime == 0) return;
79         mStartTime = 0;
80 
81         switch (mTimerType) {
82             case TimerType.SHORT_UPTIME:
83                 RecordHistogram.recordTimesHistogram(
84                         metricName, TimeUtils.uptimeMillis() - startTime);
85                 break;
86             case TimerType.MEDIUM_UPTIME:
87                 RecordHistogram.recordMediumTimesHistogram(
88                         metricName, TimeUtils.uptimeMillis() - startTime);
89                 break;
90             case TimerType.SHORT_THREAD_TIME:
91                 RecordHistogram.recordTimesHistogram(
92                         metricName, TimeUtils.currentThreadTimeMillis() - startTime);
93                 break;
94         }
95     }
96 }
97