gpt4 book ai didi

java - 在 Web 应用程序中使用 RxJava Observables 无法解释的缺乏性能改进

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:46 26 4
gpt4 key购买 nike

我正在执行一些测试,以评估使用基于 Observables 的响应式(Reactive) API 而非传统的阻塞式 API 是否具有真正的优势。

整个例子是available on Githug

令人惊讶的结果表明thoughput结果是:

  • 最好的:返回包装阻塞操作的 Callable/DeferredResult 的 REST 服务。

    <
  • 还不错:阻止 REST 服务。

  • 最差:返回 DeferredResult 的 REST 服务,其结果由 RxJava Observable 设置。

这是我的 Spring WebApp:

应用:

@SpringBootApplication
public class SpringNioRestApplication {

@Bean
public ThreadPoolTaskExecutor executor(){
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(20);
return executor;
}

public static void main(String[] args) {
SpringApplication.run(SpringNioRestApplication.class, args);
}
}

同步 Controller :

@RestController("SyncRestController")
@Api(value="", description="Synchronous data controller")
public class SyncRestController {

@Autowired
private DataService dataService;

@RequestMapping(value="/sync/data", method=RequestMethod.GET, produces="application/json")
@ApiOperation(value = "Gets data", notes="Gets data synchronously")
@ApiResponses(value={@ApiResponse(code=200, message="OK")})
public List<Data> getData(){
return dataService.loadData();
}
}

AsyncController:具有原始的 Callable 和 Observable 端点

@RestController
@Api(value="", description="Synchronous data controller")
public class AsyncRestController {

@Autowired
private DataService dataService;

private Scheduler scheduler;

@Autowired
private TaskExecutor executor;

@PostConstruct
protected void initializeScheduler(){
scheduler = Schedulers.from(executor);
}

@RequestMapping(value="/async/data", method=RequestMethod.GET, produces="application/json")
@ApiOperation(value = "Gets data", notes="Gets data asynchronously")
@ApiResponses(value={@ApiResponse(code=200, message="OK")})
public Callable<List<Data>> getData(){
return ( () -> {return dataService.loadData();} );
}

@RequestMapping(value="/observable/data", method=RequestMethod.GET, produces="application/json")
@ApiOperation(value = "Gets data through Observable", notes="Gets data asynchronously through Observable")
@ApiResponses(value={@ApiResponse(code=200, message="OK")})
public DeferredResult<List<Data>> getDataObservable(){
DeferredResult<List<Data>> dr = new DeferredResult<List<Data>>();
Observable<List<Data>> dataObservable = dataService.loadDataObservable();
dataObservable.subscribeOn(scheduler).subscribe( dr::setResult, dr::setErrorResult);
return dr;
}
}

DataServiceImpl

@Service
public class DataServiceImpl implements DataService{

@Override
public List<Data> loadData() {
return generateData();
}

@Override
public Observable<List<Data>> loadDataObservable() {
return Observable.create( s -> {
List<Data> dataList = generateData();
s.onNext(dataList);
s.onCompleted();
});
}

private List<Data> generateData(){
List<Data> dataList = new ArrayList<Data>();
for (int i = 0; i < 20; i++) {
Data data = new Data("key"+i, "value"+i);
dataList.add(data);
}
//Processing time simulation
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return dataList;
}
}

我设置了 Thread.sleep(500) 延迟以增加服务响应时间。

负载测试的结果是:

Async with Callable:700 rps,无错误

>>loadtest -c 15 -t 60 --rps 700 http://localhost:8080/async/data    
...
Requests: 0, requests per second: 0, mean latency: 0 ms
Requests: 2839, requests per second: 568, mean latency: 500 ms
Requests: 6337, requests per second: 700, mean latency: 500 ms
Requests: 9836, requests per second: 700, mean latency: 500 ms
...
Completed requests: 41337
Total errors: 0
Total time: 60.002348360999996 s
Requests per second: 689
Total time: 60.002348360999996 s

阻塞:大约 404 rps 但产生错误

>>loadtest -c 15 -t 60 --rps 700 http://localhost:8080/sync/data    
...
Requests: 7683, requests per second: 400, mean latency: 7420 ms
Requests: 9683, requests per second: 400, mean latency: 9570 ms
Requests: 11680, requests per second: 399, mean latency: 11720 ms
Requests: 13699, requests per second: 404, mean latency: 13760 ms
...
Percentage of the requests served within a certain time
50% 8868 ms
90% 22434 ms
95% 24103 ms
99% 25351 ms
100% 26055 ms (longest request)

100% 26055 ms (longest request)

-1: 7559 errors
Requests: 31193, requests per second: 689, mean latency: 14350 ms
Errors: 1534, accumulated errors: 7559, 24.2% of total requests

Async with Observable:不超过 20 rps,更快出错

>>loadtest -c 15 -t 60 --rps 700 http://localhost:8080/observable/data
Requests: 0, requests per second: 0, mean latency: 0 ms
Requests: 90, requests per second: 18, mean latency: 2250 ms
Requests: 187, requests per second: 20, mean latency: 6770 ms
Requests: 265, requests per second: 16, mean latency: 11870 ms
Requests: 2872, requests per second: 521, mean latency: 1560 ms
Errors: 2518, accumulated errors: 2518, 87.7% of total requests
Requests: 6373, requests per second: 700, mean latency: 1590 ms
Errors: 3401, accumulated errors: 5919, 92.9% of total requests

Observable 以 10 的 corePoolSize 执行,但将其增加到 50 也没有任何改善。

有什么解释?

更新:根据 akarnokd 的建议,我进行了以下更改。在服务中从 Object.create 移动到 Object.fromCallable 并在 Controller 中重用了 Scheduler,但我仍然得到相同的结果。

最佳答案

问题是由某个时刻的编程错误引起的。实际上,问题中的示例非常有效。

一个警告,以防止其他人遇到问题:小心使用 Observable.just(func),func 实际上是在创建 Observable 时调用的。所以任何放置在那里的 Thread.sleep 都会阻塞调用线程

@Override
public Observable<List<Data>> loadDataObservable() {
return Observable.just(generateData()).delay(500, TimeUnit.MILLISECONDS);
}

private List<Data> generateData(){
List<Data> dataList = new ArrayList<Data>();
for (int i = 0; i < 20; i++) {
Data data = new Data("key"+i, "value"+i);
dataList.add(data);
}
return dataList;
}

我在 RxJava Google group 开始讨论他们帮我解决了这个问题。

关于java - 在 Web 应用程序中使用 RxJava Observables 无法解释的缺乏性能改进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34286017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com