I'm using java ReactiveX (RxJava) in scala Play Framework 2.5 to communicate with couchbase asynchronously I would like to know how long it took for my observable to run? I define my observable using the code below.
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?
}
I call it using the following
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)
How can I measure how long it took for the observable to run?
Thanking you in advance
Francis
RxJava is a Java library that enables Functional Reactive Programming in Android development. It raises the level of abstraction around threading in order to simplify the implementation of complex concurrent behavior.
An Observable is like a speaker that emits the value. It does some work and emits some values. An Operator is like a translator which translates/modifies data from one form to another form. An Observer gets those values.
ReactiveX, also known as Reactive Extensions or RX, is a library for composing asynchronous and event-based programs by using observable sequences. This is perfect for Android, which is an event-driven and user-focused platform.
You'll want to start your timer in a doOnSubscribe()
block and then complete it in the onTerminated()
.
An example might be something like:
long start;
observable()
.doOnSubscribe(() -> start = System.nanoTime())
.doOnTerminate(() -> System.out.println(System.nanoTime() - start));
Alternatively you could follow what Netflix do with RxNetty and return the start time as part of the object flowing through the chain and use that at the end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With