spring webflux version: 2.7.2
I tested a simple webflux project (although I tested with SSL/TLS, I believe the problem would still exist without them), and I found problems with titles like: "HTTP request returns immediately after entering webFilter" and "HTTP returns as soon as the request enters the controller", there is a huge performance difference between the two cases.
TPS tested with Apache Bench in the case of 4-cores CPU:
- "Return as soon as the HTTP request enters the webFilter" = 67000
- "The HTTP request will return immediately after entering the controller" = 20000 ~ 30000
Some additional conditions are added:
- In the webFilter, the only thing I do is add the
keep-aliveresponse header to the response, and then return the response. (code below) - There is no code content that I added after the webFilter. The next piece of code I added is the controller
- no injection with
@Autowiredin the controller. - The controller method uses
@RequestBodyto get parameters. The length of the input parameters is very small. Take the following code as an example: message 16 bytes (code below)
Code:
- MyFilter
public class MyFilter implements WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
exchange.getResponse().getHeaders().add("keep-alive", "true");
// when i need to finish request
return exchange.getResponse().writeWith(Mono.just(DefaultDataBufferFactory.sharedInstance.wrap("hello".getBytes())));
// when i don't need to finish request
return chain.filter(exchange);
}
}
- MyController:
@RestController
public class MyController {
@PostMapping("/hello")
public Mono<HelloBody> hello(@RequestBody HelloBody helloBody) {
return Mono.just(helloBody);
}
public static class HelloBody {
private String message;
// get/set
}
}
As can be seen from the above code, such a test does not involve any business.
What I want to know is why there is such a big difference in performance returned by these two scenarios. More importantly, how can I reduce the performance loss of this part, so as to strive for more room for improvement in the performance of my program.