forked from JavaWebinar/masterjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixBenchmark.java
More file actions
85 lines (69 loc) · 2.37 KB
/
MatrixBenchmark.java
File metadata and controls
85 lines (69 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package ru.javaops.masterjava.matrix;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.TimeValue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@Warmup(iterations = 10)
@Measurement(iterations = 10)
@BenchmarkMode({Mode.SingleShotTime})
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
@Threads(1)
@Fork(10)
@Timeout(time = 5, timeUnit = TimeUnit.MINUTES)
public class MatrixBenchmark {
// Matrix size
private static final int MATRIX_SIZE = 1000;
@Param({"3", "4", "10"})
private int threadNumber;
private static int[][] matrixA;
private static int[][] matrixB;
@Setup
public void setUp() {
matrixA = MatrixUtil.create(MATRIX_SIZE);
matrixB = MatrixUtil.create(MATRIX_SIZE);
}
private ExecutorService executor;
public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder()
.include(MatrixBenchmark.class.getSimpleName())
.threads(1)
.forks(10)
.timeout(TimeValue.minutes(5))
.build();
new Runner(options).run();
}
// @Benchmark
public int[][] singleThreadMultiplyOpt() throws Exception {
return MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB);
}
// @Benchmark
public int[][] singleThreadMultiplyOpt2() throws Exception {
return MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB);
}
@Benchmark
public int[][] concurrentMultiplyStreams() throws Exception {
return MatrixUtil.concurrentMultiplyStreams(matrixA, matrixB, threadNumber);
}
// @Benchmark
public int[][] concurrentMultiply() throws Exception {
return MatrixUtil.concurrentMultiply(matrixA, matrixB, executor);
}
@Benchmark
public int[][] concurrentMultiply2() throws Exception {
return MatrixUtil.concurrentMultiply2(matrixA, matrixB, executor);
}
@Setup
public void setup() {
executor = Executors.newFixedThreadPool(threadNumber);
}
@TearDown
public void tearDown() {
executor.shutdown();
}
}