Fra*_*cis 5 java scala reactive-programming rx-java reactivex
我在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?
}
Run Code Online (Sandbox Code Playgroud)
我用以下方法称呼它
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)
Run Code Online (Sandbox Code Playgroud)
如何衡量观察者运行所需的时间?
提前感谢你
弗朗西斯
您需要在一个doOnSubscribe()块中启动计时器,然后在中完成它onTerminated().
一个例子可能是这样的:
long start;
observable()
.doOnSubscribe(() -> start = System.nanoTime())
.doOnTerminate(() -> System.out.println(System.nanoTime() - start));
Run Code Online (Sandbox Code Playgroud)
或者,您可以按照Netflix对RxNetty执行的操作,并将开始时间作为流经链的对象的一部分返回,并在最后使用它.