|
| 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 | +/** |
| 10 | + * gkislin |
| 11 | + * 23.09.2016 |
| 12 | + */ |
| 13 | +@Warmup(iterations = 10) |
| 14 | +@Measurement(iterations = 10) |
| 15 | +@BenchmarkMode({Mode.SingleShotTime}) |
| 16 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 17 | +@State(Scope.Benchmark) |
| 18 | +@Threads(1) |
| 19 | +@Fork(1) |
| 20 | +@Timeout(time = 5, timeUnit = TimeUnit.MINUTES) |
| 21 | +public class MatrixBenchmark { |
| 22 | + // Matrix size |
| 23 | + @Param({"100", "1000"}) |
| 24 | + private int matrixSize; |
| 25 | + |
| 26 | + private static final int THREAD_NUMBER = 10; |
| 27 | + private final static ExecutorService executor = Executors.newFixedThreadPool(THREAD_NUMBER); |
| 28 | + |
| 29 | + private static int[][] matrixA; |
| 30 | + private static int[][] matrixB; |
| 31 | + |
| 32 | + @Setup |
| 33 | + public void setUp() { |
| 34 | + matrixA = MatrixUtil.create(matrixSize); |
| 35 | + matrixB = MatrixUtil.create(matrixSize); |
| 36 | + } |
| 37 | + |
| 38 | + @Benchmark |
| 39 | + public int[][] singleThreadMultiplyOpt() throws Exception { |
| 40 | + return MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB); |
| 41 | + } |
| 42 | + |
| 43 | + @Benchmark |
| 44 | + public int[][] concurrentMultiplyStreams() throws Exception { |
| 45 | + return MatrixUtil.concurrentMultiplyStreams(matrixA, matrixB, executor); |
| 46 | + } |
| 47 | + |
| 48 | + @Benchmark |
| 49 | + public int[][] concurrentMultiply2() throws Exception { |
| 50 | + return MatrixUtil.concurrentMultiply2(matrixA, matrixB, executor); |
| 51 | + } |
| 52 | + |
| 53 | + @Benchmark |
| 54 | + public int[][] concurrentMultiply3() throws Exception { |
| 55 | + return MatrixUtil.concurrentMultiply3(matrixA, matrixB, executor); |
| 56 | + } |
| 57 | + |
| 58 | + @TearDown |
| 59 | + public void tearDown() { |
| 60 | + executor.shutdown(); |
| 61 | + } |
| 62 | +} |
0 commit comments