jmh 表示 M1 比 M2 快,但 M1 委托给 M2

jmh indicates that M1 is faster than M2 but M1 delegates to M2

我写了一个 JMH 基准测试,涉及两种方法:M1 和 M2。 M1 调用 M2 但出于某种原因,JMH 声称 M1 比 M2 快。

这是基准源代码:

import java.util.concurrent.TimeUnit;
import static org.bitbucket.cowwoc.requirements.Requirements.assertThat;
import static org.bitbucket.cowwoc.requirements.Requirements.requireThat;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class MyBenchmark {

    @Benchmark
    public void assertMethod() {
        assertThat("value", "name").isNotNull().isNotEmpty();
    }

    @Benchmark
    public void requireMethod() {
        requireThat("value", "name").isNotNull().isNotEmpty();
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(MyBenchmark.class.getSimpleName())
                .forks(1)
                .build();

        new Runner(opt).run();
    }
}

在上面的例子中,M1是assertThat(),M2是requireThat()。意思是,assertThat() 在后台调用 requireThat()

这是基准输出:

# JMH 1.13 (released 8 days ago)
# VM version: JDK 1.8.0_102, VM 25.102-b14
# VM invoker: C:\Program Files\Java\jdk1.8.0_102\jre\bin\java.exe
# VM options: -ea
# Warmup: 20 iterations, 1 s each
# Measurement: 20 iterations, 1 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: com.mycompany.jmh.MyBenchmark.assertMethod

# Run progress: 0.00% complete, ETA 00:01:20
# Fork: 1 of 1
# Warmup Iteration   1: 8.268 ns/op
# Warmup Iteration   2: 6.082 ns/op
# Warmup Iteration   3: 4.846 ns/op
# Warmup Iteration   4: 4.854 ns/op
# Warmup Iteration   5: 4.834 ns/op
# Warmup Iteration   6: 4.831 ns/op
# Warmup Iteration   7: 4.815 ns/op
# Warmup Iteration   8: 4.839 ns/op
# Warmup Iteration   9: 4.825 ns/op
# Warmup Iteration  10: 4.812 ns/op
# Warmup Iteration  11: 4.806 ns/op
# Warmup Iteration  12: 4.805 ns/op
# Warmup Iteration  13: 4.802 ns/op
# Warmup Iteration  14: 4.813 ns/op
# Warmup Iteration  15: 4.805 ns/op
# Warmup Iteration  16: 4.818 ns/op
# Warmup Iteration  17: 4.815 ns/op
# Warmup Iteration  18: 4.817 ns/op
# Warmup Iteration  19: 4.812 ns/op
# Warmup Iteration  20: 4.810 ns/op
Iteration   1: 4.805 ns/op
Iteration   2: 4.816 ns/op
Iteration   3: 4.813 ns/op
Iteration   4: 4.938 ns/op
Iteration   5: 5.061 ns/op
Iteration   6: 5.129 ns/op
Iteration   7: 4.828 ns/op
Iteration   8: 4.837 ns/op
Iteration   9: 4.819 ns/op
Iteration  10: 4.815 ns/op
Iteration  11: 4.872 ns/op
Iteration  12: 4.806 ns/op
Iteration  13: 4.811 ns/op
Iteration  14: 4.827 ns/op
Iteration  15: 4.837 ns/op
Iteration  16: 4.842 ns/op
Iteration  17: 4.812 ns/op
Iteration  18: 4.809 ns/op
Iteration  19: 4.806 ns/op
Iteration  20: 4.815 ns/op


Result "assertMethod":
  4.855 �(99.9%) 0.077 ns/op [Average]
  (min, avg, max) = (4.805, 4.855, 5.129), stdev = 0.088
  CI (99.9%): [4.778, 4.932] (assumes normal distribution)


# JMH 1.13 (released 8 days ago)
# VM version: JDK 1.8.0_102, VM 25.102-b14
# VM invoker: C:\Program Files\Java\jdk1.8.0_102\jre\bin\java.exe
# VM options: -ea
# Warmup: 20 iterations, 1 s each
# Measurement: 20 iterations, 1 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: com.mycompany.jmh.MyBenchmark.requireMethod

# Run progress: 50.00% complete, ETA 00:00:40
# Fork: 1 of 1
# Warmup Iteration   1: 7.193 ns/op
# Warmup Iteration   2: 4.835 ns/op
# Warmup Iteration   3: 5.039 ns/op
# Warmup Iteration   4: 5.053 ns/op
# Warmup Iteration   5: 5.077 ns/op
# Warmup Iteration   6: 5.102 ns/op
# Warmup Iteration   7: 5.088 ns/op
# Warmup Iteration   8: 5.109 ns/op
# Warmup Iteration   9: 5.096 ns/op
# Warmup Iteration  10: 5.096 ns/op
# Warmup Iteration  11: 5.091 ns/op
# Warmup Iteration  12: 5.089 ns/op
# Warmup Iteration  13: 5.099 ns/op
# Warmup Iteration  14: 5.097 ns/op
# Warmup Iteration  15: 5.090 ns/op
# Warmup Iteration  16: 5.096 ns/op
# Warmup Iteration  17: 5.088 ns/op
# Warmup Iteration  18: 5.086 ns/op
# Warmup Iteration  19: 5.087 ns/op
# Warmup Iteration  20: 5.097 ns/op
Iteration   1: 5.097 ns/op
Iteration   2: 5.088 ns/op
Iteration   3: 5.092 ns/op
Iteration   4: 5.097 ns/op
Iteration   5: 5.082 ns/op
Iteration   6: 5.089 ns/op
Iteration   7: 5.086 ns/op
Iteration   8: 5.084 ns/op
Iteration   9: 5.090 ns/op
Iteration  10: 5.086 ns/op
Iteration  11: 5.084 ns/op
Iteration  12: 5.088 ns/op
Iteration  13: 5.091 ns/op
Iteration  14: 5.092 ns/op
Iteration  15: 5.085 ns/op
Iteration  16: 5.096 ns/op
Iteration  17: 5.078 ns/op
Iteration  18: 5.125 ns/op
Iteration  19: 5.089 ns/op
Iteration  20: 5.091 ns/op


Result "requireMethod":
  5.091 �(99.9%) 0.008 ns/op [Average]
  (min, avg, max) = (5.078, 5.091, 5.125), stdev = 0.010
  CI (99.9%): [5.082, 5.099] (assumes normal distribution)


# Run complete. Total time: 00:01:21

Benchmark                       Mode  Cnt  Score   Error  Units
MyBenchmark.assertMethod        avgt   20  4.855 � 0.077  ns/op
MyBenchmark.requireMethod       avgt   20  5.091 � 0.008  ns/op

要在本地复制:

  1. 创建一个包含上述基准的 Maven 项目。

  2. 添加如下依赖:

     <dependency>
         <groupId>org.bitbucket.cowwoc</groupId>
         <artifactId>requirements</artifactId>
         <version>2.0.0</version>
     </dependency>
    
  3. 或者,从 https://github.com/cowwoc/requirements.java/

    下载库

我有以下问题:

  1. 你能重现这个结果吗?
  2. 基准测试有什么问题(如果有的话)?

更新:我post根据https://bitbucket.org/cowwoc/requirements/downloads per Aleksey Shipilev的建议更新了基准源代码、基准输出、jmh-test输出和xperfasm输出.由于问题的 30k 字符限制,我不能 post 这些到 Whosebug。

UPDATE2:我终于得到了一致的、有意义的结果。

Benchmark                  Mode  Cnt   Score   Error  Units
MyBenchmark.assertMethod   avgt   60  22.552 ± 0.020  ns/op
MyBenchmark.requireMethod  avgt   60  22.411 ± 0.114  ns/op

consistent,我的意思是我在 运行 秒内得到几乎相同的值。

meaningful,我的意思是 assertMethod()requireMethod() 慢。

我做了以下修改:

有没有人能够在不翻倍 运行 次的情况下取得这些结果?

UPDATE3:禁用内联会产生相同的结果,而不会明显降低性能。我 post 编辑了一个更详细的答案

您通过指定 forks(1) 运行在单个 VM 进程中进行测试。在 运行 时间内,虚拟机会查看您的代码并尝试弄清楚它是如何实际执行的。然后,它会根据观察到的行为创建所谓的配置文件来优化您的应用程序。

这里最有可能发生的事情称为配置文件污染,其中 运行第一个基准会对第二个基准的结果产生影响。过于简单化:如果您的 VM 通过 运行 其基准训练来很好地完成 (a),那么它需要一些额外的时间才能习惯之后做 (b)。因此,(b) 似乎需要更多时间。

为了避免这种情况,运行 你的基准测试有多个分支,其中不同的基准测试是 运行 在新的 VM 进程上,以避免这种配置文件污染。您可以在 are provided by JMH.

的示例中阅读有关分叉的更多信息

您还应该检查 the sample on state;您不应将输入作为常量引用,而应让 JMH 处理值的转义以应用实际计算。

我猜想 - 如果应用得当 - 两个基准会产生相似的 运行时间。

更新 - 这是我得到的固定基准:

Benchmark                  Mode  Cnt   Score   Error  Units
MyBenchmark.assertMethod   avgt   40  17,592 ± 1,493  ns/op
MyBenchmark.requireMethod  avgt   40  17,999 ± 0,920  ns/op

为了完成,我还 运行 使用 perfasm 进行基准测试,这两种方法基本上编译成相同的东西。

这在微基准测试中非常常见。当我下载你的代码时,我得到了相同的结果,但对于其他数字,显然我的电脑比你的慢。但是,如果我修改您的源代码以使用 5 次分叉、100 次预热迭代和 20 次测量迭代,那么 requireMethod 将变得比预期的 assertMethod 快一点。

JMH 很棒,但是很容易编写看起来不错的测试,但由于迭代次数太少,您不能相信结果。

回答我自己的问题:

内联似乎在扭曲结果。我需要做的就是获得一致、有意义的结果如下:

  • 锁定 CPU 时钟(min/max CPU 在 Windows 电源选项中设置为 99%)
  • 通过使用 @CompilerControl(CompilerControl.Mode.DONT_INLINE).
  • 注释两种方法来禁用内联

我现在得到以下结果:

Benchmark                  Mode  Cnt   Score   Error  Units
MyBenchmark.assertMethod   avgt  200  11.462 ± 0.048  ns/op
MyBenchmark.requireMethod  avgt  200  11.138 ± 0.062  ns/op

我尝试分析 -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining 的输出,但没有发现任何错误。这两种方法似乎都以相同的方式内联。 <耸肩>


基准源代码是:

import java.util.concurrent.TimeUnit;
import static org.bitbucket.cowwoc.requirements.Requirements.assertThat;
import static org.bitbucket.cowwoc.requirements.Requirements.requireThat;
import org.bitbucket.cowwoc.requirements.StringRequirements;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

@State(Scope.Benchmark)
public class MyBenchmark {

    private String name = "name";
    private String value = "value";

    @Benchmark
    public void emptyMethod() {
    }

    // Inlining leads to unexpected results: 
    @Benchmark
    @CompilerControl(CompilerControl.Mode.DONT_INLINE)
    public StringRequirements assertMethod() {
        return assertThat(value, name).isNotNull().isNotEmpty();
    }

    @Benchmark
    @CompilerControl(CompilerControl.Mode.DONT_INLINE)
    public StringRequirements requireMethod() {
        return requireThat(value, name).isNotNull().isNotEmpty();
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(MyBenchmark.class.getSimpleName())
                .jvmArgsAppend("-ea")
                .forks(3)
                .timeUnit(TimeUnit.NANOSECONDS)
                .mode(Mode.AverageTime)
                .build();

        new Runner(opt).run();
    }
}

更新apangin seems to have 为什么 assertMethod()requireMethod().

在这种特殊情况下,由于寄存器分配问题,assertMethod 确实比 requireMethod 编译得更好。

基准看起来是正确的,我可以始终如一地重现您的结果。
分析一下我做的问题the simplified benchmark:

package bench;

import com.google.common.collect.ImmutableMap;
import org.openjdk.jmh.annotations.*;

@State(Scope.Benchmark)
public class Requirements {
    private static boolean enabled = true;

    private String name = "name";
    private String value = "value";

    @Benchmark
    public Object assertMethod() {
        if (enabled)
            return requireThat(value, name);
        return null;
    }

    @Benchmark
    public Object requireMethod() {
        return requireThat(value, name);
    }

    public static Object requireThat(String parameter, String name) {
        if (name.trim().isEmpty())
            throw new IllegalArgumentException();
        return new StringRequirementsImpl(parameter, name, new Configuration());
    }

    static class Configuration {
        private Object context = ImmutableMap.of();
    }

    static class StringRequirementsImpl {
        private String parameter;
        private String name;
        private Configuration config;
        private ObjectRequirementsImpl asObject;

        StringRequirementsImpl(String parameter, String name, Configuration config) {
            this.parameter = parameter;
            this.name = name;
            this.config = config;
            this.asObject = new ObjectRequirementsImpl(parameter, name, config);
        }
    }

    static class ObjectRequirementsImpl {
        private Object parameter;
        private String name;
        private Configuration config;

        ObjectRequirementsImpl(Object parameter, String name, Configuration config) {
            this.parameter = parameter;
            this.name = name;
            this.config = config;
        }
    }
}

首先,我已经通过 -XX:+PrintInlining 验证了整个基准测试被内联到一个大方法中。显然这个编译单元有很多节点,并且没有足够的 CPU 寄存器来保存所有的中间变量。也就是说,编译器需要 spill 其中一些。

  • assertMethod4 registers 在调用 trim() 之前溢出到堆栈。
  • requireMethod之后7 registers都溢出了,以后调用到new Configuration().

-XX:+PrintAssembly 输出:

  assertMethod             |  requireMethod
  -------------------------|------------------------
  mov    %r11d,0x5c(%rsp)  |  mov    %rcx,0x20(%rsp)
  mov    %r10d,0x58(%rsp)  |  mov    %r11,0x48(%rsp)
  mov    %rbp,0x50(%rsp)   |  mov    %r10,0x30(%rsp)
  mov    %rbx,0x48(%rsp)   |  mov    %rbp,0x50(%rsp)
                           |  mov    %r9d,0x58(%rsp)
                           |  mov    %edi,0x5c(%rsp)
                           |  mov    %r8,0x60(%rsp) 

这几乎是两种编译方法除了if (enabled)检查的唯一区别。因此,性能差异可以用更多变量溢出到内存来解释。

为什么较小的方法编译的不是最优的?好吧,众所周知,寄存器分配问题是 NP 完全问题。由于无法在合理的时间内理想地解决,因此编译器通常依赖于某些启发式方法。在大方法中,像额外 if 这样的小东西可能会显着改变寄存器分配算法的结果。

不过您不必担心。我们看到的效果并不意味着 requireMethod 总是编译得更糟。在其他用例中,由于内联,编译图将完全不同。无论如何,1纳秒的差异对于实际应用程序性能来说是没有意义的。