I'm trying to test a SOCKS proxy that's under load from multiple machines. The outline of my code is something along the lines of
1 and 2 are performed in the same function.
private static void baseline() {
Download withProxy = new Download(socksPort, targetFile);
Download withoutProxy = new Download(true, socksPort, targetFile); //The true argument just indicates not to use the proxy.
try { //Come to think of it, I could just call run() directly here since this part is meant to be done serially.
withProxy.start();
withProxy.join();
withoutProxy.start();
withoutProxy.join();
//Some code for getting the times goes here.
} catch (Exception e) {
System.out.println("Couldn't get baseline.");
e.printStackTrace();
}
}
The download object inherits from Thread. Most of the work is done in the run() method, which looks like this:
public void run() {
try {
URL url = new URL("http://" + targetFile);
URLConnection urconn = null;
if (baseline) {
urconn = url.openConnection(Proxy.NO_PROXY);
} else {
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr);
urconn = url.openConnection(proxy);
}
InputStreamReader isr = new InputStreamReader(urconn.getInputStream());
System.out.println("Thread " + id + " is downloading.");
long startTime = System.currentTimeMillis();
char[] buf = new char[64];
while (isr.read(buf) != -1) {
;
}
long endTime = System.currentTimeMillis();
isr.close();
System.out.println("Thread " + id + " has completed.");
delta = (endTime - startTime);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
The problem is that when I call the baseline function, it always uses the first choice of proxy - if I run the withproxy thread first, the withoutproxy thread will use the proxy. If I run withoutproxy first, withproxy ignores the proxy. The really odd thing is that later on when I try to connect through the proxy with multiple clients, it doesn't matter how the baseline connections worked - if the baseline connection didn't use a proxy, the multiple client connections still do.
What am I missing here?
Thanks
I managed to fix it - For whatever reason, the time between subsequent calls to url.openconnection() makes a difference. Calling Thread.sleep(10000) between each start() works.