Spring延迟了Okhttp的结果性能问题
我正在尝试测试一个基本的阻塞和非阻塞API控制器,该控制器流式传输Spring延迟了Okhttp的结果性能问题,spring,performance,spring-mvc,okhttp,deferred,Spring,Performance,Spring Mvc,Okhttp,Deferred,我正在尝试测试一个基本的阻塞和非阻塞API控制器,该控制器流式传输OkHttp响应,如下所示: @SpringBootApplication public class HttpProxyServiceApplication { public static void main(String[] args) { SpringApplication.run(HttpProxyServiceApplication.class, args); } @Bean
OkHttp
响应,如下所示:
@SpringBootApplication
public class HttpProxyServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HttpProxyServiceApplication.class, args);
}
@Bean
public OkHttpClient okHttpClient() {
return new OkHttpClient();
}
@RestController
@RequestMapping(path = "/test")
public static class TestController {
private static final String URL = ...;
@Autowired
private OkHttpClient client;
@RequestMapping(method = RequestMethod.GET, path = "blocking")
public void blocking(HttpServletResponse httpServletResponse) throws IOException {
Request request = new Request.Builder()
.url(URL)
.build();
try (ResponseBody body = client.newCall(request).execute().body()) {
StreamUtils.copy(body.byteStream(), httpServletResponse.getOutputStream());
}
}
@RequestMapping(method = RequestMethod.GET, path = "non-blocking")
public DeferredResult<ResponseEntity<?>> nonBlocking() {
Request request = new Request.Builder()
.url(URL)
.build();
DeferredResult<ResponseEntity<?>> deferredResult = new DeferredResult<>();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
ResponseEntity<Void> responseEntity =
new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE);
deferredResult.setResult(responseEntity);
}
@Override
public void onResponse(Response response) throws IOException {
ResponseEntity<InputStreamResource> responseEntity =
new ResponseEntity<>(new InputStreamResource(response.body().byteStream()),
HttpStatus.ACCEPTED);
deferredResult.setResult(responseEntity);
}
});
return deferredResult;
}
}
}
@springboot应用程序
公共类HttpProxyServiceApplication{
公共静态void main(字符串[]args){
run(HttpProxyServiceApplication.class,args);
}
@豆子
公共OkHttpClient OkHttpClient(){
返回新的OkHttpClient();
}
@RestController
@请求映射(路径=“/test”)
公共静态类TestController{
私有静态最终字符串URL=。。。;
@自动连线
私人Okhttp客户端;
@RequestMapping(method=RequestMethod.GET,path=“blocking”)
公共无效阻止(HttpServletResponse HttpServletResponse)引发IOException{
Request Request=newrequest.Builder()
.url(url)
.build();
try(ResponseBody body=client.newCall(request.execute().body()){
copy(body.byteStream(),httpServletResponse.getOutputStream());
}
}
@RequestMapping(method=RequestMethod.GET,path=“非阻塞”)
public DeferredResult>DeferredResult=新的DeferredResult();
client.newCall(request).enqueue(new Callback()){
@凌驾
公共void onFailure(请求,IOE异常){
响应响应响应响应响应响应=
新响应(HttpStatus.SERVICE\u不可用);
延迟结果。设置结果(响应性);
}
@凌驾
public void onResponse(Response-Response)引发IOException{
响应响应响应响应响应响应=
新的ResponseEntity(新的InputStreamResource(response.body().ByTestStream()),
HttpStatus(已接受);
延迟结果。设置结果(响应性);
}
});
返回延迟结果;
}
}
}
重要事项:我想流式传输结果!我不想把整个结果保存在内存中。这就是为什么我为非阻塞创建了一个响应属性
然而,使用在两个API上爬行的基本JMeter脚本,非阻塞的速度实际上比阻塞的慢
- 500线
更新1: 新测试
@RequestMapping(method = RequestMethod.GET, path = "non-blocking-2")
public DeferredResult<InputStreamResource> nonBlockingBlocking() throws IOException {
Request request = new Request.Builder()
.url(URL)
.build();
DeferredResult<InputStreamResource> deferredResult = new DeferredResult<>();
deferredResult.setResult(new InputStreamResource(client.newCall(request).execute().body().byteStream()));
return deferredResult;
}
@RequestMapping(method=RequestMethod.GET,path=“non-blocking-2”)
public DeferredResult nonBlockingBlocking()引发IOException{
Request Request=newrequest.Builder()
.url(url)
.build();
DeferredResult DeferredResult=新的DeferredResult();
deferredResult.setResult(新的InputStreamResource(client.newCall(request.execute().body().ByTestStream());
返回延迟结果;
}
与完全阻塞的结果类似。因此,我不认为性能问题直接来自于
DeferredResult
,当然后者也会有相同的时间,因为您正在同步执行调用,这基本上禁用了DeferredResult
的效果。您必须在另一个线程上执行客户端调用,如果不执行,它仍然运行syncronous。请注意,尽管OkHttp有一个异步API,但它的实现目前每个请求使用一个线程。