KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > integrationtests > load > DistributedEventsLoadTest


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.tctest.spring.integrationtests.load;
5
6 import com.tctest.spring.bean.EventManager;
7 import com.tctest.spring.integrationtests.framework.AbstractDeploymentTest;
8 import com.tctest.spring.integrationtests.framework.Deployment;
9 import com.tctest.spring.integrationtests.framework.TestCallback;
10 import com.tctest.spring.integrationtests.framework.WebApplicationServer;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.Date JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 public class DistributedEventsLoadTest extends AbstractDeploymentTest {
18   private static final boolean DEBUG = false;
19   private static final int NUM_ITERATION = 10;
20
21   private static final String JavaDoc REMOTE_SERVICE_NAME = "EventManager";
22   private static final String JavaDoc BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/distributedevents.xml";
23   private static final String JavaDoc CONFIG_FILE_FOR_TEST = "/tc-config-files/event-tc-config.xml";
24   private static final String JavaDoc CONTEXT = "distributed-events";
25   private static final String JavaDoc BEAN_NAME = "eventManager";
26   
27   private Deployment deployment;
28
29   public DistributedEventsLoadTest() {
30 // disableAllUntil("2007-03-06"); // XXX timebombed
31
}
32   
33   protected void setUp() throws Exception JavaDoc {
34     super.setUp();
35     if (deployment == null) {
36       deployment = makeWAR();
37     }
38   }
39
40   public void testTwoNodeDistributedEventsLoad() throws Throwable JavaDoc {
41     publishDistributedEvents(2);
42   }
43
44   public void testFourNodeDistributedEventsLoad() throws Throwable JavaDoc {
45     publishDistributedEvents(4);
46   }
47
48   public void testEightNodeDistributedEventsLoad() throws Throwable JavaDoc {
49     publishDistributedEvents(8);
50   }
51
52   private void publishDistributedEvents(final int nodeCount) throws Throwable JavaDoc {
53     List JavaDoc servers = new ArrayList JavaDoc();
54     final List JavaDoc eventManagers = new ArrayList JavaDoc();
55
56     try {
57       for (int i = 0; i < nodeCount; i++) {
58         WebApplicationServer server = makeWebApplicationServer(CONFIG_FILE_FOR_TEST);
59         server.addWarDeployment(deployment, CONTEXT);
60         server.start();
61         servers.add(server);
62         EventManager em = (EventManager) server.getProxy(EventManager.class, REMOTE_SERVICE_NAME);
63         em.size();
64         eventManagers.add(em);
65       }
66   
67       debugPrintln("publishDistributedEvents(): num_iteration per node = " + NUM_ITERATION);
68   
69       long startTime = System.currentTimeMillis();
70   
71       debugPrintln("publishDistributedEvents(): startTime = " + startTime);
72   
73       for (int i = 0; i < NUM_ITERATION; i++) {
74         for (int node = 0; node < nodeCount; node++) {
75           debugPrintln("publishDistributedEvents(): node:" + node + " iteration:" + i);
76           ((EventManager) eventManagers.get(node)).publishEvents("foo" + node, "bar" + i, 2);
77         }
78       }
79   
80       waitForSuccess(8 * 60, new TestCallback() {
81         public void check() {
82           for (Iterator JavaDoc iter = eventManagers.iterator(); iter.hasNext();) {
83             EventManager em = (EventManager) iter.next();
84             assertEquals(NUM_ITERATION * nodeCount * 2, em.size());
85           }
86         }
87       });
88   
89       long endTime = 0;
90       for (Iterator JavaDoc iter = eventManagers.iterator(); iter.hasNext();) {
91         EventManager em = (EventManager) iter.next();
92         Date JavaDoc date = em.getLastEventTime();
93   
94         debugPrintln("eventManager = " + em.toString() + " lastEventTime = " + date.getTime());
95   
96         if (date.getTime() > endTime) {
97           endTime = date.getTime();
98         }
99       }
100   
101       long totalTime = endTime - startTime;
102       
103       printData(nodeCount, totalTime);
104     
105     } finally {
106       for (Iterator JavaDoc it = servers.iterator(); it.hasNext();) {
107         ((WebApplicationServer) it.next()).stopIgnoringExceptions();
108       }
109     }
110   }
111
112   private Deployment makeWAR() throws Exception JavaDoc {
113     return makeDeploymentBuilder(CONTEXT + ".war")
114         .addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST)
115         .addRemoteService(REMOTE_SERVICE_NAME, BEAN_NAME, EventManager.class)
116         .addDirectoryOrJARContainingClass(EventManager.class)
117         .addDirectoryContainingResource(CONFIG_FILE_FOR_TEST)
118         .makeDeployment();
119   }
120
121   private void printData(int nodeCount, long totalTime) {
122     System.out.println("**%% TERRACOTTA TEST STATISTICS %%**: nodes=" + nodeCount + " iteration="
123                        + (NUM_ITERATION * nodeCount) + " time=" + totalTime + " nanoseconds avg="
124                        + (totalTime / (NUM_ITERATION * nodeCount)) + " nanoseconds/iteration");
125   }
126
127   private void debugPrintln(String JavaDoc s) {
128     if (DEBUG) {
129       System.out.println("XXXXX " + s);
130     }
131   }
132
133 }
134
Popular Tags