A simple start:
package com.acme.stress;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* Generate a load on the Webapp
* @author levieuxbarbu
*
*/
public class StressTest {
public static void main(String[] args) throws ClientProtocolException, IOException {
StressTest stressTest = new StressTest();
stressTest.run();
}
private void run() throws ClientProtocolException, IOException {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpUriRequest request = new HttpGet("http://www.lesjoliesnana.com/");
HttpResponse response = httpclient.execute(request);
System.out.println(response);
}
}
=================================
A more complex example to generate multiple clients hammering the server with requests:
package com.acme.stress;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* Generate a load on the Webapp
* @author vernetto
*
*/
public class StressTest implements Runnable {
public static int NUMBER_OF_VUSERS = 2;
public static int PAUSE_BETWEEN_TESTS_IN_SECONDS = 10;
public static int NUMBER_OF_CYCLES = 2;
public static void main(String[] args) throws ClientProtocolException, IOException {
Thread[] threads = new Thread[NUMBER_OF_VUSERS];
for (int i = 0 ; i < NUMBER_OF_VUSERS; i++) {
StressTest stressTest = new StressTest(Integer.toString(i));
Thread thread = new Thread(stressTest);
threads[i] = thread;
thread.start();
}
}
private String name;
public StressTest (String name) {
this.name = name;
}
public void run() {
// TODO Auto-generated method stub
for (int i = 0 ; i < NUMBER_OF_CYCLES; i++) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpUriRequest request = new HttpGet("http://myserver.com/");
System.out.println("executing request # " + i + " for tester " + name);
HttpResponse response = httpclient.execute(request);
Thread.sleep(PAUSE_BETWEEN_TESTS_IN_SECONDS * 1000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Monday, September 21, 2009
Subscribe to:
Post Comments (Atom)
2 comments:
really helpful example. is there a way redirect the requests through a public proxy so that my Ip doesn't get blocked?
yes I vaguely remember that you have to set a couple of -D properties in your JVM command line... see here
http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html
http.proxyHost and http.proxyPort
Post a Comment