Bael 2052 (#5051)
* collections benchmark tests * more benchmark tests * set benchmark tests * include linkedlist in benchmark tests * Update ArrayListBenchmark.java
This commit is contained in:
parent
4bb6592cd9
commit
e88ef76805
|
@ -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<Employee> employeeList = new ArrayList<>();
|
||||||
|
//LinkedList<Employee> 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<Employee> 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,6 +10,22 @@ public class Employee {
|
||||||
this.id = id;
|
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
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
|
|
|
@ -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<Long, Employee> employeeMap = new HashMap<>();
|
||||||
|
//LinkedHashMap<Long, Employee> employeeMap = new LinkedHashMap<>();
|
||||||
|
//IdentityHashMap<Long, Employee> employeeMap = new IdentityHashMap<>();
|
||||||
|
//WeakHashMap<Long, Employee> employeeMap = new WeakHashMap<>();
|
||||||
|
//ConcurrentHashMap<Long, Employee> employeeMap = new ConcurrentHashMap<>();
|
||||||
|
//ConcurrentSkipListMap<Long, Employee> 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<Employee> employeeSet = new HashSet<>();
|
||||||
|
LinkedHashSet<Employee> employeeSet = new LinkedHashSet<>();
|
||||||
|
//ConcurrentSkipListSet<Employee> 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue