PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/jetty/contrib/terracotta/src/test/java/org/mortbay/terracotta/servlet/LightLoadTest.java

https://github.com/derickbailey/qedserver
Java | 235 lines | 187 code | 25 blank | 23 comment | 13 complexity | 36d3251b7ebda1f47a7f2097fe73ab2a MD5 | raw file
  1. // ========================================================================
  2. // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
  3. // ------------------------------------------------------------------------
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ========================================================================
  14. package org.mortbay.terracotta.servlet;
  15. import java.io.IOException;
  16. import java.io.PrintWriter;
  17. import java.util.Random;
  18. import java.util.concurrent.CyclicBarrier;
  19. import java.util.concurrent.ExecutorService;
  20. import java.util.concurrent.Executors;
  21. import java.util.concurrent.TimeUnit;
  22. import javax.servlet.ServletException;
  23. import javax.servlet.http.HttpServlet;
  24. import javax.servlet.http.HttpServletRequest;
  25. import javax.servlet.http.HttpServletResponse;
  26. import javax.servlet.http.HttpSession;
  27. import org.mortbay.jetty.HttpMethods;
  28. import org.mortbay.jetty.client.ContentExchange;
  29. import org.mortbay.jetty.client.HttpClient;
  30. import org.testng.annotations.Test;
  31. /**
  32. * @version $Revision: 1645 $ $Date: 2009-09-15 20:31:07 +1000 (Tue, 15 Sep 2009) $
  33. */
  34. public class LightLoadTest
  35. {
  36. private boolean _stress = Boolean.getBoolean( "STRESS" );
  37. @Test
  38. public void testLightLoad()
  39. throws Exception
  40. {
  41. if ( _stress )
  42. {
  43. Random random = new Random( System.nanoTime() );
  44. String contextPath = "";
  45. String servletMapping = "/server";
  46. int port1 = random.nextInt( 50000 ) + 10000;
  47. TerracottaJettyServer server1 = new TerracottaJettyServer( port1 );
  48. server1.addContext( contextPath ).addServlet( TestServlet.class, servletMapping );
  49. server1.start();
  50. try
  51. {
  52. int port2 = random.nextInt( 50000 ) + 10000;
  53. TerracottaJettyServer server2 = new TerracottaJettyServer( port2 );
  54. server2.addContext( contextPath ).addServlet( TestServlet.class, servletMapping );
  55. server2.start();
  56. try
  57. {
  58. HttpClient client = new HttpClient();
  59. client.setConnectorType( HttpClient.CONNECTOR_SOCKET );
  60. client.start();
  61. try
  62. {
  63. String[] urls = new String[2];
  64. urls[0] = "http://localhost:" + port1 + contextPath + servletMapping;
  65. urls[1] = "http://localhost:" + port2 + contextPath + servletMapping;
  66. ContentExchange exchange1 = new ContentExchange( true );
  67. exchange1.setMethod( HttpMethods.GET );
  68. exchange1.setURL( urls[0] + "?action=init" );
  69. client.send( exchange1 );
  70. exchange1.waitForDone();
  71. assert exchange1.getResponseStatus() == HttpServletResponse.SC_OK;
  72. String sessionCookie = exchange1.getResponseFields().getStringField( "Set-Cookie" );
  73. assert sessionCookie != null;
  74. // Mangle the cookie, replacing Path with $Path, etc.
  75. sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
  76. ExecutorService executor = Executors.newCachedThreadPool();
  77. int clientsCount = 50;
  78. CyclicBarrier barrier = new CyclicBarrier( clientsCount + 1 );
  79. int requestsCount = 100;
  80. Worker[] workers = new Worker[clientsCount];
  81. for ( int i = 0; i < clientsCount; ++i )
  82. {
  83. workers[i] = new Worker( barrier, requestsCount, sessionCookie, urls );
  84. workers[i].start();
  85. executor.execute( workers[i] );
  86. }
  87. // Wait for all workers to be ready
  88. barrier.await();
  89. long start = System.nanoTime();
  90. // Wait for all workers to be done
  91. barrier.await();
  92. long end = System.nanoTime();
  93. long elapsed = TimeUnit.NANOSECONDS.toMillis( end - start );
  94. System.out.println( "elapsed ms: " + elapsed );
  95. for ( Worker worker : workers )
  96. worker.stop();
  97. executor.shutdownNow();
  98. // Perform one request to get the result
  99. ContentExchange exchange2 = new ContentExchange( true );
  100. exchange2.setMethod( HttpMethods.GET );
  101. exchange2.setURL( urls[0] + "?action=result" );
  102. exchange2.getRequestFields().add( "Cookie", sessionCookie );
  103. client.send( exchange2 );
  104. exchange2.waitForDone();
  105. assert exchange2.getResponseStatus() == HttpServletResponse.SC_OK;
  106. String response = exchange2.getResponseContent();
  107. System.out.println( "get = " + response );
  108. assert response.trim().equals( String.valueOf( clientsCount * requestsCount ) );
  109. }
  110. finally
  111. {
  112. client.stop();
  113. }
  114. }
  115. finally
  116. {
  117. server2.stop();
  118. }
  119. }
  120. finally
  121. {
  122. server1.stop();
  123. }
  124. }
  125. }
  126. public static class Worker
  127. implements Runnable
  128. {
  129. private final HttpClient client;
  130. private final CyclicBarrier barrier;
  131. private final int requestsCount;
  132. private final String sessionCookie;
  133. private final String[] urls;
  134. public Worker( CyclicBarrier barrier, int requestsCount, String sessionCookie, String[] urls )
  135. {
  136. this.client = new HttpClient();
  137. this.client.setConnectorType( HttpClient.CONNECTOR_SOCKET );
  138. this.barrier = barrier;
  139. this.requestsCount = requestsCount;
  140. this.sessionCookie = sessionCookie;
  141. this.urls = urls;
  142. }
  143. public void start()
  144. throws Exception
  145. {
  146. client.start();
  147. }
  148. public void stop()
  149. throws Exception
  150. {
  151. client.stop();
  152. }
  153. public void run()
  154. {
  155. try
  156. {
  157. // Wait for all workers to be ready
  158. barrier.await();
  159. Random random = new Random( System.nanoTime() );
  160. for ( int i = 0; i < requestsCount; ++i )
  161. {
  162. int urlIndex = random.nextInt( urls.length );
  163. ContentExchange exchange = new ContentExchange( true );
  164. exchange.setMethod( HttpMethods.GET );
  165. exchange.setURL( urls[urlIndex] + "?action=increment" );
  166. exchange.getRequestFields().add( "Cookie", sessionCookie );
  167. client.send( exchange );
  168. exchange.waitForDone();
  169. assert exchange.getResponseStatus() == HttpServletResponse.SC_OK;
  170. }
  171. // Wait for all workers to be done
  172. barrier.await();
  173. }
  174. catch ( Exception x )
  175. {
  176. throw new RuntimeException( x );
  177. }
  178. }
  179. }
  180. public static class TestServlet
  181. extends HttpServlet
  182. {
  183. @Override
  184. protected void doGet( HttpServletRequest request, HttpServletResponse response )
  185. throws ServletException, IOException
  186. {
  187. String action = request.getParameter( "action" );
  188. if ( "init".equals( action ) )
  189. {
  190. HttpSession session = request.getSession( true );
  191. session.setAttribute( "value", 0 );
  192. }
  193. else if ( "increment".equals( action ) )
  194. {
  195. // Without synchronization, because it is taken care by Jetty/Terracotta
  196. HttpSession session = request.getSession( false );
  197. int value = (Integer) session.getAttribute( "value" );
  198. session.setAttribute( "value", value + 1 );
  199. }
  200. else if ( "result".equals( action ) )
  201. {
  202. HttpSession session = request.getSession( false );
  203. int value = (Integer) session.getAttribute( "value" );
  204. PrintWriter writer = response.getWriter();
  205. writer.println( value );
  206. writer.flush();
  207. }
  208. }
  209. }
  210. }