java微基准测试从列表中查找平均值

java微基准测试从列表中查找平均值,java,performance,benchmarking,jmh,Java,Performance,Benchmarking,Jmh,我有一些不同字符串的文件(大约100.000取自prod)。需要找出99%,99.9%的函数来处理该文件中的每个字符串 我尝试使用jmh编写基准测试。然而,我只能找到批处理函数(处理整个文件)所需的百分位数,或者只找到一个特定字符串所需的百分位数 public String process1(String str){ ...process... } public String processBatch(List<String> strings){ for (Stri

我有一些不同字符串的文件(大约100.000取自prod)。需要找出99%,99.9%的函数来处理该文件中的每个字符串

我尝试使用jmh编写基准测试。然而,我只能找到批处理函数(处理整个文件)所需的百分位数,或者只找到一个特定字符串所需的百分位数

public String process1(String str){
    ...process...
}

public String processBatch(List<String> strings){
    for (String str: strings){
        process1(str)
    }
}
公共字符串进程1(字符串str){
过程
}
公共字符串processBatch(列表字符串){
for(字符串str:strings){
进程1(str)
}
}
此外,我还尝试通过@param设置整个字符串列表。这使得jmh可以为每个字符串运行几十次迭代,但不能找到所需的结果


jmh中是否有任何东西可以帮助您找到所需的统计信息?如果没有,可以使用什么工具进行此操作?

这是您正在寻找的工具吗

@Warmup(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@State(Scope.Benchmark)
public class MyBenchmark {

    ClassUnderBenchmark classUnderBenchmark = new ClassUnderBenchmark();

    @State(Scope.Benchmark)
    public static class MyTestState {

        int counter = 0;
        List<String> list = Arrays.asList("aaaaa", "bbbb", "ccc");
        String currentString;

        @Setup(Level.Invocation)
        public void init() throws IOException {
            this.currentString = list.get(counter++);
            if (counter == 3) {
                counter = 0;
            }
        }
    }

    @Benchmark
    @Threads(1)
    @BenchmarkMode(Mode.SampleTime)
    public void test(MyBenchmark.MyTestState myTestState) {
        classUnderBenchmark.toUpper(myTestState.currentString);
    }

    public static class ClassUnderBenchmark {

        Random r = new Random();

        public String toUpper(String name) {
            try {
                Thread.sleep(r.nextInt(100));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return name.toUpperCase();
        }
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(MyBenchmark.class.getSimpleName())
                .jvmArgs("-XX:+UseG1GC", "-XX:MaxGCPauseMillis=50")
                .build();
        new Runner(opt).run();
    }
}

这就是你要找的吗

@Warmup(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@State(Scope.Benchmark)
public class MyBenchmark {

    ClassUnderBenchmark classUnderBenchmark = new ClassUnderBenchmark();

    @State(Scope.Benchmark)
    public static class MyTestState {

        int counter = 0;
        List<String> list = Arrays.asList("aaaaa", "bbbb", "ccc");
        String currentString;

        @Setup(Level.Invocation)
        public void init() throws IOException {
            this.currentString = list.get(counter++);
            if (counter == 3) {
                counter = 0;
            }
        }
    }

    @Benchmark
    @Threads(1)
    @BenchmarkMode(Mode.SampleTime)
    public void test(MyBenchmark.MyTestState myTestState) {
        classUnderBenchmark.toUpper(myTestState.currentString);
    }

    public static class ClassUnderBenchmark {

        Random r = new Random();

        public String toUpper(String name) {
            try {
                Thread.sleep(r.nextInt(100));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return name.toUpperCase();
        }
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(MyBenchmark.class.getSimpleName())
                .jvmArgs("-XX:+UseG1GC", "-XX:MaxGCPauseMillis=50")
                .build();
        new Runner(opt).run();
    }
}

@谢谢,我一开始就查过了。但找不到如何运行特定benchmark@CConard96谢谢,我一开始就查过了。但未能找到如何运行特定基准测试的信息
Result "test":

  N = 91
  mean =      0,056 ±(99.9%) 0,010 s/op

  Histogram, s/op:
    [0,000, 0,010) = 6 
    [0,010, 0,020) = 9 
    [0,020, 0,030) = 3 
    [0,030, 0,040) = 11 
    [0,040, 0,050) = 8 
    [0,050, 0,060) = 11 
    [0,060, 0,070) = 9 
    [0,070, 0,080) = 9 
    [0,080, 0,090) = 14 

  Percentiles, s/op:
      p(0,0000) =      0,003 s/op
     p(50,0000) =      0,059 s/op
     p(90,0000) =      0,092 s/op
     p(95,0000) =      0,095 s/op
     p(99,0000) =      0,100 s/op
     p(99,9000) =      0,100 s/op
     p(99,9900) =      0,100 s/op
     p(99,9990) =      0,100 s/op
     p(99,9999) =      0,100 s/op
    p(100,0000) =      0,100 s/op


Benchmark           Mode  Cnt  Score   Error  Units
MyBenchmark.test  sample   91  0,056 ± 0,010   s/op