forked from Alluxio/alluxio
/
BlockStoreRandomReadBench.java
225 lines (189 loc) · 8.22 KB
/
BlockStoreRandomReadBench.java
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package alluxio.worker;
import alluxio.AlluxioTestDirectory;
import alluxio.proto.dataserver.Protocol;
import alluxio.util.io.PathUtils;
import alluxio.worker.block.BlockStore;
import alluxio.worker.block.io.BlockReader;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.Unpooled;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.profile.StackProfiler;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.CommandLineOptionException;
import org.openjdk.jmh.runner.options.CommandLineOptions;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Random;
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
@Fork(value = 1, jvmArgsPrepend = "-server")
@Warmup(iterations = 2, time = 3)
@Measurement(iterations = 5, time = 3)
@BenchmarkMode(Mode.Throughput)
public class BlockStoreRandomReadBench {
/** Seed for random number generator. */
private static final long SEED = 30;
/** Consumer of block data. */
private static final byte[] SINK = new byte[64 * 1024 * 1024];
@State(Scope.Benchmark)
public static class RandomReadParams {
private final Random mRandom = new Random(SEED);
@Param({"16", "64"})
public long mBlockSizeMB;
public long mBlockSize;
public final long mLocalBlockId = 1L;
public final long mUfsMountId = 2L;
public final long mUfsBlockId = 3L;
public String mUfsPath;
/** Each random read's size: 1MB. */
@Param({"1"})
public long mReadSizeMB;
public long mReadSize;
public BlockStoreBase mBlockStoreBase;
/** Sequence of random offsets. */
public long[] mOffsets;
/** Block Data. */
public byte[] mData;
@Setup(Level.Trial)
public void setup() throws Exception {
mBlockSize = mBlockSizeMB * 1024 * 1024;
mReadSize = mReadSizeMB * 1024 * 1024;
int numReads = (int) (mBlockSize / mReadSize);
// generate an array of offsets
mOffsets = mRandom.longs(numReads, 0, mBlockSize - mReadSize).toArray();
mBlockStoreBase = BlockStoreBase.create();
mData = new byte[(int) mBlockSize];
mRandom.nextBytes(mData);
mBlockStoreBase.prepareLocalBlock(mLocalBlockId, mBlockSize, mData);
String ufsRoot = AlluxioTestDirectory.createTemporaryDirectory("ufs").getAbsolutePath();
mBlockStoreBase.mountUfs(mUfsMountId, ufsRoot);
mUfsPath = PathUtils.concatUfsPath(ufsRoot, "test_file");
mBlockStoreBase.prepareUfsFile(mUfsPath, mData);
}
@TearDown(Level.Trial)
public void teardown() throws Exception {
mBlockStoreBase.mMonoBlockStore.removeBlock(1L, mLocalBlockId);
mBlockStoreBase.close();
}
}
@Benchmark
public void monoBlockStoreRandReadLocal(RandomReadParams params) throws Exception {
randReadLocal(params.mBlockStoreBase.mMonoBlockStore,
params.mLocalBlockId, params.mBlockSize, params.mOffsets, params.mReadSize);
}
@Benchmark
public void monoBlockStoreRandTransferLocal(RandomReadParams params) throws Exception {
randTransferLocal(params.mBlockStoreBase.mMonoBlockStore,
params.mLocalBlockId, params.mBlockSize, params.mOffsets, params.mReadSize);
}
@Benchmark
public void monoBlockStoreRandReadUfs(RandomReadParams params) throws Exception {
randReadUfs(params.mBlockStoreBase.mMonoBlockStore, params.mUfsBlockId,
params.mUfsMountId, params.mUfsPath, params.mBlockSize, params.mOffsets, params.mReadSize);
}
@Benchmark
public void pagedBlockStoreRandReadLocal(RandomReadParams params) throws Exception {
randReadLocal(params.mBlockStoreBase.mPagedBlockStore,
params.mLocalBlockId, params.mBlockSize, params.mOffsets, params.mReadSize);
}
@Benchmark
public void pagedBlockStoreRandTransferLocal(RandomReadParams params) throws Exception {
randTransferLocal(params.mBlockStoreBase.mPagedBlockStore,
params.mLocalBlockId, params.mBlockSize, params.mOffsets, params.mReadSize);
}
@Benchmark
public void pagedBlockStoreRandReadUfs(RandomReadParams params) throws Exception {
randReadUfs(params.mBlockStoreBase.mPagedBlockStore, params.mUfsBlockId,
params.mUfsMountId, params.mUfsPath, params.mBlockSize, params.mOffsets, params.mReadSize);
}
private void randReadLocal(BlockStore store, long blockId,
long blockSize, long[] offsets, long readSize) throws IOException {
try (BlockReader reader = store.createBlockReader(1L, blockId, 0, false,
Protocol.OpenUfsBlockOptions.newBuilder().build())) {
for (long offset: offsets) {
ByteBuffer buffer = reader.read(offset, readSize);
ByteBuf buf = Unpooled.wrappedBuffer(buffer);
buf.readBytes(SINK, (int) offset, (int) readSize);
buf.release();
}
}
}
private void randTransferLocal(BlockStore store, long blockId,
long blockSize, long[] offsets, long readSize) throws IOException {
try (BlockReader reader = store.createBlockReader(1L, blockId, 0, false,
Protocol.OpenUfsBlockOptions.newBuilder().build())) {
for (long offset: offsets) {
ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer((int) readSize, (int) readSize);
((FileChannel) reader.getChannel()).position(offset);
while (buf.writableBytes() > 0 && reader.transferTo(buf) >= 0) {}
buf.readBytes(SINK, (int) offset, (int) readSize);
buf.release();
}
}
}
private void randReadUfs(BlockStore store, long blockId, long mountId, String ufsPath,
long blockSize, long[] offsets, long readSize) throws IOException {
try (BlockReader reader = store.createBlockReader(1L, blockId, 0, false,
Protocol.OpenUfsBlockOptions
.newBuilder()
.setNoCache(true)
.setMountId(mountId)
.setUfsPath(ufsPath)
.setBlockSize(blockSize)
.build())) {
for (long offset: offsets) {
ByteBuffer buffer = reader.read(offset, readSize);
ByteBuf buf = Unpooled.wrappedBuffer(buffer);
buf.readBytes(SINK, (int) offset, (int) readSize);
buf.release();
}
}
}
public static void main(String[] args) throws RunnerException, CommandLineOptionException {
Options argsCli = new CommandLineOptions(args);
Options opts = new OptionsBuilder()
.parent(argsCli)
.addProfiler(StackProfiler.class)
.include(BlockStoreRandomReadBench.class.getName())
.result("results.json")
.resultFormat(ResultFormatType.JSON)
.shouldDoGC(true)
.build();
new Runner(opts).run();
}
}