Home | History | Annotate | Download | only in instrumentation
      1 /*
      2  * Copyright 2017, OpenCensus Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package io.opencensus.contrib.agent.instrumentation;
     18 
     19 import io.grpc.Context;
     20 import java.util.concurrent.TimeUnit;
     21 import org.openjdk.jmh.annotations.Benchmark;
     22 import org.openjdk.jmh.annotations.BenchmarkMode;
     23 import org.openjdk.jmh.annotations.Fork;
     24 import org.openjdk.jmh.annotations.Mode;
     25 import org.openjdk.jmh.annotations.OutputTimeUnit;
     26 import org.openjdk.jmh.infra.Blackhole;
     27 
     28 /** Naive benchmarks for automatic context propagation added by {@link ThreadInstrumentation}. */
     29 public class ThreadInstrumentationBenchmark {
     30 
     31   private static final class MyRunnable implements Runnable {
     32 
     33     private final Blackhole blackhole;
     34 
     35     private MyRunnable(Blackhole blackhole) {
     36       this.blackhole = blackhole;
     37     }
     38 
     39     @Override
     40     public void run() {
     41       blackhole.consume(Context.current());
     42     }
     43   }
     44 
     45   /**
     46    * This benchmark attempts to measure the performance without any context propagation.
     47    *
     48    * @param blackhole a {@link Blackhole} object supplied by JMH
     49    */
     50   @Benchmark
     51   @BenchmarkMode(Mode.AverageTime)
     52   @OutputTimeUnit(TimeUnit.MICROSECONDS)
     53   @Fork
     54   public void none(Blackhole blackhole) throws InterruptedException {
     55     Thread t = new Thread(new MyRunnable(blackhole));
     56     t.start();
     57     t.join();
     58   }
     59 
     60   /**
     61    * This benchmark attempts to measure the performance with manual context propagation.
     62    *
     63    * @param blackhole a {@link Blackhole} object supplied by JMH
     64    */
     65   @Benchmark
     66   @BenchmarkMode(Mode.AverageTime)
     67   @OutputTimeUnit(TimeUnit.MICROSECONDS)
     68   @Fork
     69   public void manual(Blackhole blackhole) throws InterruptedException {
     70     Thread t = new Thread((Context.current().wrap(new MyRunnable(blackhole))));
     71     t.start();
     72     t.join();
     73   }
     74 
     75   /**
     76    * This benchmark attempts to measure the performance with automatic context propagation.
     77    *
     78    * @param blackhole a {@link Blackhole} object supplied by JMH
     79    */
     80   @Benchmark
     81   @BenchmarkMode(Mode.AverageTime)
     82   @OutputTimeUnit(TimeUnit.MICROSECONDS)
     83   @Fork(jvmArgsAppend = "-javaagent:contrib/agent/build/libs/agent.jar")
     84   public void automatic(Blackhole blackhole) throws InterruptedException {
     85     Thread t = new Thread(new MyRunnable(blackhole));
     86     t.start();
     87     t.join();
     88   }
     89 }
     90