0
我有一个应用程序,我正通过HTTP请求向服务器发出多个请求。我正在试图标记回应。我不知道在速度方面会发生什么。 我已经写了这个示例程序来测试相同的..我应该期望这个程序的速度是多少?我每秒获得4000个请求,我觉得它少了。请帮助。http服务器速度
了HTTPClient:
package my.service;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
public class TestClient {
public static void main(String[] args) throws Exception {
Date date = new Date();
for (int i = 0; i < 100000; i++) {
URL url = new URL("http://192.168.5.116:9999/testSpeed");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
InputStream stream = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1];
while (stream.read(b) != -1) {
bos.write(b);
}
byte[] byteArray = bos.toByteArray();
String response = new String(byteArray);
if (Integer.parseInt(response) % 1000 == 0) {
System.out.println("Response from server is --> " + response);
}
}
Date d1 = new Date();
System.out.println(d1.getTime() - date.getTime());
}
}
的httpserver:所有的
package my.service;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class TestServer {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(9999), 0);
ExecutorService executorService = Executors.newFixedThreadPool(10);
server.setExecutor(executorService);
server.createContext("/testSpeed", getHandler());
server.start();
}
private static HttpHandler getHandler() {
return new TestHandler();
}
private static class TestHandler implements HttpHandler {
int i = 0;
@Override
public void handle(HttpExchange request) throws IOException {
i++;
if (i % 1000 == 0) {
System.out.println("Number of requests received --> " + i);
}
String response = i + "";
request.sendResponseHeaders(200, response.getBytes().length);
OutputStream os = request.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
错误的方法。像这样的基本测试人员会遇到一些缺陷,例如未优化的线程池管理以及对实际业务逻辑的错误模拟。相反,您需要询问应用程序需要处理什么类型的负载,并确定您的应用程序是否符合该要求......如果不是,则需要在什么时间采取措施以及如何解决缺陷。 – PaulProgrammer 2014-11-04 17:13:05
优化如?业务逻辑非常简单,我不认为它是一个问题。如何优化线程池? – LPD 2014-11-04 17:16:23
我不知道。这个例子太简单了,无法提供任何有价值的见解。如果您需要咨询服务,请通过其他渠道与我联系。 – PaulProgrammer 2014-11-04 17:59:34