From e88ef768054c0da934645142da767f47eedb8178 Mon Sep 17 00:00:00 2001 From: Mher Baghinyan Date: Tue, 4 Sep 2018 10:38:36 +0400 Subject: [PATCH] Bael 2052 (#5051) * collections benchmark tests * more benchmark tests * set benchmark tests * include linkedlist in benchmark tests * Update ArrayListBenchmark.java --- .../performance/ArrayListBenchmark.java | 77 ++++++++++++++++++ .../performance/CopyOnWriteBenchmark.java | 78 +++++++++++++++++++ .../com/baeldung/performance/Employee.java | 16 ++++ .../performance/HashMapBenchmark.java | 73 +++++++++++++++++ .../baeldung/performance/SetBenchMark.java | 66 ++++++++++++++++ 5 files changed, 310 insertions(+) create mode 100644 core-java-collections/src/main/java/com/baeldung/performance/ArrayListBenchmark.java create mode 100644 core-java-collections/src/main/java/com/baeldung/performance/CopyOnWriteBenchmark.java create mode 100644 core-java-collections/src/main/java/com/baeldung/performance/HashMapBenchmark.java create mode 100644 core-java-collections/src/main/java/com/baeldung/performance/SetBenchMark.java diff --git a/core-java-collections/src/main/java/com/baeldung/performance/ArrayListBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/ArrayListBenchmark.java new file mode 100644 index 0000000000..dddd85007d --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/performance/ArrayListBenchmark.java @@ -0,0 +1,77 @@ +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.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 10) +public class ArrayListBenchmark { + + @State(Scope.Thread) + public static class MyState { + + List employeeList = new ArrayList<>(); + //LinkedList employeeList = new LinkedList<>(); + + long iterations = 100000; + + Employee employee = new Employee(100L, "Harry"); + + int employeeIndex = -1; + + @Setup(Level.Trial) + public void setUp() { + for (long i = 0; i < iterations; i++) { + employeeList.add(new Employee(i, "John")); + } + + employeeList.add(employee); + employeeIndex = employeeList.indexOf(employee); + } + } + + @Benchmark + public void testAddAt(ArrayListBenchmark.MyState state) { + state.employeeList.add((int) (state.iterations), new Employee(state.iterations, "John")); + } + + @Benchmark + public boolean testContains(ArrayListBenchmark.MyState state) { + return state.employeeList.contains(state.employee); + } + + @Benchmark + public int testIndexOf(ArrayListBenchmark.MyState state) { + return state.employeeList.indexOf(state.employee); + } + + @Benchmark + public Employee testGet(ArrayListBenchmark.MyState state) { + return state.employeeList.get(state.employeeIndex); + } + + @Benchmark + public boolean testRemove(ArrayListBenchmark.MyState state) { + return state.employeeList.remove(state.employee); + } + +// @Benchmark +// public void testAdd(ArrayListBenchmark.MyState state) { +// state.employeeList.add(new Employee(state.iterations + 1, "John")); +// } + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(ArrayListBenchmark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/performance/CopyOnWriteBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/CopyOnWriteBenchmark.java new file mode 100644 index 0000000000..30c50291dd --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/performance/CopyOnWriteBenchmark.java @@ -0,0 +1,78 @@ +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.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 10) +public class CopyOnWriteBenchmark { + + @State(Scope.Thread) + public static class MyState { + + CopyOnWriteArrayList employeeList = new CopyOnWriteArrayList<>(); + + long iterations = 100000; + + Employee employee = new Employee(100L, "Harry"); + + int employeeIndex = -1; + + @Setup(Level.Trial) + public void setUp() { + for (long i = 0; i < iterations; i++) { + employeeList.add(new Employee(i, "John")); + } + + employeeList.add(employee); + + employeeIndex = employeeList.indexOf(employee); + } + } + + @Benchmark + public void testAdd(CopyOnWriteBenchmark.MyState state) { + state.employeeList.add(new Employee(state.iterations + 1, "John")); + } + + @Benchmark + public void testAddAt(CopyOnWriteBenchmark.MyState state) { + state.employeeList.add((int) (state.iterations), new Employee(state.iterations, "John")); + } + + @Benchmark + public boolean testContains(CopyOnWriteBenchmark.MyState state) { + return state.employeeList.contains(state.employee); + } + + @Benchmark + public int testIndexOf(CopyOnWriteBenchmark.MyState state) { + return state.employeeList.indexOf(state.employee); + } + + @Benchmark + public Employee testGet(CopyOnWriteBenchmark.MyState state) { + return state.employeeList.get(state.employeeIndex); + } + + @Benchmark + public boolean testRemove(CopyOnWriteBenchmark.MyState state) { + return state.employeeList.remove(state.employee); + } + + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(CopyOnWriteBenchmark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} 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 index daa68ae2f1..d811cfbb7d 100644 --- a/core-java-collections/src/main/java/com/baeldung/performance/Employee.java +++ b/core-java-collections/src/main/java/com/baeldung/performance/Employee.java @@ -10,6 +10,22 @@ public class Employee { this.id = id; } + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/core-java-collections/src/main/java/com/baeldung/performance/HashMapBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/HashMapBenchmark.java new file mode 100644 index 0000000000..085daaceb7 --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/performance/HashMapBenchmark.java @@ -0,0 +1,73 @@ +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.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 10) +public class HashMapBenchmark { + + @State(Scope.Thread) + public static class MyState { + + Map employeeMap = new HashMap<>(); + //LinkedHashMap employeeMap = new LinkedHashMap<>(); + //IdentityHashMap employeeMap = new IdentityHashMap<>(); + //WeakHashMap employeeMap = new WeakHashMap<>(); + //ConcurrentHashMap employeeMap = new ConcurrentHashMap<>(); + //ConcurrentSkipListMap employeeMap = new ConcurrentSkipListMap <>(); + + // TreeMap + + long iterations = 100000; + + Employee employee = new Employee(100L, "Harry"); + + int employeeIndex = -1; + + @Setup(Level.Trial) + public void setUp() { + for (long i = 0; i < iterations; i++) { + employeeMap.put(i, new Employee(i, "John")); + } + + //employeeMap.put(iterations, employee); + } + } + + @Benchmark + public Employee testGet(HashMapBenchmark.MyState state) { + return state.employeeMap.get(state.iterations); + } + + @Benchmark + public Employee testRemove(HashMapBenchmark.MyState state) { + return state.employeeMap.remove(state.iterations); + } + + @Benchmark + public Employee testPut(HashMapBenchmark.MyState state) { + return state.employeeMap.put(state.employee.getId(), state.employee); + } + + @Benchmark + public Boolean testContainsKey(HashMapBenchmark.MyState state) { + return state.employeeMap.containsKey(state.employee.getId()); + } + + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(HashMapBenchmark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/performance/SetBenchMark.java b/core-java-collections/src/main/java/com/baeldung/performance/SetBenchMark.java new file mode 100644 index 0000000000..f70bab4fcf --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/performance/SetBenchMark.java @@ -0,0 +1,66 @@ +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.HashSet; +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.TreeSet; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 10) +public class SetBenchMark { + + @State(Scope.Thread) + public static class MyState { + + //Set employeeSet = new HashSet<>(); + LinkedHashSet employeeSet = new LinkedHashSet<>(); + //ConcurrentSkipListSet employeeSet = new ConcurrentSkipListSet <>(); + + // TreeSet  + + long iterations = 1000; + Employee employee = new Employee(100L, "Harry"); + + @Setup(Level.Trial) + public void setUp() { + for (long i = 0; i < iterations; i++) { + employeeSet.add(new Employee(i, "John")); + } + + //employeeSet.add(employee); + } + } + + @Benchmark + public boolean testAdd(SetBenchMark.MyState state) { + return state.employeeSet.add(state.employee); + } + + @Benchmark + public Boolean testContains(SetBenchMark.MyState state) { + return state.employeeSet.contains(state.employee); + } + + @Benchmark + public boolean testRemove(SetBenchMark.MyState state) { + return state.employeeSet.remove(state.employee); + } + + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(SetBenchMark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +}