我正在尝试测试多台机器负载下的 SOCKS 代理。我的代码大纲类似于
- 用一个客户端直接连接服务器,下载测试文件,记录所用时间。
- 通过代理与一个客户端连接,下载测试文件,并记录所用时间。
- 通过代理连接多个客户端,下载测试文件,记录时间。
1 和 2 以相同的功能执行。
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();
}
}
下载对象继承自 Thread。大部分工作在 run() 方法中完成,如下所示:
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);
}
}
问题是,当我调用基线函数时,它总是使用代理的首选——如果我先运行 withproxy 线程,那么 withoutproxy 线程将使用代理。如果我先运行 withoutproxy,withproxy 会忽略代理。真正奇怪的是,后来当我尝试通过代理与多个客户端连接时,基线连接如何工作并不重要——如果基线连接不使用代理,多个客户端连接仍然可以。
我在这里想念什么?
谢谢