Skip to content

Commit ccca7d0

Browse files
committed
2 04 JMH Benchmark
1 parent 98b9e16 commit ccca7d0

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<plugin>
2525
<groupId>org.apache.maven.plugins</groupId>
2626
<artifactId>maven-compiler-plugin</artifactId>
27-
<version>3.1</version>
27+
<version>3.7.0</version>
2828
<configuration>
2929
<source>${java.version}</source>
3030
<target>${java.version}</target>
@@ -34,6 +34,17 @@
3434
</build>
3535

3636
<dependencies>
37+
<dependency>
38+
<groupId>org.openjdk.jmh</groupId>
39+
<artifactId>jmh-core</artifactId>
40+
<version>RELEASE</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.openjdk.jmh</groupId>
44+
<artifactId>jmh-generator-annprocess</artifactId>
45+
<version>RELEASE</version>
46+
<scope>provided</scope>
47+
</dependency>
3748
</dependencies>
3849

3950
<profiles>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package ru.javaops.masterjava.matrix;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.TimeUnit;
8+
9+
@Warmup(iterations = 10)
10+
@Measurement(iterations = 10)
11+
@BenchmarkMode({Mode.SingleShotTime})
12+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
13+
@State(Scope.Benchmark)
14+
@Threads(1)
15+
@Fork(10)
16+
@Timeout(time = 5, timeUnit = TimeUnit.MINUTES)
17+
public class MatrixBenchmark {
18+
19+
// Matrix size
20+
private static final int MATRIX_SIZE = 1000;
21+
22+
@Param({"3", "4", "10"})
23+
private int threadNumber;
24+
25+
private static int[][] matrixA;
26+
private static int[][] matrixB;
27+
28+
@Setup
29+
public void setUp() {
30+
matrixA = MatrixUtil.create(MATRIX_SIZE);
31+
matrixB = MatrixUtil.create(MATRIX_SIZE);
32+
}
33+
34+
private ExecutorService executor;
35+
36+
// @Benchmark
37+
public int[][] singleThreadMultiplyOpt() throws Exception {
38+
return MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB);
39+
}
40+
41+
// @Benchmark
42+
public int[][] singleThreadMultiplyOpt2() throws Exception {
43+
return MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB);
44+
}
45+
46+
@Benchmark
47+
public int[][] concurrentMultiplyStreams() throws Exception {
48+
return MatrixUtil.concurrentMultiplyStreams(matrixA, matrixB, threadNumber);
49+
}
50+
51+
// @Benchmark
52+
public int[][] concurrentMultiply() throws Exception {
53+
return MatrixUtil.concurrentMultiply(matrixA, matrixB, executor);
54+
}
55+
56+
@Benchmark
57+
public int[][] concurrentMultiply2() throws Exception {
58+
return MatrixUtil.concurrentMultiply2(matrixA, matrixB, executor);
59+
}
60+
61+
@Setup
62+
public void setup() {
63+
executor = Executors.newFixedThreadPool(threadNumber);
64+
}
65+
66+
@TearDown
67+
public void tearDown() {
68+
executor.shutdown();
69+
}
70+
}

0 commit comments

Comments
 (0)