idea-jmh-plugin

repository·develop·Indexed 19 days ago

https://github.com/artyushov/idea-jmh-plugin

An IntelliJ IDEA plugin that integrates the Java Microbenchmark Harness (JMH) into the IDE. It enables developers to generate, run, and configure benchmarks with a JUnit-like experience, providing tools for benchmark method generation and run configuration management.

Tokens
1K
Snippets
3
Records
5
Agent score
16%

What's inside idea-jmh-plugin

  1. Interpret the JMH plugin results table

    develop

    When viewing benchmark results in the plugin, the output is presented in a tabular format similar to the JMH command line. The columns represent:

    • Benchmark: The name of the benchmark method.
    • Mode: The JMH measurement mode (e.g., thrpt, avgt, sample, ss).
    • Samples: The number of samples collected.
    • Score-Idea: The result calculated by the IntelliJ IDEA plugin.
    • Score-Term: The result from the standard JMH terminal/command line execution.
    • (MAX - MIN)/MAX: The relative difference between the maximum and minimum scores.
    • Units: The measurement units (e.g., ops/s, us/op, ops/us).
    Benchmark             Mode   Samples   Score-Idea   Score-Term  (MAX - MIN)/MAX     Units
    measureThroughput    thrpt         5        9.969        9.926            0.004     ops/s
    measureAvgTime        avgt         5   100228.545   100779.818            0.005     us/op
  2. Install the JMH IntelliJ IDEA plugin

    develop

    To use the plugin, you must first ensure your project is correctly configured, then install the plugin via IntelliJ IDEA.

    Prerequisites

    Your module's classpath must include the following dependencies:

    • jmh-core
    • jmh-generator-annprocess

    Additionally, Annotation processing must be enabled in your IDE settings for benchmarks to run correctly.

    Installation

    1. Open IntelliJ IDEA.
    2. Search for JMH in the plugin repositories.
    3. Install the plugin.
  3. Generate and run JMH benchmarks

    develop

    The plugin allows you to interact with JMH benchmarks using standard IntelliJ IDEA actions, similar to how you use JUnit.

    Generate a new benchmark method

    To create a new @Benchmark method:

    1. Right-click in the editor pane and select Generate micro benchmark.
    2. Alternatively, use the Generate... action via keyboard shortcuts:
      • Windows/Linux: Alt+Insert
      • macOS: Ctrl + N

    Run benchmarks

    • Run a single method: Place your cursor on the @Benchmark method declaration and press Ctrl + Shift + F10.
    • Run all benchmarks in a class: Place your cursor on the class declaration and press Ctrl + Shift + F10.

    Managing Run Configurations

    Invoking the Run action creates a new run configuration with default JMH parameters.

    • To edit a specific run: Edit the generated run configuration in the Run/Debug Configurations dialog.
    • To edit defaults for all future benchmarks: Modify the JMH run configuration template.
    // Example of a benchmark method target
    @Benchmark
    public void myBenchmarkMethod() {
        // ...
    }
  4. Fix JMH lock errors on Windows

    develop

    If you encounter the following error on Windows:

    ERROR: org.openjdk.jmh.runner.RunnerException: ERROR: Exception while trying to acquire the JMH lock (C:\WINDOWS\/jmh.lock):

    This occurs because JMH is attempting to run with an empty environment and cannot write to the lock file. You can resolve this using one of two methods:

    1. Set Environment Variables: Define a TMP or TEMP environment variable that points to a directory you have permission to write to.
    2. Set JVM Argument: Add the following JVM argument to your Run Configuration to point to a writable directory: -Djava.io.tmpdir=C:\temp
    -Djava.io.tmpdir=C:\temp
  5. Compare IDEA plugin results with command line JMH results

    develop
    The plugin provides benchmark results within IntelliJ IDEA that can be compared against standard command line JMH results to verify accuracy. For a sample benchmark (JMHSample_01_HelloWorld) run with parameters -f 10 -wi 10 -i 20 -tu us, the results showed a mean difference of only 1.5% between IDEA and the command line. Statistical validation using the Kolmogorov-Smirnov test at a significance level of 0.05 indicated that the distributions are not significantly different, suggesting the plugin does not negatively affect benchmark quality.