forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimingInfo.java
More file actions
356 lines (314 loc) · 11.6 KB
/
TimingInfo.java
File metadata and controls
356 lines (314 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.http.annotation.NotThreadSafe;
@NotThreadSafe
public class TimingInfo {
private static final int UNKNOWN = -1;
/**
* The wall clock time (as the number of milliseconds since Epoch)
* of when the timing measurement starts; or -1 if unknown.
* This field is not meant to be used for timing measurement.
* For more info, see:
* https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks
*/
private final long startEpochTimeMilli;
/**
* Start time in nanosecond used for timing measurement.
* Note the value in this field may have nothing to do with
* the wall clock time.
* The wall clock time of when the timing measurement starts
* can optionally be captured in {@link #startEpochTimeMilli}.
* For more info, see:
* https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks
*/
private final long startTimeNano;
/**
* End time in nanosecond used for timing measurement or -1 if unknown.
* Note the value in this field is only meant to be used for timing
* measurement, and is not directly related to the wall clock time.
* For more info, see:
* https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks
*/
private long endTimeNano;
private final Map<String, List<TimingInfo>> subMeasurementsByName = new HashMap<String, List<TimingInfo>>();
private final Map<String, Number> countersByName = new HashMap<String, Number>();
/**
* @deprecated The best practice is to use nanoseconds for timing measurement
* in Java. Use {@link #startTiming()} instead.
* <p>
* Captures the current start time in millisecond.
*
* @see TimingInfo#startTiming()
*/
@Deprecated
public TimingInfo() {
this(System.currentTimeMillis(), System.nanoTime(), UNKNOWN);
}
/**
* @deprecated The best practice is to use nanoseconds for timing measurement
* in Java.
* The time unit of the input parameter was ambiguous,
* and now assumed to be nanosecond.
* Please use {@link #startTiming(long)} instead.
* <p>
* Captures the start time in millisecond.
*
* @see TimingInfo#newTimingInfo(long))
*/
@Deprecated
public TimingInfo(long startTimeNano) {
this(UNKNOWN, startTimeNano, UNKNOWN);
}
/**
* @deprecated The best practice is to use nanoseconds for timing measurement
* in Java.
* The time unit of the input parameters were ambiguous,
* and now assumed to be nanosecond based on empirical observation.
* Please use {@link #newTimingInfo(long, long)} instead.
*
* @see Timing#newTimingInfo(long, long))
*/
@Deprecated
public TimingInfo(long startTimeNano, long endTimeNano) {
this(UNKNOWN, startTimeNano, endTimeNano);
}
/**
* Captures the current wall clock time (since epoch in millisecond)
* and the current time (in nanosecond) used for timing measurement.
* For more info, see:
* https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks
*/
public static TimingInfo startTiming() {
return new TimingInfo(System.currentTimeMillis(), System.nanoTime(), UNKNOWN);
}
/**
* Captures the given start time in nanosecond, ignoring the wall clock time.
*
* @param startTimeNano start time in nanosecond
*/
public static TimingInfo startTiming(long startTimeNano) {
return new TimingInfo(UNKNOWN, startTimeNano, UNKNOWN);
}
/**
* Returns a {@link TimingInfo} based on the given
* start and end time in nanosecond, ignoring the wall clock time.
*
* @param startTimeNano start time in nanosecond
* @param endTimeNano end time in nanosecond
*/
public static TimingInfo newTimingInfo(long startTimeNano, long endTimeNano) {
return new TimingInfo(UNKNOWN, startTimeNano, endTimeNano);
}
/**
* Returns a {@link TimingInfo} based on the given
* start time since epoch in millisecond,
* and the given start and end time in nanosecond.
*
* @param startEpochTimeMilli start time since epoch in millisecond
* @param startTimeNano start time in nanosecond
* @param endTimeNano end time in nanosecond
*/
public static TimingInfo newTimingInfo(
long startEpochTimeMilli, long startTimeNano, long endTimeNano) {
return new TimingInfo(startEpochTimeMilli, startTimeNano, endTimeNano);
}
/**
* A private ctor to facilitate the deprecation of using millisecond and
* migration to using nanosecond for timing measurement.
*
* @param startEpochTimeMilli start time since epoch in millisecond
* @param startTimeNano start time in nanosecond
* @param endTimeNano end time in nanosecond
*/
private TimingInfo(long startEpochTimeMilli, long startTimeNano, long endTimeNano) {
this.startEpochTimeMilli = startEpochTimeMilli;
this.startTimeNano = startTimeNano;
this.endTimeNano = endTimeNano;
}
/**
* @deprecated by {#link {@link #getStartEpochTimeMilli()} and {@link #getStartTimeNano()}.
* <p>
* The time unit of the start time returned was ambiguous,
* and is now assumed to be the wall clock time in millisecond since Epoch
* when the timing measurement starts.
*
* @see #getStartEpochTimeMilli()
* @see #getStartTimeNano()
*/
@Deprecated
public long getStartTime() {
return startEpochTimeMilli < 0
// best effort even though technically this is incorrect
? TimeUnit.NANOSECONDS.toMillis(startTimeNano)
: startEpochTimeMilli
;
}
/**
* Returns the wall clock time (as the number of milliseconds since Epoch)
* of when the timing measurement starts; or -1 if unknown.
*/
public long getStartEpochTimeMilli() {
return startEpochTimeMilli;
}
/**
* Returns the start time (in nanosecond) used for timing measurement.
*/
public long getStartTimeNano() {
return startTimeNano;
}
/**
* @deprecated by {#link {@link #getEndEpochTimeMilli()} and {@link #getEndTimeNano()}.
* The time unit of the returned end time was ambiguous,
* and is now assumed to be the wall clock time in millisecond since Epoch
* when the timing measurement ends; or -1 if unknown.
*
* @see #getEndEpochTimeMilli()
* @see #getEndTimeNano()
*/
@Deprecated
public long getEndTime() {
return getEndEpochTimeMilli();
}
/**
* Returns the wall clock time (as the number of milliseconds since Epoch)
* of when the timing measurement ended; or -1 if unknown.
*
* @see #getEndTimeNano()
*/
public long getEndEpochTimeMilli() {
return startEpochTimeMilli < 0 || endTimeNano < 0
? UNKNOWN
// make use of the wall clock time and elpased time
: startEpochTimeMilli + getElapsedTimeMillis();
}
/**
* Returns the end time (in nanoseconds) used for timing measurement;
* or -1 if unknown.
*/
public long getEndTimeNano() {
return endTimeNano;
}
/**
* Returns the elapsed time in millisecond
* (as double and therefore with higher precision than
* if it was returned in long).
*
* @see #getElapsedTimeMillis()
*/
public double getTimeTakenMillis() {
double micros = (double)TimeUnit.NANOSECONDS.toMicros(endTimeNano - startTimeNano);
return micros / 1000.0; // convert microseconds to milliseconds in double rather than long, preserving the precision
}
/**
* Returns the elapsed time in millisecond (as long).
*
* @see #getTimeTakenMillis()
*/
public long getElapsedTimeMillis() {
return TimeUnit.NANOSECONDS.toMillis(endTimeNano - startTimeNano);
}
@Override
public String toString() {
return String.valueOf(getTimeTakenMillis());
}
/**
* @deprecated The best practice is to use nanoseconds for timing measurement
* in Java.
* The time unit of the input parameter was ambiguous,
* and now assumed to be millisecond based on empirical observation.
*
* @param endTimeMilli end time in millisecond
*
* @see #setEndTimeNano(long)
*/
@Deprecated
public void setEndTime(long endTimeMilli) {
this.endTimeNano = TimeUnit.MILLISECONDS.toNanos(endTimeMilli);
}
/**
* Sets the end time (in nanosecond) used for timing measurement.
*
* @see TimingInfo#endTiming()
*/
public void setEndTimeNano(long endTimeNano) {
this.endTimeNano = endTimeNano;
}
/**
* Captures the current end time (in nanosecond) used for
* timing measurement.
*/
public TimingInfo endTiming() {
this.endTimeNano = System.nanoTime();
return this;
}
public void addSubMeasurement(String subMeasurementName, TimingInfo timingInfo) {
List<TimingInfo> timings = subMeasurementsByName.get(subMeasurementName);
if (timings == null) {
timings = new ArrayList<TimingInfo>();
subMeasurementsByName.put(subMeasurementName, timings);
}
timings.add(timingInfo);
}
public TimingInfo getSubMeasurement(String subMeasurementName) {
return getSubMeasurement(subMeasurementName, 0);
}
public TimingInfo getSubMeasurement(String subMesurementName, int index) {
List<TimingInfo> timings = subMeasurementsByName.get(subMesurementName);
if (index < 0 || timings == null || timings.size() == 0
|| index >= timings.size()) {
return null;
}
return timings.get(index);
}
public TimingInfo getLastSubMeasurement(String subMeasurementName) {
if (subMeasurementsByName == null || subMeasurementsByName.size() == 0) {
return null;
}
List<TimingInfo> timings = subMeasurementsByName.get(subMeasurementName);
if (timings == null || timings.size() == 0) {
return null;
}
return timings.get(timings.size() - 1);
}
public List<TimingInfo> getAllSubMeasurements(String subMeasurementName) {
return subMeasurementsByName.get(subMeasurementName);
}
public Map<String, List<TimingInfo>> getSubMeasurementsByName() {
return subMeasurementsByName;
}
public Number getCounter(String key) {
return countersByName.get(key);
}
public Map<String, Number> getAllCounters() {
return countersByName;
}
public void setCounter(String key, long count) {
countersByName.put(key, count);
}
public void incrementCounter(String key) {
int count = 0;
Number counter = getCounter(key);
if (counter != null) {
count = counter.intValue();
}
setCounter(key, ++count);
}
}