1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dexfuzz;
18 
19 import dexfuzz.listeners.BaseListener;
20 
21 /**
22  * For timing splits of program execution.
23  */
24 public class Timer {
25   /**
26    * The name of the timer, the phase of the program it is intended to time.
27    */
28   private String name;
29 
30   /**
31    * A point in time remembered when start() is called.
32    */
33   private long startPoint;
34 
35   /**
36    * A cumulative count of how much time has elapsed. Updated each time
37    * stop() is called.
38    */
39   private long elapsedTime;
40 
41   /**
42    * Initialise a new timer with the provided name.
43    */
Timer(String name)44   public Timer(String name) {
45     this.name = name;
46     this.elapsedTime = 0L;
47   }
48 
49   /**
50    * Start timing.
51    */
start()52   public void start() {
53     startPoint = System.currentTimeMillis();
54   }
55 
56   /**
57    * Stop timing, update how much time has elapsed.
58    */
stop()59   public void stop() {
60     long endPoint = System.currentTimeMillis();
61     elapsedTime += (endPoint - startPoint);
62   }
63 
64   /**
65    * Log the elapsed time this timer has recorded.
66    */
printTime(BaseListener listener)67   public void printTime(BaseListener listener) {
68     listener.handleTiming(name, ((float)elapsedTime) / 1000.0f);
69   }
70 }
71