forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
berktest.c
288 lines (247 loc) · 7.78 KB
/
berktest.c
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
#include <build/db.h>
#include "bdb_int.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/time.h>
#include <assert.h>
#include <pthread.h>
#include <gettimeofday_ms.h>
#include <errno.h>
#include <logmsg.h>
static DB_ENV *dbenv = NULL;
static u_int32_t commit_delay_ms = 2;
/* Common among tables */
typedef struct globalopts {
u_int8_t checksums;
u_int8_t encrypt;
u_int8_t lorder;
u_int8_t nosync;
} global_t;
global_t globals = {1, 0, 1, 1};
/* Table specific options */
typedef struct berktable {
char name[64];
u_int32_t pagesize;
int keysize;
int datasize;
DB *dbp;
} berktable_t;
static inline int set_global_flags(DB *dbp)
{
if (globals.checksums)
if (dbp->set_flags(dbp, DB_CHKSUM) != 0)
abort();
if (globals.encrypt)
if (dbp->set_flags(dbp, DB_ENCRYPT) != 0)
abort();
if (globals.lorder)
if (dbp->set_lorder(dbp, 4321) != 0)
abort();
return 0;
}
static int create_and_open(berktable_t *table)
{
int rc;
DB_TXN *tid;
if ((rc = db_create(&table->dbp, dbenv, 0)) != 0) {
logmsg(LOGMSG_ERROR, "%s db_create error: %d\n", __func__, rc);
return rc;
}
set_global_flags(table->dbp);
table->dbp->set_pagesize(table->dbp, table->pagesize);
if ((rc = dbenv->txn_begin(dbenv, NULL, &tid, 0)) != 0) {
logmsg(LOGMSG_ERROR, "%s txn_begin error: %d\n", __func__, rc);
return rc;
}
if ((rc = table->dbp->open(table->dbp, tid, table->name, NULL, DB_BTREE,
DB_THREAD | DB_CREATE | DB_DATAFILE, 0666)) !=
0) {
logmsg(LOGMSG_ERROR, "%s open error: %d\n", __func__, rc);
return rc;
}
if ((rc = tid->commit(tid, 0)) != 0) {
logmsg(LOGMSG_ERROR, "%s txn commit error: %d\n", __func__, rc);
return rc;
}
return 0;
}
static void close_tables(berktable_t *tables, int tablecount)
{
int i, rc;
char new[PATH_MAX];
berktable_t *table;
for (i = 0; i < tablecount; i++) {
table = &tables[i];
if (table->dbp) {
if ((rc = table->dbp->close(table->dbp, 0)) != 0) {
logmsg(LOGMSG_ERROR, "%s close error: %d\n", __func__, rc);
continue;
}
unlink(bdb_trans(table->name, new));
}
}
free(tables);
}
static berktable_t *create_tables(int *tablecount)
{
berktable_t *tables = calloc(sizeof(berktable_t), 2), *table;
u_int32_t uniq = getpid() * random();
char new[PATH_MAX];
int count, rc;
/* Index */
count = 0;
table = &tables[count];
snprintf(table->name, sizeof(table->name), "XXX._berktest%d.index", uniq);
unlink(bdb_trans(table->name, new));
table->pagesize = 4096;
table->keysize = 13;
table->datasize = 8;
if ((rc = create_and_open(table)) != 0) {
close_tables(tables, count);
free(tables);
return NULL;
}
/* Data file */
count++;
table = &tables[count];
snprintf(table->name, sizeof(table->name), "XXX._berktest%d.data", uniq);
unlink(bdb_trans(table->name, new));
table->pagesize = 4096;
table->keysize = 8;
table->datasize = 12;
if ((rc = create_and_open(table)) != 0) {
close_tables(tables, count);
free(tables);
return NULL;
}
*tablecount = count + 1;
return tables;
}
static u_int32_t maxkey(berktable_t *tables, int tablecount)
{
u_int32_t max = 0;
int i;
for (i = 0; i < tablecount; i++) {
berktable_t *table = &tables[i];
if (table->keysize > max)
max = table->keysize;
}
return max;
}
static u_int32_t maxdata(berktable_t *tables, int tablecount)
{
u_int32_t max = 0;
int i;
for (i = 0; i < tablecount; i++) {
berktable_t *table = &tables[i];
if (table->datasize > max)
max = table->datasize;
}
return max;
}
static void run_test(berktable_t *tables, int tablecount, int txnsize,
int iterations)
{
uint64_t start, end, totalrecs = (txnsize * iterations), persecond;
DB_TXN *tid;
DBT key = {0}, data = {0};
u_int32_t commit_flags, mkey, mdata, *keyptr;
int i, j, k, rc;
mkey = maxkey(tables, tablecount);
mdata = maxdata(tables, tablecount);
key.data = calloc(mkey, 1);
data.data = calloc(mdata, 1);
keyptr = (u_int32_t *)key.data;
*keyptr = 0;
start = gettimeofday_ms();
commit_flags = globals.nosync ? DB_TXN_NOSYNC : 0;
for (i = 0; i < iterations; i++) {
if ((rc = dbenv->txn_begin(dbenv, NULL, &tid, 0)) != 0) {
logmsg(LOGMSG_ERROR, "%s txn_begin error: %d iteration %d\n", __func__,
rc, i);
return;
}
for (j = 0; j < txnsize; j++) {
for (k = 0; k < tablecount; k++) {
berktable_t *table = &tables[k];
key.size = table->keysize;
data.size = table->datasize;
if ((rc = table->dbp->put(table->dbp, tid, &key, &data, 0)) !=
0) {
logmsg(LOGMSG_ERROR, "%s error putting record, %d\n", __func__,
rc);
tid->abort(tid);
return;
}
}
(*keyptr)++;
}
if ((rc = tid->commit(tid, commit_flags)) != 0) {
logmsg(LOGMSG_ERROR, "%s txn commit error: %d iteration %d\n", __func__,
rc, i);
return;
}
if (commit_delay_ms) {
usleep(commit_delay_ms * 1000);
}
}
end = gettimeofday_ms();
persecond = (1000 * totalrecs) / (end - start);
logmsg(LOGMSG_USER,
"%" PRIu64 " records per second for txnsize %d, commit-delay %d "
"iterations: %d total time %" PRIu64 "\n",
persecond, txnsize, commit_delay_ms, iterations, end - start);
fflush(stdout);
}
static void berktest(int txnsize, int iterations)
{
berktable_t *tables;
int tablecount;
if ((tables = create_tables(&tablecount)) == NULL) {
logmsg(LOGMSG_ERROR, "%s couldn't create tables\n", __func__);
return;
}
run_test(tables, tablecount, txnsize, iterations);
close_tables(tables, tablecount);
}
void bdb_berktest_multi(void *_bdb_state)
{
bdb_state_type *bdb_state = _bdb_state;
dbenv = bdb_state->dbenv;
berktable_t *tables;
int tablecount, iterations = 50;
if ((tables = create_tables(&tablecount)) == NULL) {
logmsg(LOGMSG_ERROR, "%s couldn't create tables\n", __func__);
return;
}
run_test(tables, tablecount, 500000, iterations);
run_test(tables, tablecount, 200000, iterations);
run_test(tables, tablecount, 100000, iterations);
run_test(tables, tablecount, 50000, iterations);
run_test(tables, tablecount, 20000, iterations);
run_test(tables, tablecount, 10000, iterations);
run_test(tables, tablecount, 5000, iterations);
run_test(tables, tablecount, 2000, iterations);
run_test(tables, tablecount, 1000, iterations);
run_test(tables, tablecount, 500, iterations);
run_test(tables, tablecount, 200, iterations);
run_test(tables, tablecount, 100, iterations);
run_test(tables, tablecount, 50, iterations);
run_test(tables, tablecount, 20, iterations);
run_test(tables, tablecount, 10, iterations);
run_test(tables, tablecount, 5, iterations);
run_test(tables, tablecount, 2, iterations);
run_test(tables, tablecount, 1, iterations);
close_tables(tables, tablecount);
}
void bdb_berktest_commit_delay(u_int32_t delayms) { commit_delay_ms = delayms; }
void bdb_berktest(void *_bdb_state, u_int32_t txnsize)
{
bdb_state_type *bdb_state = _bdb_state;
dbenv = bdb_state->dbenv;
berktest(txnsize, 50);
}