From 2fae0783706a6ef2f070139233fb3373b76a7783 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Fri, 3 Aug 2018 10:32:50 +0400 Subject: [PATCH 1/6] added benchmark tests for set and list performance --- core-java-collections/pom.xml | 18 +++---- .../performance/CollectionsBenchmark.java | 53 +++++++++++++++++++ .../com/baeldung/performance/Employee.java | 31 +++++++++++ 3 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java create mode 100644 core-java-collections/src/main/java/com/baeldung/performance/Employee.java diff --git a/core-java-collections/pom.xml b/core-java-collections/pom.xml index ff06714bfe..f954e8542e 100644 --- a/core-java-collections/pom.xml +++ b/core-java-collections/pom.xml @@ -47,26 +47,26 @@ ${eclipse.collections.version} - org.assertj - assertj-core - ${assertj.version} - test + org.openjdk.jmh + jmh-core + ${openjdk.jmh.version} - org.junit.platform - junit-platform-runner - ${junit.platform.version} - test + org.openjdk.jmh + jmh-generator-annprocess + ${openjdk.jmh.version} + + 1.19 1.2.0 3.5 4.1 4.01 1.7.0 3.6.1 - 9.2.0 + 7.1.0 diff --git a/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java new file mode 100644 index 0000000000..64cff16cd7 --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java @@ -0,0 +1,53 @@ +package com.baeldung.performance; + +import org.openjdk.jmh.annotations.*; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +public class CollectionsBenchmark { + + @State(Scope.Thread) + public static class MyState { + private Set employeeSet = new HashSet<>(); + private List employeeList = new ArrayList<>(); + + private final long m_count = 16; + + private Employee employee = new Employee(100L, "Harry"); + + @Setup(Level.Invocation) + public void setUp() { + + for (long ii = 0; ii < m_count; ii++) { + employeeSet.add(new Employee(ii, "John")); + employeeList.add(new Employee(ii, "John")); + + employeeList.add(employee); + employeeSet.add(employee); + } + } + } + + + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MINUTES) + public boolean testArrayList(MyState state) { + return state.employeeList.contains(state.employee); + } + + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MINUTES) + public boolean testHashSet(MyState state) { + return state.employeeSet.contains(state.employee); + } + + public static void main(String[] args) throws Exception { + org.openjdk.jmh.Main.main(args); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/performance/Employee.java b/core-java-collections/src/main/java/com/baeldung/performance/Employee.java new file mode 100644 index 0000000000..daa68ae2f1 --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/performance/Employee.java @@ -0,0 +1,31 @@ +package com.baeldung.performance; + +public class Employee { + + private Long id; + private String name; + + public Employee(Long id, String name) { + this.name = name; + this.id = id; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Employee employee = (Employee) o; + + if (!id.equals(employee.id)) return false; + return name.equals(employee.name); + + } + + @Override + public int hashCode() { + int result = id.hashCode(); + result = 31 * result + name.hashCode(); + return result; + } +} From adb14c2b95dff9ef5aeab3701e26eefb4fb3e66a Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Fri, 3 Aug 2018 10:43:37 +0400 Subject: [PATCH 2/6] bring back dependencies --- core-java-collections/pom.xml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/core-java-collections/pom.xml b/core-java-collections/pom.xml index f954e8542e..f9c2ec8249 100644 --- a/core-java-collections/pom.xml +++ b/core-java-collections/pom.xml @@ -46,6 +46,18 @@ eclipse-collections ${eclipse.collections.version} + + org.assertj + assertj-core + ${assertj.version} + test + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + org.openjdk.jmh jmh-core From dccf0c3b0bbc77536666ecba3554d7c29ca1ecee Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Fri, 10 Aug 2018 17:50:13 +0400 Subject: [PATCH 3/6] bring back dependencies --- .../performance/CollectionsBenchmark.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java index 64cff16cd7..ff1e8d7953 100644 --- a/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java +++ b/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java @@ -1,6 +1,9 @@ package com.baeldung.performance; import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; import java.util.ArrayList; import java.util.HashSet; @@ -15,14 +18,14 @@ public class CollectionsBenchmark { private Set employeeSet = new HashSet<>(); private List employeeList = new ArrayList<>(); - private final long m_count = 16; + private long iterations = 10000; private Employee employee = new Employee(100L, "Harry"); - @Setup(Level.Invocation) + @Setup(Level.Trial) public void setUp() { - for (long ii = 0; ii < m_count; ii++) { + for (long ii = 0; ii < iterations; ii++) { employeeSet.add(new Employee(ii, "John")); employeeList.add(new Employee(ii, "John")); @@ -35,19 +38,26 @@ public class CollectionsBenchmark { @Benchmark @BenchmarkMode(Mode.AverageTime) - @OutputTimeUnit(TimeUnit.MINUTES) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + @Warmup(iterations = 1) public boolean testArrayList(MyState state) { return state.employeeList.contains(state.employee); } @Benchmark @BenchmarkMode(Mode.AverageTime) - @OutputTimeUnit(TimeUnit.MINUTES) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + @Warmup(iterations = 1) public boolean testHashSet(MyState state) { return state.employeeSet.contains(state.employee); } public static void main(String[] args) throws Exception { - org.openjdk.jmh.Main.main(args); + Options options = new OptionsBuilder() + .include(CollectionsBenchmark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); } } From 5b9df5d901bc8768fcdcea181c3eec5e25680c9e Mon Sep 17 00:00:00 2001 From: Mher Baghinyan Date: Sat, 11 Aug 2018 17:52:08 +0400 Subject: [PATCH 4/6] Update CollectionsBenchmark.java --- .../java/com/baeldung/performance/CollectionsBenchmark.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java index ff1e8d7953..e5b7787438 100644 --- a/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java +++ b/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java @@ -28,10 +28,10 @@ public class CollectionsBenchmark { for (long ii = 0; ii < iterations; ii++) { employeeSet.add(new Employee(ii, "John")); employeeList.add(new Employee(ii, "John")); - - employeeList.add(employee); - employeeSet.add(employee); } + + employeeList.add(employee); + employeeSet.add(employee); } } From 224dce952b93153bb8c96eb0c2d0a2b4885e2b69 Mon Sep 17 00:00:00 2001 From: Mher Baghinyan Date: Sat, 11 Aug 2018 18:06:21 +0400 Subject: [PATCH 5/6] Update CollectionsBenchmark.java --- .../java/com/baeldung/performance/CollectionsBenchmark.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java index e5b7787438..6d36789c2b 100644 --- a/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java +++ b/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java @@ -25,9 +25,9 @@ public class CollectionsBenchmark { @Setup(Level.Trial) public void setUp() { - for (long ii = 0; ii < iterations; ii++) { - employeeSet.add(new Employee(ii, "John")); - employeeList.add(new Employee(ii, "John")); + for (long i = 0; i < iterations; i++) { + employeeSet.add(new Employee(i, "John")); + employeeList.add(new Employee(i, "John")); } employeeList.add(employee); From 8bcc305853240caa87ea7c9a8fe5ba9f931d48f9 Mon Sep 17 00:00:00 2001 From: Mher Baghinyan Date: Mon, 13 Aug 2018 17:12:48 +0400 Subject: [PATCH 6/6] Update CollectionsBenchmark.java --- .../com/baeldung/performance/CollectionsBenchmark.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java index 6d36789c2b..921e1608ea 100644 --- a/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java +++ b/core-java-collections/src/main/java/com/baeldung/performance/CollectionsBenchmark.java @@ -11,6 +11,9 @@ import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Warmup(iterations = 5) public class CollectionsBenchmark { @State(Scope.Thread) @@ -35,19 +38,12 @@ public class CollectionsBenchmark { } } - @Benchmark - @BenchmarkMode(Mode.AverageTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - @Warmup(iterations = 1) public boolean testArrayList(MyState state) { return state.employeeList.contains(state.employee); } @Benchmark - @BenchmarkMode(Mode.AverageTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - @Warmup(iterations = 1) public boolean testHashSet(MyState state) { return state.employeeSet.contains(state.employee); }