All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.undefinedlabs.scope.rules.JmhRunnerScopeAgentRule Maven / Gradle / Ivy

Go to download

Scope is a APM for tests to give engineering teams unprecedented visibility into their CI process to quickly identify, troubleshoot and fix failed builds. This artifact contains the classes to instrument the Java JMH Benchmark Framework.

There is a newer version: 0.15.1-beta.2
Show newest version
package com.undefinedlabs.scope.rules;

import static com.undefinedlabs.scope.agent.ScopeClassLoaderMatcher.hasClassesNamed;
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.undefinedlabs.scope.ScopeGlobalTracer;
import com.undefinedlabs.scope.rules.extractor.ScopeBenchmarkStatisticsExtractorResolver;
import com.undefinedlabs.scope.rules.model.ScopeBenchmarkStatistics;
import com.undefinedlabs.scope.utils.baggage.BaggageKeys;
import com.undefinedlabs.scope.utils.baggage.BaggageValues;
import com.undefinedlabs.scope.utils.tag.TagKeys;
import com.undefinedlabs.scope.utils.tag.TagValues;
import io.opentracing.Span;
import io.opentracing.Tracer;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.commons.lang3.StringUtils;
import org.openjdk.jmh.infra.BenchmarkParams;
import org.openjdk.jmh.results.Result;
import org.openjdk.jmh.results.RunResult;

public class JmhRunnerScopeAgentRule extends AbstractScopeAgentRule {

  @Override
  protected ElementMatcher typeMatcher() {
    return named("org.openjdk.jmh.runner.Runner");
  }

  @Override
  protected ElementMatcher classLoaderMatcher() {
    return hasClassesNamed("org.openjdk.jmh.runner.Runner");
  }

  @Override
  protected Map, String> transformers() {
    final Map, String> transformers = new HashMap<>();
    transformers.put(named("run"), JmhRunnerScopeAgentRule.class.getName() + "$RunAdvice");
    return transformers;
  }

  public static class RunAdvice {

    @Advice.OnMethodExit
    public static void exit(@Advice.Return final Object runResultsObj) {
      if (runResultsObj == null) {
        return;
      }

      final Tracer tracer = ScopeGlobalTracer.get();

      final Collection runResults = (Collection) runResultsObj;
      for (final RunResult runResult : runResults) {
        final Result primaryResult = runResult.getPrimaryResult();
        final BenchmarkParams params = runResult.getParams();
        final String testName =
            extractTestName(params.getBenchmark()) + " [" + params.getMode().name() + "]";
        final String testClass = extractTestClass(params.getBenchmark());

        final ScopeBenchmarkStatistics statistics =
            ScopeBenchmarkStatisticsExtractorResolver.INSTANCE
                .resolve(params.getMode())
                .extract(primaryResult);

        final Span span =
            tracer
                .buildSpan(testName)
                .withTag(TagKeys.SPAN_KIND, TagValues.SPAN_KIND_TEST)
                .withTag(TagKeys.Test.TEST_NAME, testName)
                .withTag(TagKeys.Test.TEST_SUITE, testClass)
                .withTag(TagKeys.Test.TEST_LANGUAGE, TagValues.Test.Language.JAVA)
                .withTag(TagKeys.Test.TEST_STATUS, TagValues.Test.TEST_PASS)
                .withTag(TagKeys.Test.TEST_TYPE, TagKeys.Test.Type.BENCHMARK)
                .withTag(TagKeys.Benchmark.RUNS, statistics.getSampleCount())
                .withTag(TagKeys.Benchmark.MODE, params.getMode().name())
                .withTag(TagKeys.Benchmark.THREAD_COUNT, params.getThreads())
                .withTag(TagKeys.Benchmark.FORK_COUNT, params.getForks())
                .withTag(TagKeys.Benchmark.OPS_PER_INVOCATION, params.getOpsPerInvocation())
                .withTag(TagKeys.Benchmark.WARMUP_FORKS, params.getWarmupForks())
                .withTag(TagKeys.Benchmark.WARMUP_COUNT, params.getWarmup().getCount())
                .withTag(TagKeys.Benchmark.Duration.SCORE, statistics.getScore())
                .withTag(
                    TagKeys.Benchmark.Duration.SCORE_CONFIDENCE,
                    Arrays.toString(statistics.getScoreConfidence()))
                .withTag(TagKeys.Benchmark.Duration.SCORE_ERROR, statistics.getScoreError())
                .withTag(TagKeys.Benchmark.Duration.MIN, statistics.getMin())
                .withTag(TagKeys.Benchmark.Duration.MEAN, statistics.getMean())
                .withTag(TagKeys.Benchmark.Duration.MAX, statistics.getMax())
                .withTag(TagKeys.Benchmark.Duration.STD_DEV, statistics.getStdDev())
                .withTag(TagKeys.Benchmark.Duration.VARIANCE, statistics.getVariance())
                .withTag(TagKeys.Benchmark.Duration.P00, statistics.getP00())
                .withTag(TagKeys.Benchmark.Duration.P50, statistics.getP50())
                .withTag(TagKeys.Benchmark.Duration.P90, statistics.getP90())
                .withTag(TagKeys.Benchmark.Duration.P95, statistics.getP95())
                .withTag(TagKeys.Benchmark.Duration.P99, statistics.getP99())
                .withTag(TagKeys.Benchmark.Duration.P100, statistics.getP100())
                .ignoreActiveSpan()
                .start();

        span.setBaggageItem(BaggageKeys.TRACE_KIND, BaggageValues.TRACE_KIND_TEST);
        span.finish();
      }
    }

    public static String extractTestName(final String benchmark) {
      if (StringUtils.isEmpty(benchmark)) {
        return "";
      }

      final String[] benchmarkChunks = benchmark.split("\\.");
      return benchmarkChunks[benchmarkChunks.length - 1];
    }

    public static String extractTestClass(final String benchmark) {
      if (StringUtils.isEmpty(benchmark)) {
        return "";
      }

      final String[] benchmarkChunks = benchmark.split("\\.");
      return StringUtils.join(
          Arrays.copyOfRange(benchmarkChunks, 0, benchmarkChunks.length - 1), ".");
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy