blob: 13319e71d9e3ff3635ba2a588943126c0ea62030 [file] [log] [blame]
Brian O'Connor8fdfb062014-01-08 17:47:57 -08001package net.onrc.onos.ofcontroller.flowmanager;
2
3import java.util.Map;
4import java.util.concurrent.ConcurrentHashMap;
5
6import org.slf4j.Logger;
7import org.slf4j.LoggerFactory;
8
9/**
10 * Class for collecting performance measurements
11 */
12public 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 }
85
86 /**
87 * A single performance measurement
88 */
89 static class Measurement {
90 long start;
91 long stop;
92
93 /**
94 * Start the measurement
95 */
96 public void start() {
97 start = System.nanoTime();
98 }
99
100 /**
101 * Stop the measurement
102 */
103 public void stop() {
104 stop = System.nanoTime();
105 }
106
107 /**
108 * Stop the measurement at a specific time
109 * @param time to stop
110 */
111 public void stop(long time){
112 stop = time;
113 }
114
115 /**
116 * Compute the elapsed time of the measurement in nanoseconds
117 *
118 * @return the measurement time in nanoseconds, or -1 if the measurement is stil running.
119 */
120 public long elapsed() {
121 if(stop == 0) {
122 return -1;
123 }
124 else {
125 return stop - start;
126 }
127 }
128
129 /**
130 * Returns the number of milliseconds for the measurement as a String.
131 */
132 public String toString() {
133 double milli = elapsed() / Math.pow(10, 6);
134 return Double.toString(milli) + "ms";
135 }
136 }
137
138 public static void main(String args[]){
139 // test the measurement overhead
140 String tag;
141 for(int i = 0; i < 100; i++){
142 tag = "foo foo foo";
143 start(tag); stop(tag);
144 tag = "bar";
145 start(tag); stop(tag);
146 tag = "baz";
147 start(tag); stop(tag);
148 report();
149 clear();
150 }
151 for(int i = 0; i < 100; i++){
152 tag = "a";
153 start(tag); stop(tag);
154 tag = "b";
155 start(tag); stop(tag);
156 tag = "c";
157 start(tag); stop(tag);
158 report();
159 clear();
160 }
161 }
162}