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