View Javadoc

1   /* ==========================================
2    * JperfProbe : Java Performance Probes
3    * ==========================================
4    *
5    * Project Info:  http://jperfprobe.sourceforge.net/
6    * Project Lead:  Tor-Erik Larsen (http://sourceforge.net/users/uptime62)
7    *
8    * (C) Copyright 2005, by Tor-Erik Larsen and Contributors.
9    *
10   * This library is free software; you can redistribute it and/or modify it
11   * under the terms of the GNU Lesser General Public License as published by
12   * the Free Software Foundation; either version 2.1 of the License, or
13   * (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful, but
16   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17   * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18   * License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public License
21   * along with this library; if not, write to the Free Software Foundation, Inc.,
22   * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
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 ProbeManagerImplTest {
35      final Logger log = LoggerFactory.getLogger(ProbeManagerImplTest.class);
36  
37      private ProbeManager probeManager;
38      private Random random = new Random();
39  
40      @Before
41      public void setUp() throws Exception {
42          probeManager = new ProbeManagerImpl();
43          probeManager.clear();
44          probeManager.setPresentation(ProbeManagerImpl.Presentation.MILLIS);
45      }
46  
47      @Test
48      public void testGetInstance() {
49          probeManager.start("Instance");
50          Probe probe = probeManager.getProbeInstance("Instance");
51  
52          assertNotNull("probe should not be null", probe);
53  
54          Probe probe2 = probeManager.getProbeInstance("Instance");
55  
56          assertSame("Probe should be same", probe, probe2);
57          Probe probe3 = probeManager.getProbeInstance("Hjalla");
58          assertNotSame("probe should not be same", probe, probe3);
59      }
60  
61      @Test
62      public void testClear() {
63          Probe probe = probeManager.getProbeInstance("Instance");
64          probeManager.clear();
65          Probe probe2 = probeManager.getProbeInstance("Instance");
66          assertNotSame("Should not be same", probe, probe2);
67      }
68  
69      @Test
70      public void testPut() {
71          Probe probe = new DefaultProbe("probe");
72          probeManager.put("probe", probe);
73          Probe probe2 = probeManager.getProbeInstance("probe");
74          assertSame("probe should be same", probe, probe2);
75      }
76  
77      @Test
78      public void testDisableAll() {
79          Probe ener = probeManager.getProbeInstance("ENER");
80          Probe toer = probeManager.getProbeInstance("TOER");
81          Probe treer = probeManager.getProbeInstance("TREER");
82  
83          assertTrue("probe should be enabled", ener.isEnabled());
84          assertTrue("probe should be enabled", toer.isEnabled());
85          assertTrue("probe should be enabled", treer.isEnabled());
86          probeManager.disable();
87          assertFalse("probe should be disabled", ener.isEnabled());
88          assertFalse("probe should be disabled", toer.isEnabled());
89          assertFalse("probe should be disabled", treer.isEnabled());
90      }
91  
92      @Test
93      public void testRunning() {
94          Probe p1 = probeManager.start("ENER");
95          Probe probe = probeManager.getProbeInstance("ENER");
96          assertTrue("Probe should have status running", probe.isRunning());
97          probeManager.stop("ENER");
98          assertFalse("Probe should have status not running", probe.isRunning());
99          Probe probe2 = probeManager.getProbeInstance("testRunning");
100         assertFalse("Probe should have status not running", probe2.isRunning());
101     }
102 
103     @Test
104     public void testEnable() {
105         Probe p1 = probeManager.getProbeInstance("p1");
106         assertTrue("DefaultProbe p1 should be default enabled", p1.isEnabled());
107         p1.disable();
108         assertFalse("DefaultProbe p1 should be disabled", p1.isEnabled());
109         p1.enable();
110         assertTrue("DefaultProbe p1 should be enabled", p1.isEnabled());
111         Probe p2 = probeManager.getProbeInstance("p2");
112         probeManager.disable();
113         assertFalse("DefaultProbe p1 should be disabled", p1.isEnabled());
114         assertFalse("DefaultProbe p2 should be disabled", p1.isEnabled());
115         probeManager.enable();
116         assertTrue("DefaultProbe p1 should be enabled", p1.isEnabled());
117         assertTrue("DefaultProbe p2 should be enabled", p1.isEnabled());
118     }
119 
120     @Test
121     public void testNames() {
122         probeManager.start("EN");
123         probeManager.start("TO");
124         probeManager.start("TRE");
125         probeManager.stop("EN");
126         probeManager.stop("TO");
127         probeManager.stop("TRE");
128 
129         Set<String> set = new HashSet<String>();
130         set.add("EN");
131         set.add("TO");
132         set.add("TRE");
133 
134         for (String name : probeManager.getNames()) {
135             assertTrue("wrong name", set.contains(name));
136         }
137     }
138 
139     @Test
140     public void testAddSamplesFromProbe() {
141         Probe p = createProbeSample("BALUBA", 10);
142         probeManager.addSampleFromProbe(p);
143         Result result = probeManager.getResult("BALUBA");
144         assertEquals("wrong average", 10.0, result.getAverage(), 0.0);
145         assertEquals("wrong max", 10.0, result.getMax(), 0.0);
146         assertEquals("wrong min", 10.0, result.getMin(), 0.0);
147     }
148 
149 
150     @Test
151     public void testStartStopInDifferentThreadsMultiP() throws Exception {
152         probeManager.setPresentation(ProbeManagerImpl.Presentation.NANOS);
153         final CountDownLatch doneSignal = new CountDownLatch(2);
154         final CountDownLatch synchLatch = new CountDownLatch(1);
155         final MockTime mt = new MockTime();
156         probeManager.setTime(mt);
157 
158         final Holder h = new Holder();
159 
160         Thread t1 = new Thread() {
161             public void run() {
162                 mt.setTime(10);
163                 h.probe = probeManager.start("HOHO");
164                 doneSignal.countDown();
165                 synchLatch.countDown();
166                 System.out.println("balla:" + Thread.currentThread().getName());
167             }
168         };
169 
170         Thread t2 = new Thread() {
171             public void run() {
172                 try {
173                     synchLatch.await();
174                     mt.setTime(24);
175                     h.probe.stop();
176                     probeManager.addSampleFromProbe(h.probe);
177 
178                     doneSignal.countDown();
179                     System.out.println("hjalla:" + Thread.currentThread().getName());
180                 } catch (InterruptedException ie) {
181 
182                 }
183             }
184 
185         };
186 
187         t1.start();
188         t2.start();
189 
190         doneSignal.await();
191         System.out.println(probeManager.toString("HOHO"));
192         assertEquals("wrong # samples", 1, probeManager.getResult("HOHO").getNSamples());
193         assertEquals("wrong max time", 14.0, probeManager.getResult("HOHO").getMax(), 0.0);
194         assertEquals("wrong min time", 14.0, probeManager.getResult("HOHO").getMin(), 0.0);
195         assertEquals("wrong avreage time", 14.0, probeManager.getResult("HOHO").getAverage(), 0.0);
196     }
197 
198     @Test
199     public void testStartStopInDifferentThreads() throws Exception {
200         probeManager.setPresentation(ProbeManagerImpl.Presentation.NANOS);
201         final CountDownLatch doneSignal = new CountDownLatch(2);
202         final CountDownLatch synchLatch = new CountDownLatch(1);
203         final MockTime mt = new MockTime();
204         probeManager.setTime(mt);
205 
206         Thread t1 = new Thread() {
207             public void run() {
208                 mt.setTime(10);
209                 probeManager.startSingle("HOHO");
210                 System.out.println("t1:" + Thread.currentThread().getName());
211                 synchLatch.countDown();
212                 doneSignal.countDown();
213             }
214         };
215 
216         Thread t2 = new Thread() {
217             public void run() {
218                 try {
219                     synchLatch.await();
220                     mt.setTime(24);
221                     probeManager.stop("HOHO");
222                     System.out.println("t2:" + Thread.currentThread().getName());
223                     doneSignal.countDown();
224                 } catch (InterruptedException ie) {
225 
226                 }
227             }
228 
229         };
230 
231         t1.start();
232         Thread.sleep(100);
233         t2.start();
234 
235         doneSignal.await();
236         System.out.println(probeManager.toString("HOHO"));
237         Result res = probeManager.getResult("HOHO");
238         probeManager.getProbeInstance("HOHO");
239         assertEquals("wrong # samples", 1, res.getNSamples());
240         assertEquals("wrong max time", 14.0, res.getMax(), 0.0);
241         assertEquals("wrong min time", 14.0, res.getMin(), 0.0);
242         assertEquals("wrong avreage time", 14.0, res.getAverage(), 0.0);
243     }
244 
245     @Test
246     public void testPresentation() {
247         probeManager.setPresentation(ProbeManagerImpl.Presentation.NANOS);
248         MockTime mt = new MockTime();
249         probeManager.setTime(mt);
250         mt.setTime(10);
251         probeManager.start("HJALLABALLA");
252         mt.setTime(22);
253         probeManager.stop("HJALLABALLA");
254 
255         System.out.println(probeManager.toString("HJALLABALLA"));
256         System.out.println(probeManager.getResult("HJALLABALLA"));
257         probeManager.setPresentation(ProbeManagerImpl.Presentation.MICROS);
258         System.out.println(probeManager.toString("HJALLABALLA"));
259     }
260 
261     @Test
262     public void testAFewProbes() {
263         Random random = new Random();
264 
265         int maxProbe = 100;
266         String probeName;
267         long startDur = 0L;
268         for (int i = 0; i < maxProbe; i++) {
269             probeName = "" + random.nextLong();
270             probeManager.start(probeName);
271             probeManager.stop(probeName);
272             probeManager.start(probeName);
273             probeManager.stop(probeName);
274             probeManager.start(probeName);
275             probeManager.stop(probeName);
276         }
277 
278         probeManager.clear();
279 
280         maxProbe = 1000;
281         startDur = 0L;
282         long stopDur = 0L;
283         for (int i = 0; i < maxProbe; i++) {
284             probeName = "" + random.nextLong();
285             long st = System.nanoTime();
286             probeManager.start(probeName);
287             startDur = startDur + System.nanoTime() - st;
288             st = System.nanoTime();
289             probeManager.stop(probeName);
290             stopDur = stopDur + System.nanoTime() - st;
291         }
292         System.out.println("avg start time new probe:" + startDur / maxProbe);
293         System.out.println("avg stop time new probe:" + stopDur / maxProbe);
294 
295         probeManager.clear();
296         //System.gc();
297 
298         startDur = 0L;
299         stopDur = 0L;
300         probeName = "per";
301         for (int i = 0; i < maxProbe; i++) {
302             long st = System.nanoTime();
303             probeManager.start(probeName);
304             startDur = startDur + System.nanoTime() - st;
305             st = System.nanoTime();
306             probeManager.stop(probeName);
307             stopDur = stopDur + System.nanoTime() - st;
308         }
309         System.out.println("avg start time existing probe:" + startDur / maxProbe);
310         System.out.println("avg stop time existing probe:" + stopDur / maxProbe);
311 
312         probeManager.clear();
313         //System.gc();
314 
315         startDur = 0L;
316         stopDur = 0L;
317         probeName = "perOle";
318         for (int i = 0; i < maxProbe; i++) {
319             long st = System.nanoTime();
320             probeManager.startSingle(probeName);
321             startDur = startDur + System.nanoTime() - st;
322             st = System.nanoTime();
323             probeManager.stop(probeName);
324             stopDur = stopDur + System.nanoTime() - st;
325         }
326         System.out.println("avg start time existing single probe:" + startDur / maxProbe);
327         System.out.println("avg stop time existing single probe:" + stopDur / maxProbe);
328 
329         probeManager.clear();
330         //System.gc();
331 
332         startDur = 0L;
333         stopDur = 0L;
334         long addSampleDur = 0L;
335         probeName = "ole";
336         for (int i = 0; i < maxProbe; i++) {
337             long st = System.nanoTime();
338             Probe p = probeManager.start(probeName);
339             startDur = startDur + System.nanoTime() - st;
340             st = System.nanoTime();
341             p.stop();
342             stopDur = stopDur + System.nanoTime() - st;
343             st = System.nanoTime();
344             probeManager.addSampleFromProbe(p);
345             addSampleDur = addSampleDur + System.nanoTime() - st;
346 
347         }
348         System.out.println("avg start time existing probe:" + startDur / maxProbe);
349         System.out.println("avg stop time existing probe no lookup:" + stopDur / maxProbe);
350         System.out.println("avg addSample existing probe no lookup:" + addSampleDur / maxProbe);
351 
352 
353     }
354 
355     static class Holder {
356         Probe probe;
357     }
358 
359     @Test
360     public void testTotalReal() {
361         probeManager.setPresentation(ProbeManagerImpl.Presentation.MILLIS);
362         long tStamp = System.currentTimeMillis();
363 
364         for (int i = 0; i < 1000; i++) {
365             probeManager.start("nTotal");
366             int nPrimes = SieveBits.countPrimes(10000);
367             probeManager.stop("nTotal");
368         }
369 
370         long elapsed = System.currentTimeMillis() - tStamp;
371 
372         log.info("elapsed (ms)   :" + elapsed);
373         log.info("calculated avg:" + elapsed / 1000.0);
374         log.info("probe   :" + probeManager.getResult("nTotal").getTotal() / 1000000);
375         log.info(probeManager.toString("nTotal"));
376 
377         assertTrue("elapsed timed less than probes total", elapsed > probeManager.getResult("nTotal").getTotal() / 1000000);
378         for (long samp : probeManager.getSamples("nTotal")) {
379             System.out.println(samp);
380         }
381     }
382 
383     @Test
384     public void testConstrWithParams() {
385         Time t = TimeFactory.getTime();
386         ProbeManager pm = new ProbeManagerImpl(0, ProbeManagerImpl.Presentation.MICROS, t);
387 
388         assertEquals("wrong # of first samples to skip ", 0, pm.getFirstSamplesToSkip());
389         assertEquals("presentation should be MICROS", ProbeManagerImpl.Presentation.MICROS, pm.getPresentation());
390         assertSame("wrong time", t, pm.getTime());
391 
392         pm.setFirstSamplesToSkip(23);
393         assertEquals("wrong # of first samples to skip ", 23, pm.getFirstSamplesToSkip());
394     }
395 
396     @Test
397     public void testGetResults() {
398         assertEquals("", 0, probeManager.getResults().size());
399         probeManager.start("balla");
400         probeManager.stop("balla");
401         assertEquals("", 1, probeManager.getResults().size());
402         probeManager.start("balla2");
403         probeManager.stop("balla2");
404         assertEquals("", 2, probeManager.getResults().size());
405     }
406 
407     @Test
408     public void testStop() {
409         probeManager.stop("OOO");
410         ResultTest.assertResult("wrong result", 0, 0, 0, 0, probeManager.getResult("OOO"));
411     }
412 
413     @Test
414     public void testGetSamples() {
415         assertNotNull("samples should not be null", probeManager.getSamples("123"));
416     }
417 
418     @Test
419     public void testOneProbeManyThreads() throws Exception {
420         int max = 205;
421         probeManager.setPresentation(ProbeManagerImpl.Presentation.MILLIS);
422         final CountDownLatch doneSignal = new CountDownLatch(max);
423         final Thread thrds[] = new Thread[max];
424 
425         for (int i = 0; i < max; i++) {
426             thrds[i] = new Thread() {
427                 public void run() {
428                     probeManager.start("MANYMANY");
429                     try {
430                         sleep(10 + random.nextInt(20));
431                     } catch (InterruptedException ie) {
432 
433                     }
434 
435                     probeManager.stop("MANYMANY");
436                     doneSignal.countDown();
437                 }
438             };
439 
440         }
441 
442         // run the threads
443         for (int i = 0; i < max; i++) {
444             thrds[i].start();
445         }
446 
447         // check the result
448         doneSignal.await();
449         log.info("YEAH");
450         log.info(probeManager.toString("MANYMANY"));
451         for (long ll : probeManager.getSamples("MANYMANY")) {
452             log.info("" + ll/1000000);
453 
454         }
455     }
456 
457 
458     private Probe createProbeSample(String pName, int t) {
459         MockTime mt = new MockTime();
460         DefaultProbe p = new DefaultProbe(pName, mt);
461         mt.setTime(0);
462         p.start();
463         mt.setTime(t);
464         p.stop();
465 
466         return p;
467     }
468 
469 
470 }