-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbtestx.c
More file actions
185 lines (162 loc) · 4.68 KB
/
dbtestx.c
File metadata and controls
185 lines (162 loc) · 4.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) 2016--2021 Wu, Xingbo <wuxb45@gmail.com>
*
* All rights reserved. No warranty, explicit or implicit, provided.
*/
#define _GNU_SOURCE
#include "lib.h"
#include "kv.h"
struct priv {
void * ref;
u32 cpro;
u32 cget;
u32 cset;
u32 klen;
u32 vlen;
struct kv * out;
struct kv * tmp;
};
#define XSA ((0))
#define XSS ((1))
#define XDA ((2))
#define XDS ((3))
#define XGA ((4))
#define XGS ((5))
#define XPA ((6))
#define XPS ((7))
static bool
kvmap_analyze(void * const passdata[2], const u64 dt, const struct vctr * const va, struct damp * const d, char * const out)
{
(void)passdata;
size_t v[8];
for (u64 i = 0; i < 8; i++)
v[i] = vctr_get(va, i);
const u64 nrop = v[XSA] + v[XDA] + v[XGA] + v[XPA];
const double mops = ((double)nrop) * 1e3 / ((double)dt);
const bool done = damp_add_test(d, mops);
const char * const pat = " set %zu %zu del %zu %zu get %zu %zu pro %zu %zu mops %.4lf avg %.4lf ravg %.4lf\n";
sprintf(out, pat, v[XSA], v[XSS], v[XDA], v[XDS], v[XGA], v[XGS], v[XPA], v[XPS], mops, damp_avg(d), damp_ravg(d));
return done;
}
static void
kvmap_batch(const struct forker_worker_info * const info,
const struct priv * const priv, const u64 nr)
{
const struct kvmap_api * const api = (typeof(api))info->passdata[0];
void * const ref = priv->ref;
struct vctr * const v = info->vctr;
struct rgen * const gen = info->gen;
rgen_next_func next = info->rgen_next;
rgen_next_func next_write = info->rgen_next_write;
struct kv * const tmp = priv->tmp;
for (u64 i = 0; i < nr; i++) {
const u32 p = random_u64() & 0xffffu;
if (p < priv->cpro) { // PROBE
kv_refill_hex64_klen(tmp, next(gen), priv->klen, NULL, 0);
vctr_add1(v, XPA);
if (kvmap_kv_probe(api, ref, tmp))
vctr_add1(v, XPS);
} else if (p < priv->cget) { // GET
kv_refill_hex64_klen(tmp, next(gen), priv->klen, NULL, 0);
vctr_add1(v, XGA);
if (kvmap_kv_get(api, ref, tmp, priv->out))
vctr_add1(v, XGS);
} else if (p < priv->cset) { // SET
kv_refill_hex64_klen(tmp, next_write(gen), priv->klen, NULL, 0);
vctr_add1(v, XSA);
tmp->vlen = priv->vlen;
if (kvmap_kv_put(api, ref, tmp))
vctr_add1(v, XSS);
} else { // DELETE
kv_refill_hex64_klen(tmp, next_write(gen), priv->klen, NULL, 0);
vctr_add1(v, XDA);
if (kvmap_kv_del(api, ref, tmp))
vctr_add1(v, XDS);
}
}
}
static void *
kvmap_worker(void * const ptr)
{
struct forker_worker_info * const info = (typeof(info))ptr;
srandom_u64(info->seed);
const struct kvmap_api * const api = (typeof(api))info->passdata[0];
struct priv p;
const u32 pset = a2u32(info->argv[0]);
const u32 pdel = a2u32(info->argv[1]);
const u32 pget = a2u32(info->argv[2]);
const u32 ppro = 100 - pset - pdel - pget;
// scaled to 65536
p.cpro = ppro * 65536 / 100;
p.cget = (ppro + pget) * 65536 / 100;
p.cset = (ppro + pget + pset) * 65536 / 100;
p.klen = a2u32(info->argv[3]);
p.vlen = a2u32(info->argv[4]);
p.ref = kvmap_ref(api, info->passdata[1]);
const u64 outlen = sizeof(struct kv) + 16 + p.klen + p.vlen + 4096;
p.tmp = yalloc(outlen);
memset(p.tmp, 0, outlen);
p.out = yalloc(outlen);
debug_assert(p.out);
if (info->end_type == FORKER_END_TIME) {
do {
kvmap_batch(info, &p, 1lu << 14);
} while (time_nsec() < info->end_magic);
} else if (info->end_type == FORKER_END_COUNT) {
kvmap_batch(info, &p, info->end_magic);
}
kvmap_unref(api, p.ref);
free(p.tmp);
free(p.out);
return NULL;
}
#define NARGS ((5))
static void
maptest_help_message(void)
{
fprintf(stderr, "%s Usage: {api ... {rgen ... {pass ...}}}\n", __func__);
kvmap_api_helper_message();
forker_passes_message();
fprintf(stderr, "%s dbtestx wargs[%d]: <pset> <pdel> <pget> <klen> <vlen>\n", __func__, NARGS);
}
static int
test_kvmap(const int argc, char ** const argv)
{
const struct kvmap_api * api = NULL;
void * map = NULL;
const int n1 = kvmap_api_helper(argc, argv, NULL, &api, &map);
if (n1 < 0)
return n1;
char *pref[64] = {};
for (int i = 0; i < n1; i++) {
pref[i] = argv[i];
}
pref[n1] = NULL;
struct pass_info pi = {};
pi.passdata[0] = (void *)api;
pi.passdata[1] = map;
pi.vctr_size = 8;
pi.wf = kvmap_worker;
pi.af = kvmap_analyze;
const int n2 = forker_passes(argc - n1, argv + n1, pref, &pi, NARGS);
if (api->fprint)
api->fprint(map, stderr);
api->destroy(map);
if (n2 < 0) {
return n2;
} else {
return n1 + n2;
}
}
int
main(int argc, char ** argv)
{
if (argc < 3) {
maptest_help_message();
exit(0);
}
const bool r = forker_main(argc - 1, argv + 1, test_kvmap);
if (r == false)
maptest_help_message();
return 0;
}