ERROR: org.openjdk.jmh.runner.RunnerException: ERROR: Exception while trying to acquire the JMH lock (C:\WINDOWS\/jmh.lock): 拒绝访问。, exiting. Use -Djmh.ignoreLock=true to forcefully continue.
at org.openjdk.jmh.runner.Runner.run(Runner.java:216)
at org.openjdk.jmh.Main.main(Main.java:71)
RunConfiguration -> Environment Variables -> include system environment viables
package com.yangsx95.test;
import org.openjdk.jmh.annotations.Benchmark;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* @author yangsx
* @version 1.0
* @date 2019/10/20
*/
public class Test {
static List<Integer> nums = new ArrayList<>();
static {
Random r = new Random();
for (int i = 0; i < 10000; i++) nums.add(1000000 + r.nextInt(1000000));
}
static void foreach() {
nums.forEach(v -> isPrime(v));
}
static void parallel() {
nums.parallelStream().forEach(Test::isPrime);
}
static boolean isPrime(int num) {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) return false;
}
return true;
}
// 测试foreach方法
@Benchmark
public void testForEach(){
Test.foreach();
}
// 测试parallel方法
@Benchmark
public void testParallel() {
Test.parallel();
}
}
# JMH version: 1.21 --JMH版本
# VM version: JDK 1.8.0_112, Java HotSpot(TM) 64-Bit Server VM, 25.112-b15 --JDK,JVM信息
# VM invoker: D:\java\jdk1.8-64-win\jre\bin\java.exe --JVM路径
# VM options: -Dfile.encoding=UTF-8 --JVM参数
# Warmup: 5 iterations, 10 s each --预热操作,每隔10s循环调用方法五次完成预热
# Measurement: 5 iterations, 10 s each -- 测试也是每隔10s调用方法五次完成测试
# Timeout: 10 min per iteration --超时:如果每次迭代超过10分钟,就算超时了
# Threads: 1 thread, will synchronize iterations -- 一个线程,进行同步迭代(可多个线程并发迭代)
# Benchmark mode: Throughput, ops/time --Benchmark的模式为Throughput(吞吐量,即每秒操作次数,也就是此方法每秒执行多少次)
# Benchmark: com.yangsx95.test.Test.testForEach --所测试的方法
# Run progress: 0.00% complete, ETA 00:08:20 --运行进度,开始时间
# Fork: 1 of 5 --第一次迭代,并且循环预热5此,循环调用5次
# Warmup Iteration 1: 0.865 ops/s --第一个10s预热
# Warmup Iteration 2: 0.854 ops/s --第二个10s
# Warmup Iteration 3: 0.871 ops/s
# Warmup Iteration 4: 0.872 ops/s
# Warmup Iteration 5: 0.872 ops/s
Iteration 1: 0.875 ops/s --开始测试,每秒大约调用foreach方法0.875次
Iteration 2: 0.874 ops/s
Iteration 3: 0.869 ops/s
Iteration 4: 0.872 ops/s
Iteration 5: 0.865 ops/s
# Run progress: 20.00% complete, ETA 00:06:56
# Fork: 2 of 5
# Warmup Iteration 1: 0.821 ops/s
# Warmup Iteration 2: 0.824 ops/s
# Warmup Iteration 3: 0.240 ops/s
# Warmup Iteration 4: 0.144 ops/s
# Warmup Iteration 5: 0.154 ops/s
Iteration 1: 0.385 ops/s
Iteration 2: 0.820 ops/s
Iteration 3: 0.821 ops/s
Iteration 4: 0.822 ops/s
Iteration 5: 0.824 ops/s
# Run progress: 40.00% complete, ETA 00:05:35
# Fork: 3 of 5
# Warmup Iteration 1: 0.865 ops/s
# Warmup Iteration 2: 0.863 ops/s
# Warmup Iteration 3: 0.862 ops/s
# Warmup Iteration 4: 0.861 ops/s
# Warmup Iteration 5: 0.862 ops/s
Iteration 1: 0.863 ops/s
Iteration 2: 0.863 ops/s
Iteration 3: 0.861 ops/s
Iteration 4: 0.865 ops/s
Iteration 5: 0.863 ops/s
# Run progress: 60.00% complete, ETA 00:03:39
# Fork: 4 of 5
# Warmup Iteration 1: 0.879 ops/s
# Warmup Iteration 2: 0.877 ops/s
# Warmup Iteration 3: 0.884 ops/s
# Warmup Iteration 4: 0.881 ops/s
# Warmup Iteration 5: 0.882 ops/s
Iteration 1: 0.883 ops/s
Iteration 2: 0.881 ops/s
Iteration 3: 0.878 ops/s
Iteration 4: 0.884 ops/s
Iteration 5: 0.883 ops/s
# Run progress: 80.00% complete, ETA 00:01:47
# Fork: 5 of 5
# Warmup Iteration 1: 0.840 ops/s
# Warmup Iteration 2: 0.832 ops/s
# Warmup Iteration 3: 0.826 ops/s
# Warmup Iteration 4: 0.829 ops/s
# Warmup Iteration 5: 0.830 ops/s
Iteration 1: 0.829 ops/s
Iteration 2: 0.825 ops/s
Iteration 3: 0.825 ops/s
Iteration 4: 0.825 ops/s
Iteration 5: 0.831 ops/s
--测试结果为
Result "com.yangsx95.test.Test.testForEach":
0.835 ±(99.9%) 0.073 ops/s [Average] --平均值为0.835,99.9%的波动在0.073内
(min, avg, max) = (0.385, 0.835, 0.884), stdev = 0.097 --最小、平均、最大吞吐量
CI (99.9%): [0.763, 0.908] (assumes normal distribution)
# Run complete. Total time: 00:08:59 --总共用时大约9分钟
REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on
why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial
experiments, perform baseline and negative tests that provide experimental control, make sure
the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.
Do not assume the numbers tell you what you want them to tell.
-- 测试结果,Cnt总共执行次数 5*5=25次,得分为 0.835, 波动在 ± 0.073 内
Benchmark Mode Cnt Score Error Units
Test.testForEach thrpt 25 0.835 ± 0.073 ops/s