title: RX Observable - measure execution time (even when nested)
body: <p>I'm using my appended class to measure execution time of my observables. This works fine, as long as I don't measure nested observables...</p>
  
  <p><strong>Working fine</strong></p>
  
  <pre><code>Observable&lt;T&gt; obs1 = ...;
  Observable&lt;T&gt; obs2 = ...;
  
  obs1
      .compose(RXMeasure.applyMeasurement(T.class, "tag1"))
      .subscribe();
  
  obs2
      .compose(RXMeasure.applyMeasurement(T.class, "tag2"))
      .subscribe();
  </code></pre>
  
  <p><strong>Not working</strong></p>
  
  <pre><code>Observable&lt;T&gt; obs3 = ...;
  
  Observable&lt;T&gt; obs = obs1
      .flatMap(resul1 -&gt; obs2)
      .flatMap(result2 -&gt; obs3)
      .subscribe();
  </code></pre>
  
  <p>This leads to the result, that all observables are subscribed to at the start and this means, the measurement is not correct anymore. Here I'm trying to execute the 3 observables sequentially...</p>
  
  <p><strong>What I want</strong></p>
  
  <p>I want an measure function that</p>
  
  <ul>
  <li>is not breaking the chain</li>
  <li>is measuring EXECUTION time of an observable (something like the time between start/end of observable instead of subscribe/terminate)</li>
  </ul>
  
  <p>Any ideas?</p>
  
  <p><strong>RXMeasure class</strong></p>
  
  <pre><code>public class RXMeasure
  {
      private static boolean mEnabled = true;
  
      public static void setEnabled(boolean enabled)
      {
          mEnabled = enabled;
      }
  
      public static &lt;T&gt; Observable.Transformer&lt;T, T&gt; applyMeasurement(Class clazz, String tag)
      {
          return observable -&gt; measure(observable, clazz, tag);
      }
  
      public static &lt;T&gt; Observable&lt;T&gt; measure(Observable&lt;T&gt; observable, Class clazz, String tag)
      {
          if (!mEnabled)
              return observable;
  
          LongHolder start = new LongHolder(0);
          return observable
                  .doOnSubscribe(() -&gt; start.set(System.currentTimeMillis()))
                  .doOnTerminate(() -&gt; L.d(clazz, "[" + tag + "] Duration: " + String.valueOf(System.currentTimeMillis() - start.get()) + "ms"));
      }
  }
  </code></pre>