Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util; |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 2 | |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 3 | import java.util.Map.Entry; |
Brian O'Connor | a5e5f83 | 2014-01-15 16:58:14 -0800 | [diff] [blame] | 4 | import java.util.Queue; |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 5 | import java.util.concurrent.ConcurrentHashMap; |
Brian O'Connor | a5e5f83 | 2014-01-15 16:58:14 -0800 | [diff] [blame] | 6 | import java.util.concurrent.ConcurrentLinkedQueue; |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 7 | |
| 8 | import org.slf4j.Logger; |
| 9 | import org.slf4j.LoggerFactory; |
| 10 | |
| 11 | /** |
| 12 | * Class for collecting performance measurements |
| 13 | */ |
| 14 | public class PerformanceMonitor { |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 15 | private final static Logger log = LoggerFactory.getLogger(PerformanceMonitor.class); |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 16 | |
| 17 | // experiment name -> PerformanceMonitor |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 18 | private static final ConcurrentHashMap<String, PerformanceMonitor> perfMons = new ConcurrentHashMap<>(); |
| 19 | |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 20 | public static PerformanceMonitor experiment(String name) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 21 | PerformanceMonitor pm = perfMons.get(name); |
| 22 | if (pm == null) { |
| 23 | pm = new PerformanceMonitor(); |
| 24 | PerformanceMonitor existing = perfMons.putIfAbsent(name, pm); |
| 25 | if (existing != null) { |
| 26 | pm = existing; |
| 27 | } |
| 28 | } |
| 29 | return pm; |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // tag -> Measurements |
| 33 | private final ConcurrentHashMap<String, Queue<Measurement>> map = new ConcurrentHashMap<>(); |
| 34 | private long overhead; |
| 35 | private long experimentStart = Long.MAX_VALUE; |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 36 | private final static double normalization = Math.pow(10, 6); |
| 37 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 38 | /** |
| 39 | * Start a performance measurement, identified by a tag |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 40 | * <p/> |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 41 | * Note: Only a single measurement can use the same tag at a time. |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 42 | * ..... not true anymore. |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 43 | * |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 44 | * @param tag for performance measurement |
| 45 | */ |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 46 | public Measurement startStep(String tag) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 47 | long start = System.nanoTime(); |
| 48 | if (start < experimentStart) { |
| 49 | experimentStart = start; |
| 50 | } |
| 51 | Queue<Measurement> list = map.get(tag); |
| 52 | if (list == null) { |
| 53 | list = new ConcurrentLinkedQueue<Measurement>(); |
| 54 | Queue<Measurement> existing_list = map.putIfAbsent(tag, list); |
| 55 | if (existing_list != null) { |
| 56 | // someone concurrently added, using theirs |
| 57 | list = existing_list; |
| 58 | } |
| 59 | } |
| 60 | Measurement m = new Measurement(); |
| 61 | list.add(m); |
| 62 | m.start(); |
| 63 | overhead += System.nanoTime() - start; |
| 64 | return m; |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 65 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 66 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 67 | /** |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 68 | * Stop a performance measurement. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 69 | * <p/> |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 70 | * You must have already started a measurement with tag. |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 71 | * |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 72 | * @param tag for performance measurement |
| 73 | */ |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 74 | public void stopStep(String tag) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | long time = System.nanoTime(); |
| 76 | Queue<Measurement> list = map.get(tag); |
| 77 | if (list == null || list.size() == 0) { |
| 78 | log.error("Tag {} does not exist", tag); |
| 79 | } |
| 80 | list.peek().stop(time); |
| 81 | if (list.size() > 1) { |
| 82 | log.error("Tag {} has multiple measurements", tag); |
| 83 | } |
| 84 | overhead += System.nanoTime() - time; |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 85 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 86 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 87 | /** |
| 88 | * Clear all performance measurements. |
| 89 | */ |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 90 | public void reset() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | map.clear(); |
| 92 | overhead = 0; |
| 93 | experimentStart = Long.MAX_VALUE; |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 94 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 95 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 96 | /** |
| 97 | * Write all performance measurements to the log |
| 98 | */ |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 99 | public void reportAll() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | String result = "Performance Results: (avg/start/stop/count)\n"; |
| 101 | if (map.size() == 0) { |
| 102 | result += "No Measurements"; |
| 103 | log.error(result); |
| 104 | return; |
| 105 | } |
| 106 | long experimentEnd = -1; |
| 107 | for (Entry<String, Queue<Measurement>> e : map.entrySet()) { |
| 108 | String key = e.getKey(); |
| 109 | Queue<Measurement> list = e.getValue(); |
| 110 | long total = 0, count = 0; |
| 111 | long start = Long.MAX_VALUE, stop = -1; |
| 112 | for (Measurement m : list) { |
| 113 | if (m.stop < 0) { |
| 114 | continue; // measurement has not been stopped |
| 115 | } |
| 116 | // Collect overall start and end times |
| 117 | if (m.start < start) { |
| 118 | start = m.start; |
| 119 | } |
| 120 | if (m.stop > stop) { |
| 121 | stop = m.stop; |
| 122 | if (stop > experimentEnd) { |
| 123 | experimentEnd = stop; |
| 124 | } |
| 125 | } |
| 126 | // Collect statistics for average |
| 127 | total += m.elapsed(); |
| 128 | count++; |
| 129 | } |
| 130 | double avg = (double) total / count; |
| 131 | // Normalize start/stop |
| 132 | start -= experimentStart; |
| 133 | stop -= experimentStart; |
| 134 | result += key + '=' + |
| 135 | (avg / normalization) + '/' + |
| 136 | (start / normalization) + '/' + |
| 137 | (stop / normalization) + '/' + |
| 138 | count + '\n'; |
| 139 | } |
| 140 | double overheadMs = overhead / normalization; |
| 141 | double experimentElapsed = (experimentEnd - experimentStart) / normalization; |
| 142 | result += "TotalTime:" + experimentElapsed + "/Overhead:" + overheadMs; |
| 143 | log.error(result); |
| 144 | // log.error("Performance Results: {} with measurement overhead: {} ms", map, overheadMilli); |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 145 | } |
Pavlin Radoslavov | b5e3178 | 2014-01-12 20:11:46 -0800 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * Write the performance measurement for a tag to the log |
| 149 | * |
| 150 | * @param tag the tag name. |
| 151 | */ |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 152 | public void reportStep(String tag) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 153 | Queue<Measurement> list = map.get(tag); |
| 154 | if (list == null) { |
| 155 | return; //TODO |
| 156 | } |
| 157 | //TODO: fix this; |
| 158 | Measurement m = list.peek(); |
| 159 | if (m != null) { |
| 160 | log.error("Performance Result: tag = {} start = {} stop = {} elapsed = {}", |
| 161 | tag, m.start, m.stop, m.toString()); |
| 162 | } else { |
| 163 | log.error("Performance Result: unknown tag {}", tag); |
| 164 | } |
Pavlin Radoslavov | b5e3178 | 2014-01-12 20:11:46 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 167 | /** |
| 168 | * A single performance measurement |
| 169 | */ |
Brian O'Connor | 2daf7a9 | 2014-01-14 11:26:35 -0800 | [diff] [blame] | 170 | public static class Measurement { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 171 | long start; |
| 172 | long stop = -1; |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 173 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 174 | /** |
| 175 | * Start the measurement |
| 176 | */ |
| 177 | public void start() { |
| 178 | if (start <= 0) { |
| 179 | start = System.nanoTime(); |
| 180 | } |
| 181 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 182 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Stop the measurement |
| 185 | */ |
| 186 | public void stop() { |
| 187 | long now = System.nanoTime(); |
| 188 | stop(now); |
| 189 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 190 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 191 | /** |
| 192 | * Stop the measurement at a specific time |
| 193 | * |
| 194 | * @param time to stop |
| 195 | */ |
| 196 | public void stop(long time) { |
| 197 | if (stop <= 0) { |
| 198 | stop = time; |
| 199 | } |
| 200 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 201 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 202 | /** |
| 203 | * Compute the elapsed time of the measurement in nanoseconds |
| 204 | * |
| 205 | * @return the measurement time in nanoseconds, or -1 if the measurement is stil running. |
| 206 | */ |
| 207 | public long elapsed() { |
| 208 | if (stop <= 0) { |
| 209 | return -1; |
| 210 | } else { |
| 211 | return stop - start; |
| 212 | } |
| 213 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 214 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 215 | /** |
| 216 | * Returns the number of milliseconds for the measurement as a String. |
| 217 | */ |
| 218 | @Override |
| 219 | public String toString() { |
| 220 | double milli = elapsed() / normalization; |
| 221 | double startMs = start / normalization; |
| 222 | double stopMs = stop / normalization; |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 223 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 224 | return milli + "ms/" + startMs + '/' + stopMs; |
| 225 | } |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 226 | } |
Yuta HIGUCHI | a7188f2 | 2014-02-18 12:48:06 -0800 | [diff] [blame] | 227 | |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 228 | @Deprecated |
| 229 | private static final PerformanceMonitor theInstance = new PerformanceMonitor(); |
| 230 | |
| 231 | @Deprecated |
| 232 | public static Measurement start(String tag) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 233 | return theInstance.startStep(tag); |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Deprecated |
| 237 | public static void stop(String tag) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 238 | theInstance.stopStep(tag); |
| 239 | ; |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Deprecated |
| 243 | public static void clear() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 244 | theInstance.reset(); |
| 245 | ; |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | @Deprecated |
| 249 | public static void report() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 250 | theInstance.reportAll(); |
| 251 | ; |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Deprecated |
| 255 | public static void report(String tag) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 256 | theInstance.reportStep(tag); |
Yuta HIGUCHI | 8f28145 | 2014-02-18 16:08:04 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Ray Milkey | 45614c5 | 2014-04-07 16:30:54 -0700 | [diff] [blame] | 259 | public static void main(String[] args) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 260 | // test the measurement overhead |
| 261 | String tag; |
| 262 | for (int i = 0; i < 2; i++) { |
| 263 | tag = "foo foo foo"; |
| 264 | Measurement m; |
| 265 | m = start(tag); |
| 266 | System.out.println(tag); |
| 267 | m.stop(); |
| 268 | m = start(tag); |
| 269 | System.out.println(tag); |
| 270 | m.stop(); |
| 271 | m = start(tag); |
| 272 | System.out.println(tag); |
| 273 | m.stop(); |
| 274 | m = start(tag); |
| 275 | System.out.println(tag); |
| 276 | m.stop(); |
| 277 | tag = "bar"; |
| 278 | start(tag); |
| 279 | stop(tag); |
| 280 | tag = "baz"; |
| 281 | start(tag); |
| 282 | stop(tag); |
| 283 | report(); |
| 284 | clear(); |
| 285 | } |
| 286 | for (int i = 0; i < 100; i++) { |
| 287 | tag = "a"; |
| 288 | start(tag); |
| 289 | stop(tag); |
| 290 | start(tag); |
| 291 | stop(tag); |
brian | 6df8823 | 2014-01-14 17:05:07 -0800 | [diff] [blame] | 292 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 293 | start(tag); |
| 294 | stop(tag); |
| 295 | start(tag); |
| 296 | stop(tag); |
| 297 | start(tag); |
| 298 | stop(tag); |
| 299 | start(tag); |
| 300 | stop(tag); |
| 301 | start(tag); |
| 302 | stop(tag); |
| 303 | start(tag); |
| 304 | stop(tag); |
brian | 6df8823 | 2014-01-14 17:05:07 -0800 | [diff] [blame] | 305 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 306 | tag = "b"; |
| 307 | start(tag); |
| 308 | stop(tag); |
| 309 | tag = "c"; |
| 310 | start(tag); |
| 311 | stop(tag); |
| 312 | report(); |
| 313 | clear(); |
| 314 | } |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 315 | } |
| 316 | } |