KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > async > impl > StageMonitorImplTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.async.impl;
5
6 import com.tc.async.api.StageMonitor.Analysis;
7 import com.tc.text.StringFormatter;
8 import com.tc.util.concurrent.ThreadUtil;
9
10 import junit.framework.TestCase;
11
12 public class StageMonitorImplTest extends TestCase {
13   public void tests() throws Exception JavaDoc {
14     StageMonitorImpl sm = new StageMonitorImpl("name", new StringFormatter());
15
16     sm.eventBegin(10);
17     ThreadUtil.reallySleep(100);
18     sm.eventBegin(1);
19     sm.eventBegin(0);
20     ThreadUtil.reallySleep(100);
21     sm.eventBegin(10);
22
23     System.out.println(sm.dumpAndFlush());
24
25     sm.eventBegin(10);
26     ThreadUtil.reallySleep(1000);
27     sm.eventBegin(10);
28
29     System.out.println(sm.dumpAndFlush());
30
31     for (int i = 0; i < 10000; i++) {
32       sm.eventBegin(i);
33     }
34
35     System.out.println(sm.dumpAndFlush());
36   }
37
38   public void testAnalyze() throws Exception JavaDoc {
39     StageMonitorImpl sm = new StageMonitorImpl("name", new StringFormatter());
40
41     Analysis an = sm.analyze();
42     assertEquals(new Integer JavaDoc(0), an.getEventCount());
43     //assertEquals(new Double(0), an.getEventsPerSecond());
44
assertEquals(new Integer JavaDoc(-1), an.getMinQueueDepth());
45     assertEquals(new Integer JavaDoc(0), an.getMaxQueueDepth());
46     assertEquals(new Double JavaDoc(-1), an.getAvgQueueDepth());
47
48     sm.eventBegin(10);
49     ThreadUtil.reallySleep(100);
50     sm.eventBegin(20);
51     an = sm.analyze();
52
53     assertEquals(new Integer JavaDoc(2), an.getEventCount());
54     String JavaDoc elapsed = String.valueOf(an.getElapsedTime().longValue());
55     assertTrue(elapsed, an.getElapsedTime().longValue() >= 100);
56     assertTrue(elapsed, an.getElapsedTime().longValue() < 2500);
57     assertEquals(new Double JavaDoc(1000 * an.getEventCount().doubleValue() / an.getElapsedTime().doubleValue()), an
58         .getEventsPerSecond());
59     assertEquals(new Integer JavaDoc(10), an.getMinQueueDepth());
60     assertEquals(new Integer JavaDoc(20), an.getMaxQueueDepth());
61
62   }
63
64 }
65
Popular Tags