1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice; |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| public final class Stopwatch { |
29 |
| String label = null; |
30 |
| long total = 0; |
31 |
| long begin = 0; |
32 |
| long end = 0; |
33 |
| |
34 |
0
| public Stopwatch() {
|
35 |
| } |
36 |
| |
37 |
0
| public Stopwatch(boolean immediate) {
|
38 |
0
| if (immediate) {
|
39 |
0
| start();
|
40 |
| } |
41 |
| } |
42 |
| |
43 |
0
| public Stopwatch(String label) {
|
44 |
0
| this.label = label;
|
45 |
| } |
46 |
| |
47 |
84
| public Stopwatch(String label, boolean immediate) {
|
48 |
84
| this.label = label;
|
49 |
84
| if (immediate) {
|
50 |
84
| start();
|
51 |
| } |
52 |
| } |
53 |
| |
54 |
84
| public void start() {
|
55 |
84
| begin = System.currentTimeMillis();
|
56 |
| } |
57 |
| |
58 |
84
| public long stop() {
|
59 |
84
| end = System.currentTimeMillis();
|
60 |
84
| total += (end - begin);
|
61 |
84
| return total;
|
62 |
| } |
63 |
| |
64 |
0
| public void cancel() {
|
65 |
0
| begin = 0;
|
66 |
0
| end = 0;
|
67 |
| } |
68 |
| |
69 |
0
| public void reset() {
|
70 |
0
| total = 0;
|
71 |
0
| begin = 0;
|
72 |
0
| end = 0;
|
73 |
| } |
74 |
| |
75 |
84
| public long elapsed() {
|
76 |
84
| if (end != 0) {
|
77 |
84
| return (end - begin);
|
78 |
| } else { |
79 |
0
| return System.currentTimeMillis() - begin;
|
80 |
| } |
81 |
| } |
82 |
| |
83 |
0
| public long total() {
|
84 |
0
| if (end != 0) {
|
85 |
0
| return total;
|
86 |
| } else { |
87 |
0
| return (System.currentTimeMillis() - begin) + total;
|
88 |
| } |
89 |
| } |
90 |
| |
91 |
0
| public String toString() {
|
92 |
0
| long t = total();
|
93 |
| |
94 |
0
| StringBuffer sb = new StringBuffer();
|
95 |
0
| if (label != null) {
|
96 |
0
| sb.append(label + ": ");
|
97 |
| } |
98 |
| |
99 |
0
| long hour = t / 3600000;
|
100 |
0
| if (hour > 0) {
|
101 |
0
| sb.append(hour + "h ");
|
102 |
0
| t = t % 3600000;
|
103 |
| } |
104 |
| |
105 |
0
| long min = t / 60000;
|
106 |
0
| if (min > 0) {
|
107 |
0
| sb.append(min + "m ");
|
108 |
0
| t = t % 60000;
|
109 |
| } |
110 |
| |
111 |
0
| long sec = t / 1000;
|
112 |
0
| if (sec > 0) {
|
113 |
0
| sb.append(sec + "s ");
|
114 |
0
| t = t % 1000;
|
115 |
| } |
116 |
| |
117 |
0
| if (t > 0) {
|
118 |
0
| sb.append(t + "ms");
|
119 |
| } |
120 |
| |
121 |
0
| return sb.toString();
|
122 |
| } |
123 |
| } |
124 |
| |