Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowmanager; |
| 2 | |
| 3 | import java.util.Map; |
| 4 | import java.util.concurrent.ConcurrentHashMap; |
| 5 | |
| 6 | import org.slf4j.Logger; |
| 7 | import org.slf4j.LoggerFactory; |
| 8 | |
| 9 | /** |
| 10 | * Class for collecting performance measurements |
| 11 | */ |
| 12 | public class PerformanceMonitor { |
| 13 | private final static Map<String, Measurement> map = new ConcurrentHashMap<String, Measurement>();; |
| 14 | private final static Logger log = LoggerFactory.getLogger(PerformanceMonitor.class); |
| 15 | private static long overhead; |
| 16 | |
| 17 | /** |
| 18 | * Start a performance measurement, identified by a tag |
| 19 | * |
| 20 | * Note: Only a single measurement can use the same tag at a time. |
| 21 | * |
| 22 | * @param tag for performance measurement |
| 23 | */ |
| 24 | public static void start(String tag) { |
| 25 | long start = System.nanoTime(); |
| 26 | Measurement m = new Measurement(); |
| 27 | if(map.put(tag, m) != null) { |
| 28 | // if there was a previous entry, we have just overwritten it |
| 29 | log.error("Tag {} already exists", tag); |
| 30 | } |
| 31 | m.start(); |
| 32 | overhead += System.nanoTime() - start; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Stop a performance measurement. |
| 37 | * |
| 38 | * You must have already started a measurement with tag. |
| 39 | * |
| 40 | * @param tag for performance measurement |
| 41 | */ |
| 42 | public static void stop(String tag) { |
| 43 | long time = System.nanoTime(); |
| 44 | Measurement m = map.get(tag); |
| 45 | if(m == null) { |
| 46 | log.error("Tag {} does not exist", tag); |
| 47 | } |
| 48 | else { |
| 49 | map.get(tag).stop(time); |
| 50 | } |
| 51 | overhead += System.nanoTime() - time; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Find a measurement, identified by tag, and return the result |
| 56 | * |
| 57 | * @param tag for performance measurement |
| 58 | * @return the time in nanoseconds |
| 59 | */ |
| 60 | public static long result(String tag) { |
| 61 | Measurement m = map.get(tag); |
| 62 | if(m != null) { |
| 63 | return m.elapsed(); |
| 64 | } |
| 65 | else { |
| 66 | return -1; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Clear all performance measurements. |
| 72 | */ |
| 73 | public static void clear() { |
| 74 | map.clear(); |
| 75 | overhead = 0; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Write all performance measurements to the log |
| 80 | */ |
| 81 | public static void report() { |
| 82 | double overheadMilli = overhead / Math.pow(10, 6); |
| 83 | log.error("Performance Results: {} with measurement overhead: {} ms", map, overheadMilli); |
| 84 | } |
Pavlin Radoslavov | b5e3178 | 2014-01-12 20:11:46 -0800 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Write the performance measurement for a tag to the log |
| 88 | * |
| 89 | * @param tag the tag name. |
| 90 | */ |
| 91 | public static void report(String tag) { |
| 92 | Measurement m = map.get(tag); |
| 93 | if (m != null) { |
| 94 | log.error("Performance Results: tag = {} start = {} stop = {} elapsed = {}", |
| 95 | tag, m.start, m.stop, m.toString()); |
| 96 | } else { |
| 97 | log.error("Performance Results: unknown tag {}", tag); |
| 98 | } |
| 99 | } |
| 100 | |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 101 | /** |
| 102 | * A single performance measurement |
| 103 | */ |
| 104 | static class Measurement { |
| 105 | long start; |
| 106 | long stop; |
| 107 | |
| 108 | /** |
| 109 | * Start the measurement |
| 110 | */ |
| 111 | public void start() { |
| 112 | start = System.nanoTime(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Stop the measurement |
| 117 | */ |
| 118 | public void stop() { |
| 119 | stop = System.nanoTime(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Stop the measurement at a specific time |
| 124 | * @param time to stop |
| 125 | */ |
| 126 | public void stop(long time){ |
| 127 | stop = time; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Compute the elapsed time of the measurement in nanoseconds |
| 132 | * |
| 133 | * @return the measurement time in nanoseconds, or -1 if the measurement is stil running. |
| 134 | */ |
| 135 | public long elapsed() { |
| 136 | if(stop == 0) { |
| 137 | return -1; |
| 138 | } |
| 139 | else { |
| 140 | return stop - start; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns the number of milliseconds for the measurement as a String. |
| 146 | */ |
| 147 | public String toString() { |
| 148 | double milli = elapsed() / Math.pow(10, 6); |
Pavlin Radoslavov | 8022fd6 | 2014-01-12 20:25:00 -0800 | [diff] [blame] | 149 | return Double.toString(milli) + " ms"; |
Brian O'Connor | 8fdfb06 | 2014-01-08 17:47:57 -0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | public static void main(String args[]){ |
| 154 | // test the measurement overhead |
| 155 | String tag; |
| 156 | for(int i = 0; i < 100; i++){ |
| 157 | tag = "foo foo foo"; |
| 158 | start(tag); stop(tag); |
| 159 | tag = "bar"; |
| 160 | start(tag); stop(tag); |
| 161 | tag = "baz"; |
| 162 | start(tag); stop(tag); |
| 163 | report(); |
| 164 | clear(); |
| 165 | } |
| 166 | for(int i = 0; i < 100; i++){ |
| 167 | tag = "a"; |
| 168 | start(tag); stop(tag); |
| 169 | tag = "b"; |
| 170 | start(tag); stop(tag); |
| 171 | tag = "c"; |
| 172 | start(tag); stop(tag); |
| 173 | report(); |
| 174 | clear(); |
| 175 | } |
| 176 | } |
| 177 | } |