|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +package com.swirlds.benchmark; |
| 3 | + |
| 4 | +import static com.swirlds.base.units.UnitConstants.MEBIBYTES_TO_BYTES; |
| 5 | + |
| 6 | +import com.hedera.pbj.runtime.io.buffer.BufferedData; |
| 7 | +import com.swirlds.base.test.fixtures.util.DataUtils; |
| 8 | +import com.swirlds.common.io.utility.FileUtils; |
| 9 | +import com.swirlds.merkledb.files.DataFileWriter; |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.time.Instant; |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.Random; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import org.openjdk.jmh.annotations.Benchmark; |
| 18 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 19 | +import org.openjdk.jmh.annotations.Fork; |
| 20 | +import org.openjdk.jmh.annotations.Level; |
| 21 | +import org.openjdk.jmh.annotations.Measurement; |
| 22 | +import org.openjdk.jmh.annotations.Mode; |
| 23 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 24 | +import org.openjdk.jmh.annotations.Param; |
| 25 | +import org.openjdk.jmh.annotations.Scope; |
| 26 | +import org.openjdk.jmh.annotations.Setup; |
| 27 | +import org.openjdk.jmh.annotations.State; |
| 28 | +import org.openjdk.jmh.annotations.TearDown; |
| 29 | +import org.openjdk.jmh.annotations.Warmup; |
| 30 | + |
| 31 | +/** |
| 32 | + * This benchmarks can be used to measure performance of {@link DataFileWriter} class. |
| 33 | + * It uses pregenerated list of data items to measure only writing speed. |
| 34 | + */ |
| 35 | +@State(Scope.Benchmark) |
| 36 | +public class DataFileWriterBenchmark { |
| 37 | + |
| 38 | + /** |
| 39 | + * Size of the buffer in MB - <b>should not</b> impact on performance with the current implementation with mapped buffer. |
| 40 | + */ |
| 41 | + @Param({"16", "64", "128", "256"}) |
| 42 | + public int bufferSizeMb; |
| 43 | + |
| 44 | + /** |
| 45 | + * Maximum size of the file to write in MB. |
| 46 | + */ |
| 47 | + @Param({"50", "200", "500"}) |
| 48 | + public int maxFileSizeMb; |
| 49 | + |
| 50 | + /** |
| 51 | + * Number of sample data items to pre-generate before each iteration. |
| 52 | + */ |
| 53 | + @Param({"10"}) |
| 54 | + public int sampleSize; |
| 55 | + |
| 56 | + /** |
| 57 | + * Range of the sample data items in bytes, chosen randomly from the sample. |
| 58 | + */ |
| 59 | + // first is test for small data items like hashes |
| 60 | + @Param({"56-56", "300-1000", "1024-8192"}) |
| 61 | + public String sampleRangeBytes; |
| 62 | + |
| 63 | + // Runtime variables |
| 64 | + private Random random; |
| 65 | + private BufferedData[] sampleData; |
| 66 | + private long maxFileSize; |
| 67 | + |
| 68 | + private Path benchmarkDir; |
| 69 | + private DataFileWriter dataFileWriter; |
| 70 | + |
| 71 | + @Setup(Level.Trial) |
| 72 | + public void setupGlobal() throws IOException { |
| 73 | + random = new Random(1234); |
| 74 | + benchmarkDir = Files.createTempDirectory("dataFileWriterBenchmark"); |
| 75 | + |
| 76 | + maxFileSize = maxFileSizeMb * MEBIBYTES_TO_BYTES; |
| 77 | + |
| 78 | + // Generate sample data |
| 79 | + String[] range = sampleRangeBytes.split("-"); |
| 80 | + int sampleMinLength = Integer.parseInt(range[0]); |
| 81 | + int sampleMaxLength = Integer.parseInt(range[1]); |
| 82 | + |
| 83 | + sampleData = new BufferedData[sampleSize]; |
| 84 | + for (int i = 0; i < sampleSize; i++) { |
| 85 | + sampleData[i] = |
| 86 | + BufferedData.wrap(DataUtils.randomUtf8Bytes(random.nextInt(sampleMinLength, sampleMaxLength + 1))); |
| 87 | + } |
| 88 | + |
| 89 | + System.out.println("Sample data sizes in bytes: " |
| 90 | + + Arrays.toString(Arrays.stream(sampleData) |
| 91 | + .mapToLong(BufferedData::length) |
| 92 | + .toArray())); |
| 93 | + } |
| 94 | + |
| 95 | + @TearDown(Level.Trial) |
| 96 | + public void tearDownGlobal() throws IOException { |
| 97 | + if (benchmarkDir != null) { |
| 98 | + FileUtils.deleteDirectory(benchmarkDir); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Setup(Level.Invocation) |
| 103 | + public void setup() throws IOException { |
| 104 | + dataFileWriter = |
| 105 | + new DataFileWriter("test", benchmarkDir, 1, Instant.now(), 1, bufferSizeMb * MEBIBYTES_TO_BYTES); |
| 106 | + } |
| 107 | + |
| 108 | + @TearDown(Level.Invocation) |
| 109 | + public void tearDown() throws IOException { |
| 110 | + dataFileWriter.close(); |
| 111 | + } |
| 112 | + |
| 113 | + @Benchmark |
| 114 | + @BenchmarkMode(Mode.AverageTime) |
| 115 | + @Measurement(iterations = 1, time = 3, timeUnit = TimeUnit.SECONDS) |
| 116 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 117 | + @Fork(value = 1, warmups = 0) |
| 118 | + @Warmup(iterations = 0) |
| 119 | + public void writeInFile() throws IOException { |
| 120 | + long fileSize = 0; |
| 121 | + BufferedData data; |
| 122 | + |
| 123 | + while (true) { |
| 124 | + data = getRandomData(); |
| 125 | + fileSize += data.length(); |
| 126 | + if (fileSize > maxFileSize) { |
| 127 | + break; |
| 128 | + } |
| 129 | + |
| 130 | + dataFileWriter.storeDataItem(data); |
| 131 | + data.flip(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private BufferedData getRandomData() { |
| 136 | + return sampleData[random.nextInt(sampleSize)]; |
| 137 | + } |
| 138 | +} |
0 commit comments