在此先感谢您的任何指示或帮助。
基本上我希望异步版本的性能比同步版本好得多,但同步版本的性能相当或更好。
我做错了什么,什么给了?我尝试不使用 Javalin,以防框架中的某些内容产生问题,似乎给出了类似的结果。我确实只用 Netty 尝试过这个(太长而无法发布代码),我也遇到了类似的结果。
我写了以下代码:(javalin-3.12.0 和 jetty-9.4.31.v20200723)
import io.javalin.Javalin;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import java.io.IOException;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class AsyncTest {
static ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(5000);
public static void main(String[] args) {
var jav = Javalin.create();
jav.config.server(() -> new Server(new QueuedThreadPool(5000, 500, 120_000)));
Javalin app = jav.start(8080);
app.get("/async-delay", ctx -> {
var async = ctx.req.startAsync();
scheduledThreadPoolExecutor.schedule(() -> {
try {
ctx.res.getOutputStream().println("ok");
} catch (IOException e) {
e.printStackTrace();
}
async.complete();
}, 100, TimeUnit.MILLISECONDS);
});
app.get("/delay", ctx -> {
Thread.sleep(100);
ctx.result("ok");
});
app.get("/no-delay", ctx -> {
ctx.result("ok");
});
}
}
并得到以下结果:
➜ ~ wrk2 -t16 -c300 -d5s -R3000 http://localhost:8080/delay
Running 5s test @ http://localhost:8080/delay
16 threads and 300 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 331.36ms 138.72ms 626.18ms 57.34%
Req/Sec nan nan 0.00 0.00%
10854 requests in 5.00s, 1.24MB read
Socket errors: connect 53, read 0, write 0, timeout 106
Requests/sec: 2170.40
Transfer/sec: 254.34KB
➜ ~ wrk2 -t16 -c300 -d5s -R3000 http://localhost:8080/async-delay
Running 5s test @ http://localhost:8080/async-delay
16 threads and 300 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 285.84ms 120.75ms 522.50ms 56.27%
Req/Sec nan nan 0.00 0.00%
11060 requests in 6.10s, 1.29MB read
Socket errors: connect 53, read 0, write 0, timeout 124
Requests/sec: 1814.16
Transfer/sec: 216.14KB
➜ ~ wrk2 -t16 -c16 -d5s -R70000 http://localhost:8080/no-delay
Running 5s test @ http://localhost:8080/no-delay
16 threads and 16 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 2.51ms 3.12ms 21.95ms 88.36%
Req/Sec nan nan 0.00 0.00%
349824 requests in 5.00s, 40.03MB read
Requests/sec: 69995.44
Transfer/sec: 8.01MB