Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long did it take to run an Observable using RxJava (ReactiveX)?

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

like image 352
Francis Avatar asked Apr 27 '16 20:04

Francis


People also ask

What is ReactiveX RxJava?

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.

What is an Observable in RxJava?

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.

What is ReactiveX programming?

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.


1 Answers

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.

like image 77
tddmonkey Avatar answered Oct 16 '22 18:10

tddmonkey