簡體   English   中英

Jetty Async 未按預期工作,性能比同步差

[英]Jetty Async not working as anticipated, experiencing worse performance than sync

在此先感謝您的任何指示或幫助。

基本上我希望異步版本的性能比同步版本好得多,但同步版本的性能相當或更好。

我做錯了什么,什么給了? 我嘗試不使用 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

由於 Jetty 9+ 與 get go 是 100% 異步的,因此這種缺乏差異是有道理的。 (事實上,在 Jetty 9+ 中,當使用InputStream.read()OutputStream.write()等同步 API 時,需要做額外的工作來假裝是同步的)

此外,您的負載測試工作負載也不現實。

  • 您需要更多的客戶端機器來進行測試。 沒有一個單獨的軟件客戶端能夠對 Jetty 服務器施加壓力。 在達到任何類型的 Jetty 服務限制之前,您將達到系統資源限制。
    • 客戶端機器與服務器機器的比率至少為 4 比 1(我們以 8 比 1 的比率進行測試),以產生足夠的負載來給 Jetty 施加壓力。
  • 您需要到服務器的許多並發連接。 (想想 40,000+)
    • 或者您想要圖片中的 HTTP/2(這也以自己獨特的方式強調服務器資源)
  • 您希望返回大量數據(需要多個網絡緩沖區才能返回)。
  • 您還想加入一些讀取速度很慢的客戶端連接(這在同步服務器上可能會通過簡單地消耗太多資源而影響不慢的連接的 rest)

是的,Jaokim 稱之為,wrk 是這里的瓶頸。 如果我按照上面的建議並行運行它們,則 rps 的數量是 4 倍。

➜  ~ ➜  ~ wrk2 -t16 -c500 -d5s -R3000 http://localhost:8080/async-delay & ; wrk2 -t16 -c500 -d5s -R3000 http://localhost:8080/async-delay & ; wrk2 -t16 -c500 -d5s -R3000 http://localhost:8080/async-delay & ; wrk2 -t16 -c500 -d5s -R3000 http://localhost:8080/async-delay & ;
[1] 2779
[2] 2780
[3] 2781
[4] 2782
Running 5s test @ http://localhost:8080/async-delay
  16 threads and 500 connections
Running 5s test @ http://localhost:8080/async-delay
  16 threads and 500 connections
Running 5s test @ http://localhost:8080/async-delay
  16 threads and 500 connections
Running 5s test @ http://localhost:8080/async-delay
  16 threads and 500 connections
➜  ~   Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   104.09ms   13.99ms 337.66ms   97.43%
    Req/Sec        nan       nan   0.00      0.00%
  7066 requests in 5.06s, 841.85KB read
  Socket errors: connect 261, read 14, write 1, timeout 522
Requests/sec:   1395.35
Transfer/sec:    166.24KB
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   103.84ms   12.70ms 239.23ms   97.48%
    Req/Sec        nan       nan   0.00      0.00%
  7066 requests in 5.06s, 841.85KB read
  Socket errors: connect 261, read 9, write 2, timeout 522
Requests/sec:   1395.56
Transfer/sec:    166.27KB

[1]    2779 done       wrk2 -t16 -c500 -d5s -R3000 http://localhost:8080/async-delay
[2]    2780 done       wrk2 -t16 -c500 -d5s -R3000 http://localhost:8080/async-delay
➜  ~ ➜  ~   Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   103.62ms   12.48ms 243.58ms   97.67%
    Req/Sec        nan       nan   0.00      0.00%
  7064 requests in 6.16s, 841.61KB read
  Socket errors: connect 261, read 13, write 2, timeout 584
Requests/sec:   1147.51
Transfer/sec:    136.71KB
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   103.50ms   12.94ms 339.46ms   97.83%
    Req/Sec        nan       nan   0.00      0.00%
  7055 requests in 6.16s, 840.54KB read
  Socket errors: connect 261, read 6, write 2, timeout 646
Requests/sec:   1145.42
Transfer/sec:    136.47KB

[3]  - 2781 done       wrk2 -t16 -c500 -d5s -R3000 http://localhost:8080/async-delay
[4]  + 2782 done       wrk2 -t16 -c500 -d5s -R3000 http://localhost:8080/async-delay
➜  ~ ➜  ~

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM