01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tctest.perf.collections;
05:
06: import com.tc.util.Assert;
07:
08: public class Timer {
09:
10: private long startMillis;
11: private long endMillis;
12: private long elapsedMillis;
13: private long maxElapsed;
14: private long minElapsed;
15: private long totalElapsed;
16: private long lapseCount;
17:
18: public void start() {
19: reset();
20: startMillis = System.currentTimeMillis();
21: endMillis = 0;
22: }
23:
24: public void stop() {
25: endMillis = System.currentTimeMillis();
26: updateStats();
27: }
28:
29: public void reset() {
30: startMillis = 0;
31: endMillis = 0;
32: }
33:
34: public void restAll() {
35: reset();
36: maxElapsed = 0;
37: minElapsed = 0;
38: totalElapsed = 0;
39: lapseCount = 0;
40: }
41:
42: public long getElapsedMillis() {
43: return elapsedMillis;
44: }
45:
46: public long getLapseCount() {
47: return lapseCount;
48: }
49:
50: public long getMaxElapsed() {
51: return maxElapsed;
52: }
53:
54: public long getMinElapsed() {
55: return minElapsed;
56: }
57:
58: public long getTotalElapsed() {
59: return totalElapsed;
60: }
61:
62: public long getAvgElapsed() {
63: if (lapseCount == 0) {
64: Assert.inv(totalElapsed == 0);
65: return 0;
66: }
67: return totalElapsed / lapseCount;
68: }
69:
70: private void updateStats() {
71: elapsedMillis = endMillis - startMillis;
72: maxElapsed = Math.max(maxElapsed, elapsedMillis);
73: minElapsed = Math.min(minElapsed, elapsedMillis);
74: totalElapsed += elapsedMillis;
75: lapseCount++;
76: }
77:
78: }
|