Skip to content

Commit d8674eb

Browse files
authored
Implemented batch reads and batch writes; (#3)
1 parent babe67e commit d8674eb

File tree

10 files changed

+819
-215
lines changed

10 files changed

+819
-215
lines changed

benchmarks/CMakeLists.txt

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
function(create_benchmark bm_name FILES)
2-
3-
add_executable(bench_${bm_name}
4-
bench_${bm_name}.cpp
5-
${FILES}
6-
)
7-
8-
target_link_libraries(bench_${bm_name}
9-
PUBLIC
10-
AshDBLib
11-
${CONAN_LIBS_BOOST}
12-
${CONAN_LIBS_BENCHMARK}
13-
)
14-
15-
endfunction()
16-
171
include_directories(${CMAKE_SOURCE_DIR}/include)
182

193
set(SOURCE_FILES
@@ -22,4 +6,14 @@ set(SOURCE_FILES
226
../include/ashdb/ashdb.h
237
)
248

25-
create_benchmark("basic" "${SOURCE_FILES}")
9+
add_executable(bench_basic
10+
bench_basic.cpp
11+
${FILES}
12+
)
13+
14+
target_link_libraries(bench_basic
15+
PUBLIC
16+
AshDBLib
17+
${CONAN_LIBS_BOOST}
18+
${CONAN_LIBS_BENCHMARK}
19+
)

benchmarks/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Benchmark
2+
3+
## 2021-06: Alpha 2
4+
```
5+
Run on (16 X 2300 MHz CPU s)
6+
CPU Caches:
7+
L1 Data 32 KiB (x8)
8+
L1 Instruction 32 KiB (x8)
9+
L2 Unified 256 KiB (x8)
10+
L3 Unified 16384 KiB (x1)
11+
Load Average: 1.38, 1.79, 1.91
12+
------------------------------------------------------------------
13+
Benchmark Time CPU Iterations
14+
------------------------------------------------------------------
15+
DBCreateOpen 26721 ns 26713 ns 26123
16+
DBOpenClose 26744 ns 26742 ns 26250
17+
DBWriteInt 91091 ns 91028 ns 7822
18+
DBMultipleIntWrites 9226194 ns 9219063 ns 79
19+
DBRandomIntReads 1860875 ns 1863817 ns 371
20+
DBWriteStruct 99882 ns 98629 ns 7482
21+
DBMultipleStructWrites 9522477 ns 9514260 ns 73
22+
DBRandomStructReads 1988915 ns 1990536 ns 351
23+
BatchWriteMultipleFiles 4966214 ns 4652037 ns 108
24+
BatchReadMultipleFiles 778121 ns 778157 ns 918
25+
```
26+
27+
28+
## 2021-05: Alpha 1
29+
```
30+
CPU Caches:
31+
L1 Data 32 KiB (x8)
32+
L1 Instruction 32 KiB (x8)
33+
L2 Unified 256 KiB (x8)
34+
L3 Unified 16384 KiB (x1)
35+
Load Average: 2.11, 2.01, 1.79
36+
--------------------------------------------------------------------
37+
Benchmark Time CPU Iterations
38+
--------------------------------------------------------------------
39+
BM_DBCreateOpen 25794 ns 25792 ns 26568
40+
BM_DBOpenClose 25181 ns 25181 ns 27629
41+
BM_DBWriteInt 91354 ns 91269 ns 7487
42+
BM_DBMultipleIntWrites 9255495 ns 9255213 ns 75
43+
BM_DBRandomIntReads 1845182 ns 1846939 ns 378
44+
BM_DBWriteStruct 97525 ns 95837 ns 7297
45+
BM_DBMultipleStructWrites 10170736 ns 10177726 ns 73
46+
BM_DBRandomStructReads 1975045 ns 1977746 ns 354
47+
```

benchmarks/bench_basic.cpp

Lines changed: 105 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ boost::filesystem::path tempFolder(const std::string& subfolder)
1414
return temp;
1515
}
1616

17-
static void BM_DBCreateOpen(benchmark::State& state)
17+
static void DBCreateOpen(benchmark::State& state)
1818
{
19-
auto tempfolder = (tempFolder("BM_DBCreateOpen")).string();
19+
auto tempfolder = (tempFolder("DBCreateOpen")).string();
2020

2121
ashdb::Options options;
2222
options.create_if_missing = true;
@@ -28,11 +28,11 @@ static void BM_DBCreateOpen(benchmark::State& state)
2828
db.open();
2929
}
3030
}
31-
BENCHMARK(BM_DBCreateOpen);
31+
BENCHMARK(DBCreateOpen);
3232

33-
static void BM_DBOpenClose(benchmark::State& state)
33+
static void DBOpenClose(benchmark::State& state)
3434
{
35-
auto tempfolder = (tempFolder("BM_DBOpenClose")).string();
35+
auto tempfolder = (tempFolder("DBOpenClose")).string();
3636

3737
ashdb::Options options;
3838
options.create_if_missing = true;
@@ -46,11 +46,11 @@ static void BM_DBOpenClose(benchmark::State& state)
4646
db.close();
4747
}
4848
}
49-
BENCHMARK(BM_DBOpenClose);
49+
BENCHMARK(DBOpenClose);
5050

51-
static void BM_DBWriteInt(benchmark::State& state)
51+
static void DBWriteInt(benchmark::State& state)
5252
{
53-
auto tempfolder = (tempFolder("BM_DBWriteInt")).string();
53+
auto tempfolder = (tempFolder("DBWriteInt")).string();
5454

5555
ashdb::Options options;
5656
options.create_if_missing = true;
@@ -63,11 +63,11 @@ static void BM_DBWriteInt(benchmark::State& state)
6363
db.write(3);
6464
}
6565
}
66-
BENCHMARK(BM_DBWriteInt);
66+
BENCHMARK(DBWriteInt);
6767

68-
static void BM_DBMultipleIntWrites(benchmark::State& state)
68+
static void DBMultipleIntWrites(benchmark::State& state)
6969
{
70-
auto tempfolder = (tempFolder("BM_DBMultipleIntWrites")).string();
70+
auto tempfolder = (tempFolder("DBMultipleIntWrites")).string();
7171

7272
ashdb::Options options;
7373
options.create_if_missing = true;
@@ -83,11 +83,11 @@ static void BM_DBMultipleIntWrites(benchmark::State& state)
8383
}
8484
}
8585
}
86-
BENCHMARK(BM_DBMultipleIntWrites);
86+
BENCHMARK(DBMultipleIntWrites);
8787

88-
static void BM_DBRandomIntReads(benchmark::State& state)
88+
static void DBRandomIntReads(benchmark::State& state)
8989
{
90-
auto tempfolder = (tempFolder("BM_DBRandomIntReads")).string();
90+
auto tempfolder = (tempFolder("DBRandomIntReads")).string();
9191

9292
std::random_device rd;
9393
std::mt19937 gen(rd());
@@ -115,13 +115,13 @@ static void BM_DBRandomIntReads(benchmark::State& state)
115115
}
116116
}
117117
}
118-
BENCHMARK(BM_DBRandomIntReads);
118+
BENCHMARK(DBRandomIntReads);
119119

120-
#include "../tests/test_class1.h"
120+
#include "../tests/Person.h"
121121

122-
static void BM_DBWriteStruct(benchmark::State& state)
122+
static void DBWriteStruct(benchmark::State& state)
123123
{
124-
auto tempfolder = (tempFolder("BM_DBWriteStruct")).string();
124+
auto tempfolder = (tempFolder("DBWriteStruct")).string();
125125

126126
ashdb::Options options;
127127
options.create_if_missing = true;
@@ -143,11 +143,11 @@ static void BM_DBWriteStruct(benchmark::State& state)
143143
db.write(p);
144144
}
145145
}
146-
BENCHMARK(BM_DBWriteStruct);
146+
BENCHMARK(DBWriteStruct);
147147

148-
static void BM_DBMultipleStructWrites(benchmark::State& state)
148+
static void DBMultipleStructWrites(benchmark::State& state)
149149
{
150-
auto tempfolder = (tempFolder("BM_DBStructMultipleWrites")).string();
150+
auto tempfolder = (tempFolder("DBStructMultipleWrites")).string();
151151

152152
ashdb::Options options;
153153
options.create_if_missing = true;
@@ -174,11 +174,11 @@ static void BM_DBMultipleStructWrites(benchmark::State& state)
174174
}
175175
}
176176
}
177-
BENCHMARK(BM_DBMultipleStructWrites);
177+
BENCHMARK(DBMultipleStructWrites);
178178

179-
static void BM_DBRandomStructReads(benchmark::State& state)
179+
static void DBRandomStructReads(benchmark::State& state)
180180
{
181-
auto tempfolder = (tempFolder("BM_DBRandomIntReads")).string();
181+
auto tempfolder = (tempFolder("DBRandomIntReads")).string();
182182

183183
std::random_device rd;
184184
std::mt19937 gen(rd());
@@ -215,6 +215,86 @@ static void BM_DBRandomStructReads(benchmark::State& state)
215215
}
216216
}
217217
}
218-
BENCHMARK(BM_DBRandomStructReads);
218+
BENCHMARK(DBRandomStructReads);
219+
220+
// comparison with "DBMultipleStructWrites"
221+
static void BatchWriteMultipleFiles(benchmark::State& state)
222+
{
223+
auto tempfolder = (tempFolder("DBStructMultipleWrites")).string();
224+
225+
ashdb::Options options;
226+
options.create_if_missing = true;
227+
options.error_if_exists = false;
228+
229+
ashdb::AshDB<project::Person> db{ tempfolder, options };
230+
db.open();
231+
232+
while (state.KeepRunning())
233+
{
234+
project::PersonDB::Batch batch;
235+
for (auto i = 0u; i < 100; ++i)
236+
{
237+
state.PauseTiming();
238+
project::Person p;
239+
p.name.first = "Firstname" + std::to_string(i);
240+
p.name.middle = (i % 2) ? "Middle" + std::to_string(i) : "";
241+
p.name.last = "Lastname" + std::to_string(i);
242+
p.age = (i % 80);
243+
p.salary = (i % 5) * 12345.67;
244+
p.married = (i % 2) == 0;
245+
batch.push_back(p);
246+
state.ResumeTiming();
247+
}
248+
249+
db.write(batch);
250+
}
251+
}
252+
BENCHMARK(BatchWriteMultipleFiles);
253+
254+
// compare to "DBRandomStructReads"
255+
static void BatchReadMultipleFiles(benchmark::State& state)
256+
{
257+
auto tempfolder = (tempFolder("BatchReadMultipleFiles")).string();
258+
259+
ashdb::Options options;
260+
options.filesize_max = 100;
261+
262+
ashdb::AshDB<project::Person> db{ tempfolder, options };
263+
db.open();
264+
265+
project::PersonDB::Batch batch;
266+
for (auto i = 0u; i < 200; ++i)
267+
{
268+
project::Person p;
269+
p.name.first = "Firstname" + std::to_string(i);
270+
p.name.middle = (i % 2) ? "Middle" + std::to_string(i) : "";
271+
p.name.last = "Lastname" + std::to_string(i);
272+
p.age = (i % 80);
273+
p.salary = (i % 5) * 12345.67;
274+
p.married = (i % 2) == 0;
275+
276+
batch.push_back(p);
277+
}
278+
db.write(batch);
279+
280+
std::random_device rd;
281+
std::mt19937 gen(rd());
282+
283+
while (state.KeepRunning())
284+
{
285+
state.PauseTiming();
286+
std::uniform_int_distribution<> distrib(0, 99);
287+
std::uint32_t idx = static_cast<std::uint32_t>(distrib(gen));
288+
state.ResumeTiming();
289+
290+
// read 100 records at a random idx
291+
auto b = db.read(idx, 100);
292+
293+
state.PauseTiming();
294+
b.clear(); // do something to avoid unused var warning
295+
state.ResumeTiming();
296+
}
297+
}
298+
BENCHMARK(BatchReadMultipleFiles);
219299

220300
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)