1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package net.sf.jperfprobe;
25
26 import static org.junit.Assert.*;
27 import org.junit.*;
28 import org.slf4j.*;
29
30 import java.util.*;
31 import java.util.concurrent.*;
32
33
34 public class ProbeManagerTest {
35 final Logger log = LoggerFactory.getLogger(ProbeManagerTest.class);
36
37 private ProbeManager probeManager;
38
39 @Before
40 public void setUp() throws Exception {
41 probeManager = new ProbeManager();
42 probeManager.clear();
43 probeManager.setPresentation(ProbeManager.Presentation.MILLIS);
44 }
45
46 @Test
47 public void testGetInstance() {
48 probeManager.start("Instance");
49 Probe probe = probeManager.getProbeInstance("Instance");
50
51 assertNotNull("probe should not be null", probe);
52
53 Probe probe2 = probeManager.getProbeInstance("Instance");
54
55 assertSame("Probe should be same", probe, probe2);
56 Probe probe3 = probeManager.getProbeInstance("Hjalla");
57 assertNotSame("probe should not be same", probe, probe3);
58 }
59
60 @Test
61 public void testClear() {
62 Probe probe = probeManager.getProbeInstance("Instance");
63 probeManager.clear();
64 Probe probe2 = probeManager.getProbeInstance("Instance");
65 assertNotSame("Should not be same", probe, probe2);
66 }
67
68 @Test
69 public void testPut() {
70 Probe probe = new DefaultProbe("probe");
71 probeManager.put("probe", probe);
72 Probe probe2 = probeManager.getProbeInstance("probe");
73 assertSame("probe should be same", probe, probe2);
74 }
75
76 @Test
77 public void testDisableAll() {
78 Probe ener = probeManager.getProbeInstance("ENER");
79 Probe toer = probeManager.getProbeInstance("TOER");
80 Probe treer = probeManager.getProbeInstance("TREER");
81
82 assertTrue("probe should be enabled", ener.isEnabled());
83 assertTrue("probe should be enabled", toer.isEnabled());
84 assertTrue("probe should be enabled", treer.isEnabled());
85 probeManager.disableAll();
86 assertFalse("probe should be disabled", ener.isEnabled());
87 assertFalse("probe should be disabled", toer.isEnabled());
88 assertFalse("probe should be disabled", treer.isEnabled());
89 }
90
91 @Test
92 public void testRunning() {
93 Probe p1 = probeManager.start("ENER");
94 Probe probe = probeManager.getProbeInstance("ENER");
95 assertTrue("Probe should have status running", probe.isRunning());
96 probeManager.stop("ENER");
97 assertFalse("Probe should have status not running", probe.isRunning());
98 Probe probe2 = probeManager.getProbeInstance("testRunning");
99 assertFalse("Probe should have status not running", probe2.isRunning());
100 }
101
102 @Test
103 public void testEnable() {
104 Probe p1 = probeManager.getProbeInstance("p1");
105 assertTrue("DefaultProbe p1 should be default enabled", p1.isEnabled());
106 p1.disable();
107 assertFalse("DefaultProbe p1 should be disabled", p1.isEnabled());
108 p1.enable();
109 assertTrue("DefaultProbe p1 should be enabled", p1.isEnabled());
110 Probe p2 = probeManager.getProbeInstance("p2");
111 probeManager.disableAll();
112 assertFalse("DefaultProbe p1 should be disabled", p1.isEnabled());
113 assertFalse("DefaultProbe p2 should be disabled", p1.isEnabled());
114 probeManager.enableAll();
115 assertTrue("DefaultProbe p1 should be enabled", p1.isEnabled());
116 assertTrue("DefaultProbe p2 should be enabled", p1.isEnabled());
117 }
118
119 @Test
120 public void testNames() {
121 probeManager.start("EN");
122 probeManager.start("TO");
123 probeManager.start("TRE");
124 probeManager.stop("EN");
125 probeManager.stop("TO");
126 probeManager.stop("TRE");
127
128 Set<String> set = new HashSet<String>();
129 set.add("EN");
130 set.add("TO");
131 set.add("TRE");
132
133 for (String name : probeManager.getNames()) {
134 assertTrue("wrong name", set.contains(name));
135 }
136 }
137
138 public void testAddSamplesFromProbe() {
139 MockTime mt = new MockTime();
140
141 DefaultProbe p = new DefaultProbe("BALUBA", mt);
142 mt.setTime(0);
143 p.start();
144 mt.setTime(10);
145 p.stop();
146 probeManager.addSampleFromProbe(p);
147 Result result = probeManager.getResult("BALUBA");
148 assertEquals("wrong average", 10.0, result.getAverage(), 0.0);
149 assertEquals("wrong max", 10.0, result.getMax(), 0.0);
150 assertEquals("wrong min", 10.0, result.getMin(), 0.0);
151 }
152
153 @Test
154 public void testStartStopInDifferentThreadsMultiP() throws Exception {
155 probeManager.setPresentation(ProbeManager.Presentation.NANOS);
156 final CountDownLatch doneSignal = new CountDownLatch(2);
157 final CountDownLatch synchLatch = new CountDownLatch(1);
158 final MockTime mt = new MockTime();
159 probeManager.setTime(mt);
160
161 final Holder h = new Holder();
162
163 Thread t1 = new Thread() {
164 public void run() {
165 mt.setTime(10);
166 h.probe = probeManager.start("HOHO");
167 doneSignal.countDown();
168 synchLatch.countDown();
169 System.out.println("balla:" + Thread.currentThread().getName());
170 }
171 };
172
173 Thread t2 = new Thread() {
174 public void run() {
175 try {
176 synchLatch.await();
177 mt.setTime(24);
178 h.probe.stop();
179 probeManager.addSampleFromProbe(h.probe);
180
181 doneSignal.countDown();
182 System.out.println("hjalla:" + Thread.currentThread().getName());
183 } catch (InterruptedException ie) {
184
185 }
186 }
187
188 };
189
190 t1.start();
191 t2.start();
192
193 doneSignal.await();
194 System.out.println(probeManager.toString("HOHO"));
195 assertEquals("wrong # samples", 1, probeManager.getResult("HOHO").getNSamples());
196 assertEquals("wrong max time", 14.0, probeManager.getResult("HOHO").getMax(), 0.0);
197 assertEquals("wrong min time", 14.0, probeManager.getResult("HOHO").getMin(), 0.0);
198 assertEquals("wrong avreage time", 14.0, probeManager.getResult("HOHO").getAverage(), 0.0);
199 }
200
201 @Test
202 public void testStartStopInDifferentThreads() throws Exception {
203 probeManager.setPresentation(ProbeManager.Presentation.NANOS);
204 final CountDownLatch doneSignal = new CountDownLatch(2);
205 final CountDownLatch synchLatch = new CountDownLatch(1);
206 final MockTime mt = new MockTime();
207 probeManager.setTime(mt);
208
209 Thread t1 = new Thread() {
210 public void run() {
211 mt.setTime(10);
212 probeManager.startSingle("HOHO");
213 System.out.println("t1:" + Thread.currentThread().getName());
214 synchLatch.countDown();
215 doneSignal.countDown();
216 }
217 };
218
219 Thread t2 = new Thread() {
220 public void run() {
221 try {
222 synchLatch.await();
223 mt.setTime(24);
224 probeManager.stop("HOHO");
225 System.out.println("t2:" + Thread.currentThread().getName());
226 doneSignal.countDown();
227 } catch (InterruptedException ie) {
228
229 }
230 }
231
232 };
233
234 t1.start();
235 Thread.sleep(100);
236 t2.start();
237
238 doneSignal.await();
239 System.out.println(probeManager.toString("HOHO"));
240 Result res = probeManager.getResult("HOHO");
241 probeManager.getProbeInstance("HOHO");
242 assertEquals("wrong # samples", 1, res.getNSamples());
243 assertEquals("wrong max time", 14.0, res.getMax(), 0.0);
244 assertEquals("wrong min time", 14.0, res.getMin(), 0.0);
245 assertEquals("wrong avreage time", 14.0, res.getAverage(), 0.0);
246 }
247
248 @Test
249 public void testPresentation() {
250 probeManager.setPresentation(ProbeManager.Presentation.NANOS);
251 MockTime mt = new MockTime();
252 probeManager.setTime(mt);
253 mt.setTime(10);
254 probeManager.start("HJALLABALLA");
255 mt.setTime(22);
256 probeManager.stop("HJALLABALLA");
257
258 System.out.println(probeManager.toString("HJALLABALLA"));
259 System.out.println(probeManager.getResult("HJALLABALLA"));
260 probeManager.setPresentation(ProbeManager.Presentation.MICROS);
261 System.out.println(probeManager.toString("HJALLABALLA"));
262 }
263
264 @Test
265 public void testManyProbes() {
266 Random random = new Random();
267
268 int maxProbe = 100;
269 String probeName;
270 long startDur = 0L;
271 for (int i = 0; i < maxProbe; i++) {
272 probeName = "" + random.nextLong();
273 probeManager.start(probeName);
274 probeManager.stop(probeName);
275 probeManager.start(probeName);
276 probeManager.stop(probeName);
277 probeManager.start(probeName);
278 probeManager.stop(probeName);
279 }
280
281 probeManager.clear();
282
283 maxProbe = 1000;
284 startDur = 0L;
285 long stopDur = 0L;
286 for (int i = 0; i < maxProbe; i++) {
287 probeName = "" + random.nextLong();
288 long st = System.nanoTime();
289 probeManager.start(probeName);
290 startDur = startDur + System.nanoTime() - st;
291 st = System.nanoTime();
292 probeManager.stop(probeName);
293 stopDur = stopDur + System.nanoTime() - st;
294 }
295 System.out.println("avg start time new probe:" + startDur / maxProbe);
296 System.out.println("avg stop time new probe:" + stopDur / maxProbe);
297
298 probeManager.clear();
299
300
301 startDur = 0L;
302 stopDur = 0L;
303 probeName = "per";
304 for (int i = 0; i < maxProbe; i++) {
305 long st = System.nanoTime();
306 probeManager.start(probeName);
307 startDur = startDur + System.nanoTime() - st;
308 st = System.nanoTime();
309 probeManager.stop(probeName);
310 stopDur = stopDur + System.nanoTime() - st;
311 }
312 System.out.println("avg start time existing probe:" + startDur / maxProbe);
313 System.out.println("avg stop time existing probe:" + stopDur / maxProbe);
314
315 probeManager.clear();
316
317
318 startDur = 0L;
319 stopDur = 0L;
320 probeName = "perOle";
321 for (int i = 0; i < maxProbe; i++) {
322 long st = System.nanoTime();
323 probeManager.startSingle(probeName);
324 startDur = startDur + System.nanoTime() - st;
325 st = System.nanoTime();
326 probeManager.stop(probeName);
327 stopDur = stopDur + System.nanoTime() - st;
328 }
329 System.out.println("avg start time existing single probe:" + startDur / maxProbe);
330 System.out.println("avg stop time existing single probe:" + stopDur / maxProbe);
331
332 probeManager.clear();
333
334
335 startDur = 0L;
336 stopDur = 0L;
337 long addSampleDur = 0L;
338 probeName = "ole";
339 for (int i = 0; i < maxProbe; i++) {
340 long st = System.nanoTime();
341 Probe p = probeManager.start(probeName);
342 startDur = startDur + System.nanoTime() - st;
343 st = System.nanoTime();
344 p.stop();
345 stopDur = stopDur + System.nanoTime() - st;
346 st = System.nanoTime();
347 probeManager.addSampleFromProbe(p);
348 addSampleDur = addSampleDur + System.nanoTime() - st;
349
350 }
351 System.out.println("avg start time existing probe:" + startDur / maxProbe);
352 System.out.println("avg stop time existing probe no lookup:" + stopDur / maxProbe);
353 System.out.println("avg addSample existing probe no lookup:" + addSampleDur / maxProbe);
354
355
356 }
357
358 static class Holder {
359 Probe probe;
360 }
361 }