KickJava   Java API By Example, From Geeks To Geeks.

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


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.ISingleton;
7 import com.tctest.spring.integrationtests.framework.AbstractDeploymentTest;
8 import com.tctest.spring.integrationtests.framework.Deployment;
9 import com.tctest.spring.integrationtests.framework.DeploymentBuilder;
10 import com.tctest.spring.integrationtests.framework.WebApplicationServer;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import junit.framework.Assert;
17
18 public class SingletonLoadTest extends AbstractDeploymentTest {
19   private static final String JavaDoc REMOTE_SERVICE_NAME = "Singleton";
20   private static final String JavaDoc BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory.xml";
21   private static final String JavaDoc CONFIG_FILE_FOR_TEST = "/tc-config-files/singleton-tc-config.xml";
22   private static final int NUM_ITERATION = 500;
23
24   private String JavaDoc CONTEXT = "test-singleton";
25
26   private Deployment deployment;
27
28   protected void setUp() throws Exception JavaDoc {
29     super.setUp();
30     if (deployment == null) deployment = makeDeployment();
31   }
32
33   public void testTwoNodeSingletonLoad() throws Exception JavaDoc {
34     runNodes(2);
35   }
36
37   public void testFourNodeSingletonLoad() throws Exception JavaDoc {
38     runNodes(4);
39   }
40
41   public void testEightNodeSingletonLoad() throws Exception JavaDoc {
42     runNodes(8);
43   }
44
45   /*
46    * Time needed to update the Singleton object's counter NUM_ITERATION times is measured. All nodes are utilized to
47    * update the counter in round-robin fashion.
48    */

49   private void runNodes(int nodeCount) throws Exception JavaDoc {
50     List JavaDoc servers = new ArrayList JavaDoc();
51     List JavaDoc singletons = new ArrayList JavaDoc();
52
53     try {
54       for (int i = 0; i < nodeCount; i++) {
55         WebApplicationServer server = makeWebApplicationServer(CONFIG_FILE_FOR_TEST);
56         server.addWarDeployment(deployment, CONTEXT);
57         server.start();
58         servers.add(server);
59         singletons.add(server.getProxy(ISingleton.class, REMOTE_SERVICE_NAME));
60       }
61   
62       // ((WebApplicationServer) servers.get(0)).ping(URL);
63

64       long startTime = System.currentTimeMillis();
65       // round-robin
66
for (int i = 0; i < NUM_ITERATION; i++) {
67         ((ISingleton) singletons.get(i % nodeCount)).incrementCounter();
68       }
69       long endTime = System.currentTimeMillis();
70       long totalTime = (endTime - startTime);
71   
72       // check clustering
73
for (Iterator JavaDoc iter = servers.iterator(); iter.hasNext();) {
74         WebApplicationServer cur = (WebApplicationServer) iter.next();
75         ISingleton isp = (ISingleton) cur.getProxy(ISingleton.class, REMOTE_SERVICE_NAME);
76         Assert.assertEquals(NUM_ITERATION, isp.getCounter());
77       }
78   
79       printData(nodeCount, totalTime);
80
81     } finally {
82       for (Iterator JavaDoc it = servers.iterator(); it.hasNext();) {
83         ((WebApplicationServer) it.next()).stopIgnoringExceptions();
84       }
85     }
86   }
87
88   private Deployment makeDeployment() throws Exception JavaDoc {
89     DeploymentBuilder builder = makeDeploymentBuilder(CONTEXT + ".war");
90     addBeanDefinitions(builder);
91     configureRemoteInterfaces(builder);
92     addClassesAndLibraries(builder);
93     return builder.makeDeployment();
94   }
95
96   private void addBeanDefinitions(DeploymentBuilder builder) {
97     builder.addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
98   }
99
100   private void configureRemoteInterfaces(DeploymentBuilder builder) {
101     builder.addRemoteService(REMOTE_SERVICE_NAME, "singleton", ISingleton.class);
102   }
103
104   private void addClassesAndLibraries(DeploymentBuilder builder) {
105     builder.addDirectoryOrJARContainingClass(ISingleton.class);
106     builder.addDirectoryContainingResource(CONFIG_FILE_FOR_TEST);
107   }
108
109
110   private void printData(int nodeCount, long totalTime) {
111     System.out.println("**%% TERRACOTTA TEST STATISTICS %%**: nodes=" + nodeCount + " iteration=" + NUM_ITERATION
112                        + " time=" + totalTime + " nanoseconds avg=" + (totalTime / NUM_ITERATION)
113                        + " nanoseconds/iteration");
114   }
115 }
116
Popular Tags