|
6 | 6 | package io.opentelemetry.contrib.disk.buffering.internal.files.utils; |
7 | 7 |
|
8 | 8 | import io.opentelemetry.contrib.disk.buffering.internal.storage.files.utils.FileTransferUtil; |
9 | | -import java.io.File; |
10 | | -import java.io.FileInputStream; |
11 | | -import java.io.IOException; |
12 | | -import java.nio.file.Files; |
13 | | -import java.nio.file.StandardOpenOption; |
14 | 9 | import org.openjdk.jmh.annotations.Benchmark; |
15 | 10 | import org.openjdk.jmh.annotations.BenchmarkMode; |
16 | 11 | import org.openjdk.jmh.annotations.Mode; |
|
19 | 14 | import org.openjdk.jmh.annotations.State; |
20 | 15 | import org.openjdk.jmh.annotations.TearDown; |
21 | 16 |
|
| 17 | +import java.io.File; |
| 18 | +import java.io.FileInputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.StandardOpenOption; |
| 22 | + |
22 | 23 | public class FileTransferUtilBenchmark { |
23 | 24 |
|
24 | | - @Benchmark |
25 | | - @BenchmarkMode(Mode.AverageTime) |
26 | | - public void fileTransfer(FileTransferState state) throws IOException { |
27 | | - state.fileTransferUtil.transferBytes(state.offset, state.amountOfBytesToTransfer); |
28 | | - } |
29 | | - |
30 | | - @State(Scope.Benchmark) |
31 | | - public static class FileTransferState { |
32 | | - public FileTransferUtil fileTransferUtil; |
33 | | - public int offset; |
34 | | - public int amountOfBytesToTransfer; |
35 | | - private File inputFile; |
36 | | - private File outputFile; |
37 | | - |
38 | | - @Setup |
39 | | - public void setUp() throws IOException { |
40 | | - outputFile = File.createTempFile("output", ".txt"); |
41 | | - inputFile = File.createTempFile("input", ".txt"); |
42 | | - int totalDataSize = 1024 * 1024; // 1MB |
43 | | - byte[] data = new byte[totalDataSize]; |
44 | | - Files.write(inputFile.toPath(), data, StandardOpenOption.CREATE); |
45 | | - fileTransferUtil = new FileTransferUtil(new FileInputStream(inputFile), outputFile); |
46 | | - offset = 512; |
47 | | - amountOfBytesToTransfer = totalDataSize - offset; |
| 25 | + @Benchmark |
| 26 | + @BenchmarkMode(Mode.AverageTime) |
| 27 | + public void fileTransfer(FileTransferState state) |
| 28 | + throws IOException { |
| 29 | + state.fileTransferUtil.transferBytes(state.offset, state.amountOfBytesToTransfer); |
48 | 30 | } |
49 | 31 |
|
50 | | - @TearDown |
51 | | - public void tearDown() throws IOException { |
52 | | - fileTransferUtil.close(); |
53 | | - inputFile.delete(); |
54 | | - outputFile.delete(); |
| 32 | + @State(Scope.Benchmark) |
| 33 | + public static class FileTransferState { |
| 34 | + public FileTransferUtil fileTransferUtil; |
| 35 | + public int offset; |
| 36 | + public int amountOfBytesToTransfer; |
| 37 | + private File inputFile; |
| 38 | + private File outputFile; |
| 39 | + |
| 40 | + @Setup |
| 41 | + public void setUp() throws IOException { |
| 42 | + outputFile = File.createTempFile("output", ".txt"); |
| 43 | + inputFile = File.createTempFile("input", ".txt"); |
| 44 | + int totalDataSize = 1024 * 1024; // 1MB |
| 45 | + byte[] data = new byte[totalDataSize]; |
| 46 | + Files.write(inputFile.toPath(), data, StandardOpenOption.CREATE); |
| 47 | + fileTransferUtil = new FileTransferUtil(new FileInputStream(inputFile), outputFile); |
| 48 | + offset = 512; |
| 49 | + amountOfBytesToTransfer = totalDataSize - offset; |
| 50 | + } |
| 51 | + |
| 52 | + @TearDown |
| 53 | + public void tearDown() throws IOException { |
| 54 | + fileTransferUtil.close(); |
| 55 | + inputFile.delete(); |
| 56 | + outputFile.delete(); |
| 57 | + } |
55 | 58 | } |
56 | | - } |
57 | 59 | } |
0 commit comments