【问题标题】:How long did it take to run an Observable using RxJava (ReactiveX)?使用 RxJava (ReactiveX) 运行 Observable 需要多长时间?
【发布时间】:2016-04-27 20:33:09
【问题描述】:

我在 scala Play Framework 2.5 中使用 java ReactiveX (RxJava) 与 couchbase 进行异步通信我想知道我的 observable 运行需要多长时间?我使用下面的代码定义我的 observable。

def get(id: String) : Observable[Profile] = {
  this.bucket
    .async()
    // can I have a start time here possibly using map?
    .get(id)
    .map[Profile](toProfile)
    // can I have an end time here possibly using map?
}

我使用以下方式调用它

Thread.sleep(1000)

val observable = get("myID")

Thread.sleep(1000)

// measure start time here
println("observable: " + observable.toBlocking.first())
// measure end time here

Thread.sleep(1000)

我如何测量 observable 运行需要多长时间?

提前谢谢你

弗朗西斯

【问题讨论】:

    标签: java scala rx-java reactive-programming reactivex


    【解决方案1】:

    您需要在 doOnSubscribe() 块中启动计时器,然后在 onTerminated() 中完成它。

    一个例子可能是这样的:

    long start;
    observable()
        .doOnSubscribe(() -> start = System.nanoTime())
        .doOnTerminate(() -> System.out.println(System.nanoTime() - start));
    

    或者,您可以按照 Netflix 对 RxNetty 的操作,将开始时间作为流经链的对象的一部分返回并在最后使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      • 2011-02-20
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多