{
String[] urls = new String[2];
urls[0] = "http://localhost:" + port1 + contextPath + servletMapping;
urls[1] = "http://localhost:" + port2 + contextPath + servletMapping;
ContentExchange exchange1 = new ContentExchange(true);
exchange1.setMethod(HttpMethods.GET);
exchange1.setURL(urls[0] + "?action=init");
client.send(exchange1);
exchange1.waitForDone();
assert exchange1.getResponseStatus() == HttpServletResponse.SC_OK;
String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
assert sessionCookie != null;
System.out.println("sessionCookie = " + sessionCookie);
ExecutorService executor = Executors.newCachedThreadPool();
int clientsCount = 50;
CyclicBarrier barrier = new CyclicBarrier(clientsCount + 1);
int requestsCount = 100;
Worker[] workers = new Worker[clientsCount];
for (int i = 0; i < clientsCount; ++i)
{
workers[i] = new Worker(barrier, requestsCount, sessionCookie, urls);
workers[i].start();
executor.execute(workers[i]);
}
// Wait for all workers to be ready
barrier.await();
long start = System.nanoTime();
// Wait for all workers to be done
barrier.await();
long end = System.nanoTime();
long elapsed = TimeUnit.NANOSECONDS.toMillis(end - start);
System.out.println("elapsed ms: " + elapsed);
for (Worker worker : workers) worker.stop();
executor.shutdownNow();
// Perform one request to get the result
ContentExchange exchange2 = new ContentExchange(true);
exchange2.setMethod(HttpMethods.GET);
exchange2.setURL(urls[0] + "?action=result");
exchange2.getRequestFields().add("Cookie", sessionCookie);
client.send(exchange2);
exchange2.waitForDone();
assert exchange2.getResponseStatus() == HttpServletResponse.SC_OK;
String response = exchange2.getResponseContent();
System.out.println("get = " + response);
assert response.trim().equals(String.valueOf(clientsCount * requestsCount));
}
finally
{