-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Copy pathdb_basic_bench.cc
139 lines (120 loc) · 3.52 KB
/
db_basic_bench.cc
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
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
// this is a simple micro-benchmark for compare ribbon filter vs. other filter
// for more comprehensive, please check the dedicate util/filter_bench.
#include <benchmark/benchmark.h>
#include "rocksdb/db.h"
#include "rocksdb/options.h"
#include "util/random.h"
namespace ROCKSDB_NAMESPACE {
static void DBOpen(benchmark::State& state) {
// create DB
DB* db;
Options options;
auto env = Env::Default();
std::string db_path;
auto s = env->GetTestDirectory(&db_path);
if (!s.ok()) {
state.SkipWithError(s.ToString().c_str());
return;
}
std::string db_name = db_path + "/bench_dbopen";
DestroyDB(db_name, options);
options.create_if_missing = true;
s = DB::Open(options, db_name, &db);
if (!s.ok()) {
state.SkipWithError(s.ToString().c_str());
return;
}
db->Close();
options.create_if_missing = false;
auto rnd = Random(12345);
for (auto _ : state) {
s = DB::Open(options, db_name, &db);
if (!s.ok()) {
state.SkipWithError(s.ToString().c_str());
}
state.PauseTiming();
auto wo = WriteOptions();
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 100; j++) {
s = db->Put(wo, rnd.RandomString(10), rnd.RandomString(100));
if (!s.ok()) {
state.SkipWithError(s.ToString().c_str());
}
}
s = db->Flush(FlushOptions());
}
if (!s.ok()) {
state.SkipWithError(s.ToString().c_str());
}
s = db->Close();
if (!s.ok()) {
state.SkipWithError(s.ToString().c_str());
}
state.ResumeTiming();
}
DestroyDB(db_name, options);
}
BENCHMARK(DBOpen)->Iterations(200); // specify iteration number as the db size
// is impacted by iteration number
static void DBClose(benchmark::State& state) {
// create DB
DB* db;
Options options;
auto env = Env::Default();
std::string db_path;
auto s = env->GetTestDirectory(&db_path);
if (!s.ok()) {
state.SkipWithError(s.ToString().c_str());
return;
}
std::string db_name = db_path + "/bench_dbclose";
DestroyDB(db_name, options);
options.create_if_missing = true;
s = DB::Open(options, db_name, &db);
if (!s.ok()) {
state.SkipWithError(s.ToString().c_str());
return;
}
db->Close();
options.create_if_missing = false;
auto rnd = Random(12345);
for (auto _ : state) {
state.PauseTiming();
s = DB::Open(options, db_name, &db);
if (!s.ok()) {
state.SkipWithError(s.ToString().c_str());
}
auto wo = WriteOptions();
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 100; j++) {
s = db->Put(wo, rnd.RandomString(10), rnd.RandomString(100));
if (!s.ok()) {
state.SkipWithError(s.ToString().c_str());
}
}
s = db->Flush(FlushOptions());
}
if (!s.ok()) {
state.SkipWithError(s.ToString().c_str());
}
state.ResumeTiming();
s = db->Close();
if (!s.ok()) {
state.SkipWithError(s.ToString().c_str());
}
}
DestroyDB(db_name, options);
}
BENCHMARK(DBClose)->Iterations(200); // specify iteration number as the db size
// is impacted by iteration number
} // namespace ROCKSDB_NAMESPACE
#ifndef BENCHMARK_MAIN_ONCE
#ifndef IS_BENCH_BUILD
BENCHMARK_MAIN();
#define BENCHMARK_MAIN_ONCE
#endif
#endif