Make jmh available for all subprojects. (#1425)

diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index db5e1ce..91279cc 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -119,6 +119,36 @@
 `Disallowed import - edu.umd.cs.findbugs.annotations.SuppressFBWarnings. [ImportControl]`
 could mean that `import-control.xml` needs to be updated.
 
+## Benchmarks
+
+### Invoke all benchmarks on a sub-project
+
+```bash
+$ ./gradlew clean :opencensus-impl-core:jmh
+```
+
+### Invoke on a single benchmark class
+
+```bash
+./gradlew -PjmhIncludeSingleClass=BinaryFormatImplBenchmark clean :opencensus-impl-core:jmh
+```
+
+### Debug compilation errors
+When you make incompatible changes in the Benchmarks classes you may get compilation errors which
+are related to the old code not being compatible with the new code. Some of the reasons are:
+* Any plugin cannot delete the generated code (jmh generates code) because if the user configured
+the directory as the same as source code the plugin will delete users source code.
+* After you run jmh, a gradle daemon will stay alive which may cache the generated code in memory
+and use use that generated code even if the files were changed. This is an issue for classes
+generated with auto-value.
+
+Run this commands to clean the Gradle's cache:
+```bash
+./gradlew --stop
+rm -fr .gradle/
+rm -fr benchmarks/build
+```
+
 ## Proposing changes
 
 Create a Pull Request with your changes. Please add any user-visible changes to
diff --git a/all/build.gradle b/all/build.gradle
index 0ba7cb6..3832e6c 100644
--- a/all/build.gradle
+++ b/all/build.gradle
@@ -1,11 +1,5 @@
 description = "OpenCensus All"
 
-buildscript {
-    repositories {
-        mavenCentral()
-    }
-}
-
 def subprojects = [
         project(':opencensus-api'),
         project(':opencensus-impl-core'),
diff --git a/api/build.gradle b/api/build.gradle
index e494ce0..31274ca 100644
--- a/api/build.gradle
+++ b/api/build.gradle
@@ -9,5 +9,7 @@
     signature "net.sf.androidscents.signature:android-api-level-14:4.0_r4@signature"
 }
 
-javadoc.exclude 'io/opencensus/internal/**'
-javadoc.exclude 'io/opencensus/trace/internal/**'
\ No newline at end of file
+javadoc {
+    exclude 'io/opencensus/internal/**'
+    exclude 'io/opencensus/trace/internal/**'
+}
diff --git a/benchmarks/README.md b/benchmarks/README.md
index 617333b..e591a8d 100644
--- a/benchmarks/README.md
+++ b/benchmarks/README.md
@@ -1,22 +1,3 @@
 # OpenCensus Benchmarks
 
-## To run the benchmark use
-
-```
-$ ./gradlew :opencensus-benchmarks:jmh
-```
-
-## To debug compilation errors
-When you make incompatible changes in the Benchmarks classes you may get compilation errors which
-are related to the old code not being compatible with the new code. Some of the reasons are:
-* Any plugin cannot delete the generated code (jmh generates code) because if the user configured
-the directory as the same as source code the plugin will delete users source code.
-* After you run jmh a gradle demon will stay alive which may cache the generated code in memory and 
-generates the same code even if the files were changed.
-
-Run this commands to clean the Gradle's cache:
-```bash
-./gradlew --stop
-rm -fr .gradle/
-rm -fr benchmarks/build
-```
\ No newline at end of file
+See [here](../CONTRIBUTING.md#benchmarks) for how to run and debug issues with benchmarks.
\ No newline at end of file
diff --git a/benchmarks/build.gradle b/benchmarks/build.gradle
index 4a92224..04688dd 100644
--- a/benchmarks/build.gradle
+++ b/benchmarks/build.gradle
@@ -1,30 +1,5 @@
 description = 'OpenCensus Benchmarks'
 
-buildscript {
-    repositories {
-        jcenter()
-        maven {
-            url "https://plugins.gradle.org/m2/"
-        }
-    }
-    dependencies {
-        classpath "me.champeau.gradle:jmh-gradle-plugin:0.4.7"
-        classpath "gradle.plugin.io.morethan.jmhreport:gradle-jmh-report:0.7.0"
-    }
-}
-
-apply plugin: "me.champeau.gradle.jmh"
-apply plugin: "io.morethan.jmhreport"
-
-jmh {
-    jmhVersion = '1.20'
-    warmupIterations = 10
-    iterations = 10
-    fork = 1
-    failOnError = true
-    resultFormat = 'JSON'
-}
-
 dependencies {
     compile project(':opencensus-api'),
             project(':opencensus-impl-core'),
@@ -32,18 +7,6 @@
             project(':opencensus-impl')
 }
 
-compileJmhJava {
-    options.compilerArgs = compileJava.options.compilerArgs
-}
-
-// Generate html report for findbugsJmh.
-findbugsJmh {
-    reports {
-        xml.enabled = false
-        html.enabled = true
-    }
-}
-
 jmhReport {
     jmhResultPath = project.file("${project.buildDir}/reports/jmh/results.json")
     jmhReportOutput = project.file("${project.buildDir}/reports/jmh")
diff --git a/build.gradle b/build.gradle
index ff5ad7c..e0d9938 100644
--- a/build.gradle
+++ b/build.gradle
@@ -12,6 +12,8 @@
         classpath "net.ltgt.gradle:gradle-apt-plugin:0.10"
         classpath 'com.github.ben-manes:gradle-versions-plugin:0.17.0'
         classpath "gradle.plugin.com.github.sherter.google-java-format:google-java-format-gradle-plugin:0.7.1"
+        classpath "me.champeau.gradle:jmh-gradle-plugin:0.4.7"
+        classpath "gradle.plugin.io.morethan.jmhreport:gradle-jmh-report:0.7.0"
     }
 }
 
@@ -35,6 +37,8 @@
     apply plugin: 'ru.vyarus.animalsniffer'
     apply plugin: 'findbugs'
     apply plugin: 'net.ltgt.apt'
+    apply plugin: "me.champeau.gradle.jmh"
+    apply plugin: "io.morethan.jmhreport"
     // Plugins that require java8
     if (JavaVersion.current().isJava8Compatible()) {
         if (useErrorProne) {
@@ -65,7 +69,7 @@
         }
     }
 
-    [compileJava, compileTestJava].each() {
+    [compileJava, compileTestJava, compileJmhJava].each() {
         // We suppress the "try" warning because it disallows managing an auto-closeable with
         // try-with-resources without referencing the auto-closeable within the try block.
         // We suppress the "processing" warning as suggested in
@@ -272,6 +276,12 @@
             html.enabled = true
         }
     }
+    findbugsJmh {
+        reports {
+            xml.enabled = false
+            html.enabled = true
+        }
+    }
 
     checkstyle {
         configFile = file("$rootDir/buildscripts/checkstyle.xml")
@@ -286,6 +296,7 @@
     // Disable checkstyle if no java8.
     checkstyleMain.enabled = JavaVersion.current().isJava8Compatible()
     checkstyleTest.enabled = JavaVersion.current().isJava8Compatible()
+    checkstyleJmh.enabled = JavaVersion.current().isJava8Compatible()
 
     // Google formatter works only on java8.
     if (JavaVersion.current().isJava8Compatible()) {
@@ -325,6 +336,29 @@
         archives javadocJar, sourcesJar
     }
 
+    jmh {
+        jmhVersion = '1.20'
+        warmupIterations = 10
+        iterations = 10
+        fork = 1
+        failOnError = true
+        resultFormat = 'JSON'
+        // Allow to run single benchmark class like:
+        // ./gradlew -PjmhIncludeSingleClass=StatsTraceContextBenchmark clean :grpc-core:jmh
+        if (project.hasProperty('jmhIncludeSingleClass')) {
+            include = [
+                    project.property('jmhIncludeSingleClass')
+            ]
+        }
+    }
+
+    jmhReport {
+        jmhResultPath = project.file("${project.buildDir}/reports/jmh/results.json")
+        jmhReportOutput = project.file("${project.buildDir}/reports/jmh")
+    }
+
+    tasks.jmh.finalizedBy tasks.jmhReport
+
     uploadArchives {
         repositories {
             mavenDeployer {
diff --git a/contrib/agent/build.gradle b/contrib/agent/build.gradle
index f0c1abf..11271a4 100644
--- a/contrib/agent/build.gradle
+++ b/contrib/agent/build.gradle
@@ -1,7 +1,5 @@
 plugins {
   id 'com.github.johnrengelman.shadow' version '2.0.2'
-  id 'me.champeau.gradle.jmh' version '0.4.5'
-  id 'io.morethan.jmhreport' version '0.7.0'
 }
 
 description = 'OpenCensus Agent'
@@ -234,17 +232,6 @@
   jmh libraries.grpc_context
 }
 
-// Disable checkstyle for JMH benchmarks if not java8.
-checkstyleJmh.enabled = JavaVersion.current().isJava8Compatible()
-
-// Generate html report for findbugsJmh.
-findbugsJmh {
-  reports {
-    xml.enabled = false
-    html.enabled = true
-  }
-}
-
 // Make the agent JAR available using a fixed file name so that we don't have to modify the JMH
 // benchmarks whenever the version changes.
 task agentJar(type: Copy) {
@@ -257,19 +244,3 @@
 
 jmhJar.dependsOn agentJar
 jmhJar.dependsOn integrationTest
-
-jmh {
-  jmhVersion = '1.19'
-  warmupIterations = 10
-  iterations = 10
-  fork = 1
-  failOnError = true
-  resultFormat = 'JSON'
-}
-
-jmhReport {
-  jmhResultPath = project.file("${project.buildDir}/reports/jmh/results.json")
-  jmhReportOutput = project.file("${project.buildDir}/reports/jmh")
-}
-
-tasks.jmh.finalizedBy tasks.jmhReport
diff --git a/benchmarks/src/jmh/java/io/opencensus/benchmarks/trace/propagation/B3FormatImplBenchmark.java b/impl_core/src/jmh/java/io/opencensus/implcore/trace/propagation/B3FormatImplBenchmark.java
similarity index 94%
rename from benchmarks/src/jmh/java/io/opencensus/benchmarks/trace/propagation/B3FormatImplBenchmark.java
rename to impl_core/src/jmh/java/io/opencensus/implcore/trace/propagation/B3FormatImplBenchmark.java
index eb20ec2..736c370 100644
--- a/benchmarks/src/jmh/java/io/opencensus/benchmarks/trace/propagation/B3FormatImplBenchmark.java
+++ b/impl_core/src/jmh/java/io/opencensus/implcore/trace/propagation/B3FormatImplBenchmark.java
@@ -14,14 +14,13 @@
  * limitations under the License.
  */
 
-package io.opencensus.benchmarks.trace.propagation;
+package io.opencensus.implcore.trace.propagation;
 
 import io.opencensus.trace.SpanContext;
 import io.opencensus.trace.SpanId;
 import io.opencensus.trace.TraceId;
 import io.opencensus.trace.TraceOptions;
 import io.opencensus.trace.Tracestate;
-import io.opencensus.trace.Tracing;
 import io.opencensus.trace.propagation.SpanContextParseException;
 import io.opencensus.trace.propagation.TextFormat;
 import io.opencensus.trace.propagation.TextFormat.Getter;
@@ -49,7 +48,7 @@
 
     @Setup
     public void setup() {
-      textFormatBase = new TextFormatBenchmarkBase(Tracing.getPropagationComponent().getB3Format());
+      textFormatBase = new TextFormatBenchmarkBase(new B3Format());
       Random random = new Random(1234);
       spanContext =
           SpanContext.create(
diff --git a/benchmarks/src/jmh/java/io/opencensus/benchmarks/trace/propagation/BinaryFormatImplBenchmark.java b/impl_core/src/jmh/java/io/opencensus/implcore/trace/propagation/BinaryFormatImplBenchmark.java
similarity index 94%
rename from benchmarks/src/jmh/java/io/opencensus/benchmarks/trace/propagation/BinaryFormatImplBenchmark.java
rename to impl_core/src/jmh/java/io/opencensus/implcore/trace/propagation/BinaryFormatImplBenchmark.java
index 391ca90..70e590b 100644
--- a/benchmarks/src/jmh/java/io/opencensus/benchmarks/trace/propagation/BinaryFormatImplBenchmark.java
+++ b/impl_core/src/jmh/java/io/opencensus/implcore/trace/propagation/BinaryFormatImplBenchmark.java
@@ -14,14 +14,13 @@
  * limitations under the License.
  */
 
-package io.opencensus.benchmarks.trace.propagation;
+package io.opencensus.implcore.trace.propagation;
 
 import io.opencensus.trace.SpanContext;
 import io.opencensus.trace.SpanId;
 import io.opencensus.trace.TraceId;
 import io.opencensus.trace.TraceOptions;
 import io.opencensus.trace.Tracestate;
-import io.opencensus.trace.Tracing;
 import io.opencensus.trace.propagation.BinaryFormat;
 import io.opencensus.trace.propagation.SpanContextParseException;
 import java.util.Random;
@@ -45,7 +44,7 @@
 
     @Setup
     public void setup() {
-      binaryFormat = Tracing.getPropagationComponent().getBinaryFormat();
+      binaryFormat = new BinaryFormatImpl();
       Random random = new Random(1234);
       spanContext =
           SpanContext.create(
diff --git a/benchmarks/src/jmh/java/io/opencensus/benchmarks/trace/propagation/TextFormatBenchmarkBase.java b/impl_core/src/jmh/java/io/opencensus/implcore/trace/propagation/TextFormatBenchmarkBase.java
similarity index 97%
rename from benchmarks/src/jmh/java/io/opencensus/benchmarks/trace/propagation/TextFormatBenchmarkBase.java
rename to impl_core/src/jmh/java/io/opencensus/implcore/trace/propagation/TextFormatBenchmarkBase.java
index 902b0a3..1463692 100644
--- a/benchmarks/src/jmh/java/io/opencensus/benchmarks/trace/propagation/TextFormatBenchmarkBase.java
+++ b/impl_core/src/jmh/java/io/opencensus/implcore/trace/propagation/TextFormatBenchmarkBase.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package io.opencensus.benchmarks.trace.propagation;
+package io.opencensus.implcore.trace.propagation;
 
 import io.opencensus.trace.SpanContext;
 import io.opencensus.trace.propagation.SpanContextParseException;