1 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 ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 import junit.framework.Assert; 17 18 public class SingletonLoadTest extends AbstractDeploymentTest { 19 private static final String REMOTE_SERVICE_NAME = "Singleton"; 20 private static final String BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory.xml"; 21 private static final String CONFIG_FILE_FOR_TEST = "/tc-config-files/singleton-tc-config.xml"; 22 private static final int NUM_ITERATION = 500; 23 24 private String CONTEXT = "test-singleton"; 25 26 private Deployment deployment; 27 28 protected void setUp() throws Exception { 29 super.setUp(); 30 if (deployment == null) deployment = makeDeployment(); 31 } 32 33 public void testTwoNodeSingletonLoad() throws Exception { 34 runNodes(2); 35 } 36 37 public void testFourNodeSingletonLoad() throws Exception { 38 runNodes(4); 39 } 40 41 public void testEightNodeSingletonLoad() throws Exception { 42 runNodes(8); 43 } 44 45 49 private void runNodes(int nodeCount) throws Exception { 50 List servers = new ArrayList (); 51 List singletons = new ArrayList (); 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 64 long startTime = System.currentTimeMillis(); 65 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 for (Iterator 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 it = servers.iterator(); it.hasNext();) { 83 ((WebApplicationServer) it.next()).stopIgnoringExceptions(); 84 } 85 } 86 } 87 88 private Deployment makeDeployment() throws Exception { 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 |