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