-
Notifications
You must be signed in to change notification settings - Fork 0
/
BplusPerfTest.java
314 lines (247 loc) · 11.8 KB
/
BplusPerfTest.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package perf_test;
import com.keks.kv_storage.KVStore;
import com.keks.kv_storage.bplus.BPlusEngine;
import com.keks.kv_storage.bplus.conf.BPlusConfParamsEnum;
import com.keks.kv_storage.bplus.conf.BtreeConf;
import com.keks.kv_storage.conf.TableEngineType;
import com.keks.kv_storage.kv_table_conf.KvTableConfParamsEnum;
import com.keks.kv_storage.record.KVRecord;
import com.keks.kv_storage.utils.Time;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.cumulative.CumulativeTimer;
import io.micrometer.core.instrument.distribution.HistogramSnapshot;
import io.micrometer.core.instrument.distribution.ValueAtPercentile;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Disabled
public class BplusPerfTest {
public static String printStatsCsv(int threads, long millis, CumulativeTimer timer, long writtenBytes) {
ArrayList<String> elems = new ArrayList<>();
ArrayList<String> headers = new ArrayList<>();
HistogramSnapshot histogramSnapshot = timer.takeSnapshot();
headers.add("threads");
elems.add(String.valueOf(threads));
headers.add("records");
elems.add(String.valueOf(timer.count()));
headers.add("written mb");
elems.add(String.valueOf(writtenBytes / 1024));
headers.add("avg record size bytes");
elems.add(String.valueOf(writtenBytes / timer.count()));
headers.add("total sec");
elems.add(String.valueOf(String.format("%.1f", (millis / 1_000.0))));
headers.add("mean millis");
elems.add(String.valueOf(String.format("%.3f", timer.mean(TimeUnit.MILLISECONDS))));
headers.add("ops/sec");
elems.add(String.valueOf((int) (timer.count() / (millis / 1_000.0))));
for (ValueAtPercentile valueAtPercentile : histogramSnapshot.percentileValues()) {
headers.add("p" + valueAtPercentile.percentile() + " millis");
elems.add(String.valueOf(String.format("%.3f", valueAtPercentile.value(TimeUnit.MILLISECONDS))));
}
System.out.println(String.join(",", headers));
System.out.println(String.join(",", elems));
return String.join(",", elems);
}
@Test
public void exPut(@TempDir Path tmpPath) throws InterruptedException, ExecutionException, TimeoutException {
Time.mes = true;
List<Integer> threadsList = Arrays.asList(1, 4, 8, 16, 32, 50, 100, 160, 200);
// List<Integer> threadsList = Arrays.asList(1);
StringBuilder sb = new StringBuilder();
for (Integer threadNum : threadsList) {
Path resolve = tmpPath.resolve(String.valueOf(threadNum));
resolve.toFile().mkdir();
Instant start = Instant.now();
DataGenerator dataGenerator = new DataGenerator(5_000_000, threadNum);
CumulativeTimer timer = runPut(threadNum, dataGenerator, resolve);
Instant finish = Instant.now();
Duration between = Duration.between(start, finish);
String metr = printStatsCsv(threadNum, between.toMillis(), timer, dataGenerator.getTotalBytes());
sb.append(metr).append("\n");
}
System.out.println(sb);
}
private CumulativeTimer runPut(int numberOfThreads, DataGenerator dataGenerator, Path tmpPath) throws ExecutionException, InterruptedException, TimeoutException {
SimpleMeterRegistry oneSimpleMeter = new SimpleMeterRegistry();
BPlusEngine.putOptimizedTimer = (CumulativeTimer) Timer
.builder("optimizedTimer")
.publishPercentileHistogram(true)
.publishPercentiles(0.5, 0.75, 0.99, 0.999, 0.9999)
.register(oneSimpleMeter);
String dbName = "bplus_test_db";
String tblName = "bplus_table_test";
try (KVStore kvStore = new KVStore(tmpPath.toFile())) {
kvStore.createDB(dbName);
Properties properties = new Properties() {{
put(BPlusConfParamsEnum.BTREE_ORDER, BtreeConf.MAX_ORDER);
put(BPlusConfParamsEnum.BTREE_PAGE_BUFFER_SIZE_BYTES, 256L * 1024 * 1024);
put(KvTableConfParamsEnum.ENABLE_WAL, false);
}};
kvStore.createTable(
dbName,
tblName,
TableEngineType.BPLUS.toString(),
properties);
Consumer<Integer> func = i -> {
Iterator<KVRecord> batchIter = dataGenerator.getBatchIter(i);
kvStore.put(dbName, tblName, batchIter);
};
runConcurrentTest(
numberOfThreads,
func,
numberOfThreads);
return BPlusEngine.putOptimizedTimer;
}
}
@Test
public void exGet(@TempDir Path tmpPath) throws InterruptedException, ExecutionException, TimeoutException {
Time.mes = true;
List<Integer> threadsList = Arrays.asList(1, 4, 8, 16, 32, 50, 100, 160, 200);
// List<Integer> threadsList = Arrays.asList(1);
String dbName = "bplus_test_db";
String tblName = "bplus_table_test";
int numberOfRecords = 1_000_000;
DataGenerator dataGenerator = new DataGenerator(numberOfRecords, 1);
try (KVStore kvStore = new KVStore(tmpPath.toFile());) {
kvStore.createDB(dbName);
Properties properties = new Properties() {{
put(BPlusConfParamsEnum.BTREE_ORDER, BtreeConf.MAX_ORDER);
put(BPlusConfParamsEnum.BTREE_PAGE_BUFFER_SIZE_BYTES, 256L * 1024 * 1024);
put(KvTableConfParamsEnum.ENABLE_WAL, false);
}};
kvStore.createTable(
dbName,
tblName,
TableEngineType.BPLUS.toString(),
properties);
Iterator<KVRecord> batchIter = dataGenerator.getBatchIter(0);
kvStore.put(dbName, tblName, batchIter);
}
StringBuilder sb = new StringBuilder();
for (Integer threadNum : threadsList) {
Instant start = Instant.now();
CumulativeTimer timer = runGet(threadNum, numberOfRecords, tmpPath);
Instant finish = Instant.now();
Duration between = Duration.between(start, finish);
String metr = printStatsCsv(threadNum, between.toMillis(), timer, dataGenerator.getTotalBytes());
sb.append(metr).append("\n");
}
System.out.println(sb);
}
private CumulativeTimer runGet(int numberOfThreads, int numberOfRecords, Path tmpPath) throws ExecutionException, InterruptedException, TimeoutException {
String dbName = "bplus_test_db";
String tblName = "bplus_table_test";
SimpleMeterRegistry oneSimpleMeter = new SimpleMeterRegistry();
CumulativeTimer optimizedTimer = (CumulativeTimer) Timer
.builder("optimizedTimer")
.publishPercentileHistogram(true)
.publishPercentiles(0.5, 0.75, 0.99, 0.999, 0.9999)
.register(oneSimpleMeter);
AtomicInteger cnt = new AtomicInteger();
try (KVStore kvStore = new KVStore(tmpPath.toFile());) {
Consumer<Integer> func = i -> {
String key = "key" + String.format("%10d", i);
String value = "value" + i;
Time.withTimer(optimizedTimer, () -> {
kvStore.get(dbName, tblName, key);
// assertEquals(value, new String(kvStore.get(dbName, tblName, key)));
}
);
};
runConcurrentTest(
numberOfRecords,
func,
numberOfThreads);
return optimizedTimer;
}
}
@Test
public void exRemove(@TempDir Path tmpPath) throws InterruptedException, ExecutionException, TimeoutException {
Time.mes = true;
List<Integer> threadsList = Arrays.asList(1, 5, 10, 15, 20, 25, 30, 50, 100, 150, 200);
int records = 1_000_000;
for (Integer threadNum : threadsList) {
Path resolve = tmpPath.resolve(String.valueOf(threadNum));
resolve.toFile().mkdir();
Instant start = Instant.now();
DataGenerator dataGenerator = new DataGenerator(records, threadNum);
CumulativeTimer timer = runRemove(threadNum, records, dataGenerator, resolve);
Instant finish = Instant.now();
Duration between = Duration.between(start, finish);
printStatsCsv(threadNum, between.toMillis(), timer, dataGenerator.getTotalBytes());
}
}
private CumulativeTimer runRemove(int numberOfThreads, int numberOfRecords, DataGenerator dataGenerator, Path tmpPath) throws ExecutionException, InterruptedException, TimeoutException {
String dbName = "bplus_test_db";
String tblName = "bplus_table_test";
try (KVStore kvStore = new KVStore(tmpPath.toFile())) {
kvStore.createDB(dbName);
Properties properties = new Properties() {{
put(BPlusConfParamsEnum.BTREE_ORDER, BtreeConf.MAX_ORDER);
put(BPlusConfParamsEnum.BTREE_PAGE_BUFFER_SIZE_BYTES, 400_000_000L);
}};
kvStore.createTable(
dbName,
tblName,
TableEngineType.BPLUS.toString(),
properties);
Consumer<Integer> func = i -> {
String key = "key" + i;
String value = "value" + i;
// adding record
kvStore.put(dbName, tblName, key, value.getBytes());
};
runConcurrentTest(
numberOfRecords,
func,
15);
}
SimpleMeterRegistry oneSimpleMeter = new SimpleMeterRegistry();
CumulativeTimer optimizedTimer = (CumulativeTimer) Timer
.builder("optimizedTimer")
.publishPercentileHistogram(true)
.publishPercentiles(0.1, 0.5, 0.75, 0.99, 0.999, 0.9999)
.register(oneSimpleMeter);
BPlusEngine.removeOptimizedTimer = optimizedTimer;
AtomicInteger cnt = new AtomicInteger();
try (KVStore kvStore = new KVStore(tmpPath.toFile());) {
Consumer<Integer> func = i -> {
Iterator<String> batchIter = dataGenerator.getBatchIterKeys(i);
kvStore.remove(dbName, tblName, batchIter);
};
runConcurrentTest(
numberOfRecords,
func,
numberOfThreads);
return optimizedTimer;
}
}
public static void runConcurrentTest(int taskCount,
Consumer<Integer> function,
int numberOfThreads) throws InterruptedException, ExecutionException, TimeoutException {
ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
List<Future<?>> futures = new ArrayList<>(taskCount);
for (int i = 0; i < taskCount; i++) {
int y = i;
Future<?> future1 = executor.submit(() -> function.accept(y));
futures.add(future1);
}
executor.shutdown();
if (!executor.awaitTermination(10000, TimeUnit.SECONDS)) {
throw new InterruptedException();
}
assertEquals(taskCount, futures.size());
for (Future<?> future : futures) {
future.get(1, TimeUnit.SECONDS);
}
}
}