* initial commit

* changes for articlesBAEL-3380

* Removed evaluation article files

* BAEL-3380

* adding test case BAEL-3380

* BAEL-3380

* BAEL-3380 - updated tests and removed main class

* BAEL-3380 - Updated Test class name

* BAEL-3380 - Updated Test class name

* BAEL-3380 - Updated testcase names as per BDD naming convention

* BAEL 3380 - Added JMH benchmark
This commit is contained in:
rohitchaudhary2023 2020-01-01 02:52:53 +05:30 committed by maibin
parent 70aa0ba234
commit ce26810b4a
2 changed files with 107 additions and 0 deletions

View File

@ -19,6 +19,16 @@
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version> <version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
</dependency> </dependency>
<!-- test scoped --> <!-- test scoped -->
<dependency> <dependency>
@ -37,9 +47,34 @@
<filtering>true</filtering> <filtering>true</filtering>
</resource> </resource>
</resources> </resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>benchmarks</finalName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jmh.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build> </build>
<properties> <properties>
<jmh.version>1.19</jmh.version>
<!-- util --> <!-- util -->
<commons-lang3.version>3.9</commons-lang3.version> <commons-lang3.version>3.9</commons-lang3.version>
<!-- testing --> <!-- testing -->

View File

@ -0,0 +1,72 @@
package com.baeldung.arraysort;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 5)
@Measurement(iterations = 10)
@Fork(2)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class ArraySortingBenchmark {
@State(Scope.Benchmark)
public static class ArrayContainer {
@Param({ "1000", "10000", "100000", "1000000" })
int arraySize;
// initial unsorted array
int[] unsortedArray;
//cloned array to sort
int[] arrayToSort;
@Setup(Level.Trial)
public void createUnSortedArray() {
unsortedArray = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
unsortedArray[i] = new Random().nextInt(1000);
}
}
@Setup(Level.Invocation)
public void createUnSortedArrayCopy() {
arrayToSort = unsortedArray.clone();
}
int[] getArrayToSort() {
return arrayToSort;
}
}
@Benchmark
public void benchmark_arrays_parallel_sort(ArrayContainer d, Blackhole b) {
int[] arr = d.getArrayToSort();
Arrays.parallelSort(arr);
b.consume(arr);
}
@Benchmark
public void benchmark_arrays_sort(ArrayContainer d, Blackhole b) {
int[] arr = d.getArrayToSort();
Arrays.sort(arr);
b.consume(arr);
}
}