-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.c
More file actions
164 lines (136 loc) · 3.68 KB
/
Copy pathbenchmark.c
File metadata and controls
164 lines (136 loc) · 3.68 KB
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
#include <bitcask/config.h>
#include <bitcask/store.h>
#include "util.h"
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
const char *DATA_DIR = "./bench_data";
typedef struct {
int iterations;
uint32_t max_file_size;
int key_sz;
int val_sz;
bool wipe_dir;
bool run_compactor;
enum fsync_policy_enum fsync_policy;
} benchmark_params_t;
static benchmark_params_t default_params(void) {
return (benchmark_params_t){
.iterations = 10e3,
.max_file_size = MAX_FILE_SIZE_DEFAULT,
.wipe_dir = 0,
.key_sz = 32,
.val_sz = 1 << 10,
.fsync_policy = FSYNC_DISABLED,
};
}
static int run_benchmark(benchmark_params_t params) {
if (params.wipe_dir)
wipe_dir(DATA_DIR, 1);
int rc = 0;
config_t config = config_create(DATA_DIR);
config.fsync_policy = params.fsync_policy;
config.max_file_size = params.max_file_size;
store_t *store = store_create(config);
if (!store) {
perror("store_create");
exit(1);
}
char *key = malloc(params.key_sz + 1);
char *val = malloc(params.val_sz + 1);
assert(key);
assert(val);
if (params.run_compactor) {
rc = store_start_compactor(store);
if (rc) {
perror("start compactor");
exit(1);
}
}
long start = clock_mono_us();
for (int i = 0; i < params.iterations; i++) {
snprintf(key, params.key_sz, "key-%0*d", params.key_sz - 5, i);
snprintf(val, params.val_sz, "val-%0*d", params.key_sz - 5, i);
int rc = store_put(store, key, val, params.val_sz);
if (rc) {
fprintf(stderr, "put failed: %s\n", strerror(-rc));
exit(1);
}
}
double n_secs = (clock_mono_us() - start) / 1e6;
fprintf(stderr, "%d writes in %f - %f IOPS\n", params.iterations, n_secs,
params.iterations / n_secs);
if (params.run_compactor)
store_stop_compactor(store);
start = clock_mono_us();
for (int i = 0; i < params.iterations; i++) {
// remove sequential access bias
int j = rand() % params.iterations;
snprintf(key, params.key_sz, "key-%0*d", params.key_sz - 5, j);
char *got = NULL;
size_t val_out;
int rc = store_get(store, key, &got, &val_out);
if (rc) {
fprintf(stderr, "get failed: %s\n", strerror(-rc));
exit(1);
}
free(got);
}
n_secs = (clock_mono_us() - start) / 1e6;
fprintf(stderr, "%d reads in %f - %f IOPS\n", params.iterations, n_secs,
params.iterations / n_secs);
store_destroy(store);
return 0;
}
int parse_int(const char *name) {
int val = strtol(optarg, NULL, 10);
if (val <= 0) {
fprintf(stderr, "Failed to parse %s: invalid value", name);
exit(EXIT_FAILURE);
}
return val;
}
int main(int argc, char *argv[]) {
benchmark_params_t params = default_params();
int arg;
while ((arg = getopt(argc, argv, "v:k:n:m:f:wc")) != -1) {
switch (arg) {
case 'n':
params.iterations = parse_int("n");
break;
case 'k':
params.key_sz = parse_int("key_sz");
break;
case 'v':
params.val_sz = parse_int("val_sz");
break;
case 'm':
params.max_file_size = parse_int("max_file_size");
break;
case 'w':
params.wipe_dir = true;
break;
case 'c':
params.run_compactor = true;
break;
case 'f':
if (strncmp(optarg, "always", 7) == 0)
params.fsync_policy = FSYNC_ALWAYS;
else if (strncmp(optarg, "every_sec", 9) == 0)
params.fsync_policy = FSYNC_EVERY_SEC;
else
params.fsync_policy = FSYNC_DISABLED;
break;
case '?':
default:
fprintf(stderr, "invalid usage\n");
return -1;
}
}
return run_benchmark(params);
}