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