7

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

Francis
  • 379
  • 5
  • 21

1 Answers1

11

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.

tddmonkey
  • 20,798
  • 10
  • 58
  • 67