简体   繁体   English

如何处理重复测试的持续时间并计算它们的平均时间

[英]How to handle duration of repeated tests and calculate their average time

According to this topic: Collectors.mapping vs conventional Stream.map根据这个主题: Collectors.mapping 与常规 Stream.map

I decided to conduct my own provisional test cases to find out an approximate performance difference between Collectors.mapping and Stream.map operations.我决定进行自己的临时测试用例,以找出Collectors.mappingStream.map操作之间的大致性能差异。 In connection with the above, I created two helper methods that will supply stab data to my fake container.结合上述内容,我创建了两个辅助方法,它们将为我的假容器提供 stab 数据。

  private List<Employee> dataStab() {
    List<Employee> employees = new ArrayList<>();

    for (int i = 0; i < 10000000; i++) {
      Employee employee = randomizeEmployeeParam();
      employees.add(employee);
    }
    return employees;
  }

  private Employee randomizeEmployeeParam() {

    return
        new
            Employee(
            "Employee" + rand.nextInt(3000),
            rand.nextInt(50),
            1000 + (10000 - 1000) * rand.nextDouble());
  }

I created two tests which are conducting accordingly Collectors.mapping and Stream.map operations.我创建了两个测试,分别执行Collectors.mappingStream.map操作。

  @RepeatedTest(10)
  void collectorsMapping() {
    Instant start = Instant.now();
    List<String> strings = employeeService.collectorsMapping(dataStab());
    Instant finish = Instant.now();
    long timeElapsed = Duration.between(start, finish).toMillis();
    System.out.println(timeElapsed);
    assertEquals(10000000, strings.size());
  }

  @RepeatedTest(10)
  void streamMapAndCollect() {
    Instant start = Instant.now();
    List<String> mapAndCollect = employeeService.streamMapAndCollect(dataStab());
    Instant finish = Instant.now();
    long timeElapsed = Duration.between(start, finish).toMillis();
    System.out.println(timeElapsed);
    assertEquals(10000000, mapAndCollect.size());
  }

Tests work so far but now I suppose to manually calculate an average time of these tests basing on the result in the run window in IntelliJ.到目前为止测试工作,但现在我想根据 IntelliJ 中运行 window 的结果手动计算这些测试的平均时间。 Moreover In the long term if I will increase @RepeatedTest param to calculate the closest result this will be a little bit tedious.此外,从长远来看,如果我增加@RepeatedTest参数来计算最接近的结果,这将有点乏味。 I want to know if it is possible to handle the duration param timeElapsed and calculate the average value for n repeated tests.我想知道是否可以处理持续时间参数timeElapsed并计算 n 次重复测试的平均值。

If I understand correctly you are trying to benchmark two different methods that are doing the same thing.如果我理解正确,您正在尝试对执行相同操作的两种不同方法进行基准测试。
Benchmarking can be tricky to perform especially when it comes to making sure the conditions remain the same during the testing.执行基准测试可能很棘手,尤其是在确保测试期间条件保持不变时。
For instance the JVM might start to optimize some operations after some time.例如,JVM 可能会在一段时间后开始优化某些操作。
The best tool for such a test is probably JMH ( https://openjdk.java.net/projects/code-tools/jmh/ ).进行此类测试的最佳工具可能是 JMH ( https://openjdk.java.net/projects/code-tools/jmh/ )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM