Java 重载测试API
目前,我们希望通过发送并发请求来测试API,以便评估性能,因此我的代码:Java 重载测试API,java,Java,目前,我们希望通过发送并发请求来测试API,以便评估性能,因此我的代码: int concurrency =2000; int loopTimes = 10 ; ExecutorService executorService = Executors.newFixedThreadPool(concurrency); for(int i=0;i<loopTimes;i++){ CountD
int concurrency =2000;
int loopTimes = 10 ;
ExecutorService executorService = Executors.newFixedThreadPool(concurrency);
for(int i=0;i<loopTimes;i++){
CountDownLatch latch = new CountDownLatch(concurrency);
for(int j=0;j<concurrency;j++) {
executorService.submit(new POST_REQUEST(body,latch));
}
latch.await();
System.out.println("Query Complete : "+i);
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.DAYS);
我的实现是否正确,因为从API方面看,我们也看到了垃圾数据?您告诉服务器您正在发送JSON,然后发送一些纯文本。这不是正确的客户端行为。不使用hey、jmeter、Gatting等工具是否有具体原因?
POST_REQUEST
是标准类还是您自己的类?您的“Run”课程名为Run
?我认为这两段代码之间存在脱节。
@Override
public void run() {
try {
URL url = new URL ("api-url:8080");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
String body = "[
{
event_id: 'b5a9e3ab-d796-4de0-8d4b-00e331785340',
}
]"
try(OutputStream os = con.getOutputStream()) {
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
}
latch.countDown();
}
catch (IOException e) {
e.printStackTrace();
}
}