package test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.util.concurrent.atomic.AtomicInteger; public class Main { //public static final String TEST_URL = "http://localhost:8080/things/56a320e2a24f4d8cb718050dbdf30464;exec?name=who"; public static final String TEST_URL = "http://localhost:8080/TestGF/TestRequestCount"; public static final int USER_COUNT = 5; public static final int ITERATION_COUNT = 1000; private static AtomicInteger counter = new AtomicInteger(); public static void main(String[] args) throws Exception { int users = USER_COUNT; int iterations = ITERATION_COUNT; if (args.length == 2) { users = Integer.valueOf(args[0]); iterations = Integer.valueOf(args[1]); } System.out.println("users=" + users); System.out.println("iterations=" + iterations); Thread[] threads = new Thread[users]; for (int k = 0; k < threads.length; k++) { threads[k] = startRun(iterations); } for (Thread t : threads) { t.join(iterations * users * 500); } System.out.println("Counter: " + counter); } static Thread startRun(final int loops) { Thread t = new Thread() { public void run() { long t0 = System.currentTimeMillis(); try { for (int i = 0; i < loops; i++) { // URL u = new URL(TEST_URL); // HttpURLConnection connection = (HttpURLConnection) u.openConnection(); // connection.addRequestProperty("Counter", Integer.toString(counter.incrementAndGet())); // connection.getContent(); makeRequest("localhost", 8080, TEST_URL); } } catch(Exception e) { System.out.println(e.getMessage()); } System.out.println("Millis = " + (System.currentTimeMillis() - t0)); } }; t.start(); return t; } public static void makeRequest(String host, int port, String url) throws UnknownHostException, IOException { Socket socket = new Socket(host, port); try { OutputStream out = socket.getOutputStream(); out.write(new String("GET " + url + " HTTP/1.1\r\n").getBytes()); out.write(new String("Host: " + host + "\r\n").getBytes()); out.write(new String("Counter: " + Integer.toString(counter.incrementAndGet()) + "\r\n\r\n").getBytes()); out.flush(); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line = reader.readLine(); if (!"HTTP/1.1 200 OK".equals(line)) { throw new IOException("Error!!!"); } } finally { socket.close(); } } }