0

According to this topic: Collectors.mapping vs conventional 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. In connection with the above, I created two helper methods that will supply stab data to my fake container.

  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.

  @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. Moreover In the long term if I will increase @RepeatedTest param to calculate the closest result this will be a little bit tedious. I want to know if it is possible to handle the duration param timeElapsed and calculate the average value for n repeated tests.

Martin
  • 1,139
  • 4
  • 23
  • 49

1 Answers1

0

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.
The best tool for such a test is probably JMH (https://openjdk.java.net/projects/code-tools/jmh/).

jbleduigou
  • 139
  • 7