1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
3 | * contributor license agreements. See the NOTICE file distributed with this |
---|
4 | * work for additional information regarding copyright ownership. The ASF |
---|
5 | * licenses this file to you under the Apache License, Version 2.0 (the |
---|
6 | * "License"); you may not use this file except in compliance with the License. |
---|
7 | * You may obtain a copy of the License at |
---|
8 | * |
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | * |
---|
11 | * Unless required by applicable law or agreed to in writing, software |
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
---|
13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
---|
14 | * License for the specific language governing permissions and limitations under |
---|
15 | * the License. |
---|
16 | */ |
---|
17 | package org.apache.hadoop.io.file.tfile; |
---|
18 | |
---|
19 | /** |
---|
20 | * A nano-second timer. |
---|
21 | */ |
---|
22 | public class NanoTimer { |
---|
23 | private long last = -1; |
---|
24 | private boolean started = false; |
---|
25 | private long cumulate = 0; |
---|
26 | |
---|
27 | /** |
---|
28 | * Constructor |
---|
29 | * |
---|
30 | * @param start |
---|
31 | * Start the timer upon construction. |
---|
32 | */ |
---|
33 | public NanoTimer(boolean start) { |
---|
34 | if (start) this.start(); |
---|
35 | } |
---|
36 | |
---|
37 | /** |
---|
38 | * Start the timer. |
---|
39 | * |
---|
40 | * Note: No effect if timer is already started. |
---|
41 | */ |
---|
42 | public void start() { |
---|
43 | if (!this.started) { |
---|
44 | this.last = System.nanoTime(); |
---|
45 | this.started = true; |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * Stop the timer. |
---|
51 | * |
---|
52 | * Note: No effect if timer is already stopped. |
---|
53 | */ |
---|
54 | public void stop() { |
---|
55 | if (this.started) { |
---|
56 | this.started = false; |
---|
57 | this.cumulate += System.nanoTime() - this.last; |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Read the timer. |
---|
63 | * |
---|
64 | * @return the elapsed time in nano-seconds. Note: If the timer is never |
---|
65 | * started before, -1 is returned. |
---|
66 | */ |
---|
67 | public long read() { |
---|
68 | if (!readable()) return -1; |
---|
69 | |
---|
70 | return this.cumulate; |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Reset the timer. |
---|
75 | */ |
---|
76 | public void reset() { |
---|
77 | this.last = -1; |
---|
78 | this.started = false; |
---|
79 | this.cumulate = 0; |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Checking whether the timer is started |
---|
84 | * |
---|
85 | * @return true if timer is started. |
---|
86 | */ |
---|
87 | public boolean isStarted() { |
---|
88 | return this.started; |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Format the elapsed time to a human understandable string. |
---|
93 | * |
---|
94 | * Note: If timer is never started, "ERR" will be returned. |
---|
95 | */ |
---|
96 | public String toString() { |
---|
97 | if (!readable()) { |
---|
98 | return "ERR"; |
---|
99 | } |
---|
100 | |
---|
101 | return NanoTimer.nanoTimeToString(this.cumulate); |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * A utility method to format a time duration in nano seconds into a human |
---|
106 | * understandable stirng. |
---|
107 | * |
---|
108 | * @param t |
---|
109 | * Time duration in nano seconds. |
---|
110 | * @return String representation. |
---|
111 | */ |
---|
112 | public static String nanoTimeToString(long t) { |
---|
113 | if (t < 0) return "ERR"; |
---|
114 | |
---|
115 | if (t == 0) return "0"; |
---|
116 | |
---|
117 | if (t < 1000) { |
---|
118 | return t + "ns"; |
---|
119 | } |
---|
120 | |
---|
121 | double us = (double) t / 1000; |
---|
122 | if (us < 1000) { |
---|
123 | return String.format("%.2fus", us); |
---|
124 | } |
---|
125 | |
---|
126 | double ms = us / 1000; |
---|
127 | if (ms < 1000) { |
---|
128 | return String.format("%.2fms", ms); |
---|
129 | } |
---|
130 | |
---|
131 | double ss = ms / 1000; |
---|
132 | if (ss < 1000) { |
---|
133 | return String.format("%.2fs", ss); |
---|
134 | } |
---|
135 | |
---|
136 | long mm = (long) ss / 60; |
---|
137 | ss -= mm * 60; |
---|
138 | long hh = mm / 60; |
---|
139 | mm -= hh * 60; |
---|
140 | long dd = hh / 24; |
---|
141 | hh -= dd * 24; |
---|
142 | |
---|
143 | if (dd > 0) { |
---|
144 | return String.format("%dd%dh", dd, hh); |
---|
145 | } |
---|
146 | |
---|
147 | if (hh > 0) { |
---|
148 | return String.format("%dh%dm", hh, mm); |
---|
149 | } |
---|
150 | |
---|
151 | if (mm > 0) { |
---|
152 | return String.format("%dm%.1fs", mm, ss); |
---|
153 | } |
---|
154 | |
---|
155 | return String.format("%.2fs", ss); |
---|
156 | |
---|
157 | /** |
---|
158 | * StringBuilder sb = new StringBuilder(); String sep = ""; |
---|
159 | * |
---|
160 | * if (dd > 0) { String unit = (dd > 1) ? "days" : "day"; |
---|
161 | * sb.append(String.format("%s%d%s", sep, dd, unit)); sep = " "; } |
---|
162 | * |
---|
163 | * if (hh > 0) { String unit = (hh > 1) ? "hrs" : "hr"; |
---|
164 | * sb.append(String.format("%s%d%s", sep, hh, unit)); sep = " "; } |
---|
165 | * |
---|
166 | * if (mm > 0) { String unit = (mm > 1) ? "mins" : "min"; |
---|
167 | * sb.append(String.format("%s%d%s", sep, mm, unit)); sep = " "; } |
---|
168 | * |
---|
169 | * if (ss > 0) { String unit = (ss > 1) ? "secs" : "sec"; |
---|
170 | * sb.append(String.format("%s%.3f%s", sep, ss, unit)); sep = " "; } |
---|
171 | * |
---|
172 | * return sb.toString(); |
---|
173 | */ |
---|
174 | } |
---|
175 | |
---|
176 | private boolean readable() { |
---|
177 | return this.last != -1; |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * Simple tester. |
---|
182 | * |
---|
183 | * @param args |
---|
184 | */ |
---|
185 | public static void main(String[] args) { |
---|
186 | long i = 7; |
---|
187 | |
---|
188 | for (int x = 0; x < 20; ++x, i *= 7) { |
---|
189 | System.out.println(NanoTimer.nanoTimeToString(i)); |
---|
190 | } |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|