简体   繁体   中英

Async API giving worse performance

Interesting, I would think have 255 concurrent users, an async API would have better performance. Here are 2 of my endpoints in my Spring server:

@RequestMapping("/async")
public CompletableFuture<String> g(){
    CompletableFuture<String> f = new CompletableFuture<>();
    f.runAsync(() -> {
        try {
            Thread.sleep(500);
            f.complete("Finished");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });

    return f;
}

@RequestMapping("/sync")
public String h() throws InterruptedException {
    Thread.sleep(500);
    return "Finished";
}

In the /async it runs it on a different thread. I am using Siege for load testing as follows:

siege http://localhost:8080/sync --concurrent=255 --time=10S > /dev/null

For the async endpoint, I got a transaction number of 27 hits

For the sync endpoint, I got a transaction number of 1531 hits

So why is this? Why isnt the async endpoint able to handle more transactions?

Because the async endpoint is using a shared (the small ForkJoinPool.commonPool() ) threadpool to execute the sleeps, whereas the sync endpoint uses the larger threadpool of the application server. Since the common pool is so small, you're running maybe 4-8 operations (well, if you call sleeping an operation) at a time, while others are waiting for their turn to even get in the pool. You can use a bigger pool with CompletableFuture.runAsync(Runnable, Executor) (you're also calling the method wrong, it's a static method that returns a CompletableFuture ).

Async isn't a magical "make things faster" technique. Your example is flawed as all the requests take 500ms and you're only adding overhead in the async one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM