001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.spring.integrationtests.load;
006:
007: import com.tc.test.server.appserver.deployment.Deployment;
008: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
009: import com.tc.test.server.appserver.deployment.ServerTestSetup;
010: import com.tc.test.server.appserver.deployment.WebApplicationServer;
011: import com.tctest.spring.bean.ISingleton;
012: import com.tctest.spring.integrationtests.SpringDeploymentTest;
013:
014: import java.util.ArrayList;
015: import java.util.Iterator;
016: import java.util.List;
017:
018: import junit.framework.Assert;
019: import junit.framework.Test;
020:
021: public class SingletonLoadTest extends SpringDeploymentTest {
022: private static final String REMOTE_SERVICE_NAME = "Singleton";
023: private static final String BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory.xml";
024: private static final String CONFIG_FILE_FOR_TEST = "/tc-config-files/singleton-tc-config.xml";
025: private static final int NUM_ITERATION = 500;
026:
027: private String CONTEXT = "test-singleton";
028:
029: private Deployment deployment;
030:
031: public static Test suite() {
032: return new ServerTestSetup(SingletonLoadTest.class);
033: }
034:
035: protected void setUp() throws Exception {
036: super .setUp();
037: if (deployment == null)
038: deployment = makeDeployment();
039: }
040:
041: public void testFourNodeSingletonLoad() throws Exception {
042: runNodes(4);
043: }
044:
045: /*
046: * Time needed to update the Singleton object's counter NUM_ITERATION times is measured. All nodes are utilized to
047: * update the counter in round-robin fashion.
048: */
049: private void runNodes(int nodeCount) throws Exception {
050: List servers = new ArrayList();
051: List singletons = new ArrayList();
052:
053: try {
054: for (int i = 0; i < nodeCount; i++) {
055: WebApplicationServer server = makeWebApplicationServer(CONFIG_FILE_FOR_TEST);
056: server.addWarDeployment(deployment, CONTEXT);
057: server.start();
058: servers.add(server);
059: singletons.add(server.getProxy(ISingleton.class,
060: REMOTE_SERVICE_NAME));
061: }
062:
063: // ((WebApplicationServer) servers.get(0)).ping(URL);
064:
065: long startTime = System.currentTimeMillis();
066: // round-robin
067: for (int i = 0; i < NUM_ITERATION; i++) {
068: ((ISingleton) singletons.get(i % nodeCount))
069: .incrementCounter();
070: }
071: long endTime = System.currentTimeMillis();
072: long totalTime = (endTime - startTime);
073:
074: // check clustering
075: for (Iterator iter = servers.iterator(); iter.hasNext();) {
076: WebApplicationServer cur = (WebApplicationServer) iter
077: .next();
078: ISingleton isp = (ISingleton) cur.getProxy(
079: ISingleton.class, REMOTE_SERVICE_NAME);
080: Assert.assertEquals(NUM_ITERATION, isp.getCounter());
081: }
082:
083: printData(nodeCount, totalTime);
084:
085: } finally {
086: for (Iterator it = servers.iterator(); it.hasNext();) {
087: WebApplicationServer cur = (WebApplicationServer) it
088: .next();
089: ISingleton isp = (ISingleton) cur.getProxy(
090: ISingleton.class, REMOTE_SERVICE_NAME);
091: // reset for subsequent test runs
092: isp.resetCounter();
093: cur.stopIgnoringExceptions();
094: }
095: }
096: }
097:
098: private Deployment makeDeployment() throws Exception {
099: DeploymentBuilder builder = makeDeploymentBuilder(CONTEXT
100: + ".war");
101: addBeanDefinitions(builder);
102: configureRemoteInterfaces(builder);
103: addClassesAndLibraries(builder);
104: return builder.makeDeployment();
105: }
106:
107: private void addBeanDefinitions(DeploymentBuilder builder) {
108: builder.addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
109: }
110:
111: private void configureRemoteInterfaces(DeploymentBuilder builder) {
112: builder.addRemoteService(REMOTE_SERVICE_NAME, "singleton",
113: ISingleton.class);
114: }
115:
116: private void addClassesAndLibraries(DeploymentBuilder builder) {
117: builder.addDirectoryOrJARContainingClass(ISingleton.class);
118: builder.addDirectoryContainingResource(CONFIG_FILE_FOR_TEST);
119: }
120:
121: private void printData(int nodeCount, long totalTime) {
122: System.out
123: .println("**%% TERRACOTTA TEST STATISTICS %%**: nodes="
124: + nodeCount + " iteration=" + NUM_ITERATION
125: + " time=" + totalTime + " nanoseconds avg="
126: + (totalTime / NUM_ITERATION)
127: + " nanoseconds/iteration");
128: }
129: }
|