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.server.appserver.load;
006:
007: import com.tc.test.server.appserver.deployment.AbstractDeploymentTest;
008: import com.tc.test.server.appserver.deployment.Deployment;
009: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
010: import com.tc.test.server.appserver.deployment.ServerTestSetup;
011: import com.tc.test.server.appserver.deployment.WebApplicationServer;
012: import com.tc.test.server.appserver.load.Node;
013: import com.tc.test.server.util.TcConfigBuilder;
014: import com.tctest.webapp.servlets.CounterServlet;
015:
016: import java.net.URL;
017:
018: import junit.framework.Test;
019:
020: public class MultiNodeLoadTest extends AbstractDeploymentTest {
021: private static final String CONTEXT = "MultiNodeLoadTest";
022: private static final String SERVLET = "CounterServlet";
023:
024: private static final int SESSIONS_PER_NODE = 10;
025: private static final long TEST_DURATION = 4 * 60 * 1000;
026:
027: private Deployment deployment;
028: private TcConfigBuilder configBuilder;
029:
030: public static Test suite() {
031: return new ServerTestSetup(MultiNodeLoadTest.class);
032: }
033:
034: private Deployment makeDeployment() throws Exception {
035: DeploymentBuilder builder = makeDeploymentBuilder(CONTEXT
036: + ".war");
037: builder.addServlet(SERVLET, "/" + SERVLET + "/*",
038: CounterServlet.class, null, false);
039: return builder.makeDeployment();
040: }
041:
042: public void setUp() throws Exception {
043: super .setUp();
044: if (deployment == null) {
045: deployment = makeDeployment();
046: configBuilder = new TcConfigBuilder();
047: configBuilder.addWebApplication(CONTEXT);
048: }
049: }
050:
051: public void testFourNodeLoad() throws Throwable {
052: runFourNodeLoad(true);
053: }
054:
055: protected void runFourNodeLoad(boolean sticky) throws Throwable {
056: assertTimeDirection();
057: runNodes(4, sticky);
058: }
059:
060: private WebApplicationServer createAndStartServer()
061: throws Exception {
062: WebApplicationServer server = makeWebApplicationServer(configBuilder);
063: server.addWarDeployment(deployment, CONTEXT);
064: server.start();
065: return server;
066: }
067:
068: private URL createUrl(int port, String params) throws Exception {
069: URL url = new URL("http://localhost:" + port + "/" + CONTEXT
070: + "/" + SERVLET
071: + (params.length() > 0 ? "?" + params : ""));
072: return url;
073: }
074:
075: private void runNodes(int nodeCount, boolean sticky)
076: throws Throwable {
077: Thread[] nodeRunners = new Thread[nodeCount];
078: Node[] nodes = new Node[nodeCount];
079: WebApplicationServer[] servers = new WebApplicationServer[nodeCount];
080:
081: URL[] allUrls = new URL[nodeCount];
082: for (int i = 0; i < nodeCount; i++) {
083: servers[i] = createAndStartServer();
084: allUrls[i] = createUrl(servers[i].getPort(), "");
085: }
086:
087: URL[] validateUrls = new URL[nodeCount];
088: for (int i = 0; i < nodeCount; i++) {
089: validateUrls[i] = createUrl(servers[i].getPort(),
090: "read=true");
091: }
092:
093: for (int i = 0; i < nodeCount; i++) {
094: if (sticky) {
095: URL validateUrl = createUrl(
096: servers[(i + 1) % nodeCount].getPort(),
097: "read=true");
098: nodes[i] = new Node(allUrls[i], validateUrl,
099: SESSIONS_PER_NODE, TEST_DURATION);
100: } else {
101: nodes[i] = new Node(allUrls, validateUrls,
102: SESSIONS_PER_NODE, TEST_DURATION);
103: }
104:
105: nodeRunners[i] = new Thread(nodes[i], "Runner["
106: + servers[i].getPort() + "]");
107: }
108:
109: for (int i = 0; i < nodeCount; i++) {
110: nodeRunners[i].start();
111: }
112:
113: for (int i = 0; i < nodeCount; i++) {
114: nodeRunners[i].join();
115: }
116:
117: for (int i = 0; i < nodeCount; i++) {
118: nodes[i].checkError();
119: }
120: }
121: }
|