-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark
executable file
·388 lines (311 loc) · 11.9 KB
/
benchmark
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env node
const os = require('os');
const fs = require('fs').promises;
const { argv } = require('yargs');
const Redis = require('ioredis');
const TwoD = require('../');
function hostinfo() {
const r = {
arch: os.arch(),
cpus: os.cpus().reduce((a, x) => {
const k = `${x.model}/${x.speed}`;
a[k] = k in a ? a[k] + 1 : 1;
return a;
}, {}),
mem: {
free: os.freemem(),
total: os.totalmem()
},
os: {
name: os.platform(),
release: os.release()
}
}
if ('version' in os) {
r.os.version = os.version();
}
return r;
}
const redisInfoFilterAllowed = ['redis_version', 'redis_build_id', 'redis_mode', 'os', 'arch_bits',
'used_memory', 'used_memory_rss', 'used_memory_peak', 'total_system_memory', 'mem_fragmentation_ratio'];
const redisInfoFilter = (element) => redisInfoFilterAllowed.indexOf(element[0]) !== -1;
const T = {
p: {
tf: 4,
pad: 5
},
run: async (name, exec) => {
console.log(`# ${T.c(true)} ${name}`);
const start = process.hrtime.bigint();
const result = await exec();
const ret = { name, result, timeNs: Number(process.hrtime.bigint() - start) };
console.log(`##${''.padStart(T.p.pad, ' ')} ${(Number(ret.timeNs) / 1e9).toFixed(T.p.tf)}s` +
(result && typeof result !== 'function' ? `, returned "${JSON.stringify(result, null, 2)}"` : ''));
return ret;
},
seq: {
main: mainSequence
},
c: (i) => `${String(++T._c).padStart(T.p.pad, '0')}`,
_c: 0,
};
async function mainSequence(key, redisConn, chunkWidth, multiplier = 1) {
const bitmap = new TwoD.SparseBitmap({
[TwoD.BackingStoreKey]: redisConn,
[TwoD.ChunkWidthKey]: chunkWidth
});
if (!bitmap.isPipelineCapable) {
throw new Error('mainSequence must be run with a pipeline-capable backing store');
}
const lim = bitmap[TwoD.ChunkWidthKey] * multiplier;
const results = [];
const findAllBounds = TwoD.Util.Bounds.fromArray([[0,0], [lim,lim]]);
const findAllClosure = async (k) => {
const inBounds = await bitmap.inBounds(k, findAllBounds);
if (inBounds.length !== lim*lim) {
throw new Error('len');
}
return inBounds.length;
};
const xAll = async (v, k, m, p = false) => {
const actStr = v === 1 ? '' : 'un';
results.push((await T.run(`${actStr}set all ${lim}x${lim}/${lim*lim} bits${(m ? ` (${m})` : '')}`, async () => {
for (let x = 0; x < lim; x++) {
const exec = (v === 1 ? bitmap.set : bitmap.unset).bind(bitmap);
const rowAll = async () => {
for (let y = 0; y < lim; y++) {
await exec(k, x, y);
}
};
await (p ? bitmap.pipelinedMutate(rowAll) : rowAll());
}
})));
};
const setPercentageRand = async (l, a, k, m, p = false) => {
const spRand = () => Math.floor(Math.random() * l);
let num = Math.round((l*l) * (a > 1.0 ? (1.0 / a) : (a / 100.0)));
const tracker = {};
results.push((await T.run(`random set ${a}% in ${l}x${l}/${num} bits${(m ? ` (${m})` : '')}`, async () => {
while (num > 0) {
const [rX, rY] = [spRand(), spRand()];
const rKey = `${rX},${rY}`;
// don't allow the same point to be included more than once
if (!(rKey in tracker)) {
tracker[rKey] = [rX, rY];
--num;
}
}
const runner = async () => {
for (let coord of Object.values(tracker)) {
await bitmap.set(k, coord[0], coord[1]);
}
};
await (p ? bitmap.pipelinedMutate(runner) : runner());
})));
return tracker;
};
const setAll = xAll.bind(null, 1);
const unsetAll = xAll.bind(null, 0);
await setAll(key, 'pipeline', true);
results.push((await T.run(`find all in ${TwoD.Util.Bounds.toString(findAllBounds)} (pipeline)`, findAllClosure.bind(null, key))));
await unsetAll(key, 'pipeline', true);
const npKey = `${key}:np`;
await setAll(npKey, 'no-pipeline');
bitmap.isPipelineCapable = false;
results.push((await T.run(`find all in ${TwoD.Util.Bounds.toString(findAllBounds)} (no-pipeline)`, findAllClosure.bind(null, npKey))));
bitmap.isPipelineCapable = true;
await unsetAll(npKey, 'no-pipeline');
const prcntRandRound = async (nPrcnt, nLim) => {
nLim *= lim;
const nPRBounds = {
from: { x: 0, y: 0 },
to: { x: nLim, y: nLim }
};
let nPRKey = `${key}:${nLim}:${nPrcnt}:rand-P`;
let nPRTracked = await setPercentageRand(nLim, nPrcnt, nPRKey, 'pipeline', true);
results.push((await T.run(`find ${nPrcnt}% in ${TwoD.Util.Bounds.toString(nPRBounds)} (pipeline)`, async () => {
const inBounds = await bitmap.inBounds(nPRKey, nPRBounds);
if (Object.keys(nPRTracked).length !== inBounds.length) {
throw new Error(`lengths! ${Object.keys(nPRTracked).length} vs ${inBounds.length}`);
}
})));
nPRKey = `${key}:${nLim}:${nPrcnt}:rand-NP`;
nPRTracked = await setPercentageRand(nLim, nPrcnt, nPRKey, 'no-pipeline');
bitmap.isPipelineCapable = false;
results.push((await T.run(`find ${nPrcnt}% in ${TwoD.Util.Bounds.toString(nPRBounds)} (no-pipeline)`, async () => {
const inBounds = await bitmap.inBounds(nPRKey, nPRBounds);
if (Object.keys(nPRTracked).length !== inBounds.length) {
throw new Error(`lengths! ${Object.keys(nPRTracked).length} vs ${inBounds.length}`);
}
})));
bitmap.isPipelineCapable = true;
};
await prcntRandRound(0.01, 100);
await prcntRandRound(1, 10);
await prcntRandRound(0.25, 20);
await prcntRandRound(0.001, 200);
await prcntRandRound(0.1, 20);
await prcntRandRound(0.025, 40);
const halfWidth = bitmap[TwoD.ChunkWidthKey] / 2;
const onePFiveWidth = halfWidth + bitmap[TwoD.ChunkWidthKey];
const strideSet = async (stride, nLim, boundsTo, p) => {
nLim *= multiplier;
const ssKey = `${key}:${nLim}:${stride}:ss:${p ? 'P' : 'NP'}`;
const nBits = nLim * nLim;
const findBounds = { from: { x: -1, y: -1 }, to: boundsTo };
results.push((await T.run(`stride-set:${stride} over ${nLim}x${nLim}/${nBits}/~${Math.ceil(nBits / stride)} bits (${p ? '' : 'no-'}pipeline)`, async () => {
const yTrack = { current: 0, buffer: [], total: 0 };
for (let count = 0; count <= nBits; count += stride) {
const x = count % nLim;
const y = Math.floor(count / nLim);
yTrack.buffer.push([x, y]);
if (y !== yTrack.current) {
if (yTrack.current === 0) {
findBounds.to.x += (findBounds.from.x = x + halfWidth);
findBounds.to.y += (findBounds.from.y = y + halfWidth);
}
const rowChunk = async () => {
for (let bufPoint of yTrack.buffer) {
await bitmap.set(ssKey, bufPoint[0], bufPoint[1]);
}
};
await (p ? bitmap.pipelinedMutate(rowChunk) : rowChunk());
yTrack.current = y;
yTrack.total += yTrack.buffer.length;
yTrack.buffer = [];
}
}
return yTrack.total;
})));
results.push((await T.run(`stride-set:${stride} - find in bounds: ${TwoD.Util.Bounds.toString(findBounds)} (${p ? '' : 'no-'}pipeline)`, async () => {
if (!p) {
bitmap.isPipelineCapable = false;
}
const inB = await bitmap.inBounds(ssKey, findBounds);
if (!p) {
bitmap.isPipelineCapable = true;
}
return inB.length;
})));
};
await strideSet(11, onePFiveWidth, { x: onePFiveWidth, y: onePFiveWidth }, true);
await strideSet(11, onePFiveWidth, { x: onePFiveWidth, y: onePFiveWidth }, false);
await strideSet(919, 900, { x: 960, y: 640 }, true);
await strideSet(919, 900, { x: 960, y: 640 }, false);
await strideSet(4243, 1440, { x: 2560, y: 1440 }, true);
await strideSet(4243, 1440, { x: 2560, y: 1440 }, false);
await strideSet(101111, 42221, { x: 4096, y: 2048 }, true);
await strideSet(101111, 42221, { x: 4096, y: 2048 }, false);
return results;
}
async function main() {
const mult = argv.m || argv.mult || 1;
const host = argv.h || argv.host || 'localhost';
const port = argv.p || argv.port || 6379;
const auth = argv.a || argv.auth;
const db = argv.n || argv.db;
const wdth = argv.c || argv.chunkWidth || TwoD.Defaults[TwoD.ChunkWidthKey];
const iter = argv.i || argv.iter || 3;
const wait = argv.w || argv.wait || 31; // seconds
const redisOpts = {};
if (auth) {
redisOpts.password = auth;
}
if (db) {
redisOpts.db = db;
}
const redisConn = new Redis(host, port, redisOpts);
const report = {
host: hostinfo(),
node: Object.entries(process.versions).filter(e => ['node', 'v8'].indexOf(e[0]) !== -1).reduce((a, x) => ({ [x[0]]: x[1], ...a}), {}),
redis: { host, port },
run: {
wait,
multiplier: mult,
chunkWidth: wdth,
iterations: iter,
complete: false,
},
results: []
};
redisConn.on('ready', async () => {
const overallStart = process.hrtime.bigint();
report.redis.info = Object.entries(redisConn.serverInfo).filter(redisInfoFilter).reduce((a, x) => ({ [x[0]]: x[1], ...a}), {});
console.log(`Benchmarking with c:${wdth}/m:${mult} on "${report.host.os.name}-${report.host.arch} ${report.host.os.release}; ` +
`redis ${report.redis.info.redis_version} (${report.redis.host}); node ${report.node.node}"`);
const key = `benchmark:${Date.now()}`;
const delAllKeys = async () => {
report.results.push((await T.run(`delete all '*${key}*'`, async () => {
const keys = await redisConn.keys(`*${key}*`);
const chunkSize = 1e5;
let remain = keys.length;
let off = 0;
while (remain > 0) {
const cSize = Math.min(remain, chunkSize);
await redisConn.del(...keys.slice(off, off + cSize));
off += cSize;
remain -= cSize;
}
return keys.length;
})));
}
const cleanup = async (sig) => {
if (sig) {
console.log('\nInterrupted! Cleaning up immediately...');
await delAllKeys();
T.run = () => {};
}
const reportPath = `report.${(new Date().toISOString()).replace(/[-:.]/g, '')}.json`;
await fs.writeFile(reportPath, JSON.stringify(report, null, 2));
report.run.timeSec = Number(process.hrtime.bigint() - overallStart) / 1e9;
console.log(`\nDone in ${report.run.timeSec.toFixed(2)}s, report written to: ${reportPath}`);
if (sig) {
process.exit();
}
};
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
process.on('SIGHUP', cleanup);
const iteration = async (i, completeCb) => {
console.log(`\n*** Iteration ${(iter-i)+1} ***`);
report.results = report.results.concat((await T.seq.main(key, redisConn, wdth, mult)));
await delAllKeys();
if (--i > 0) {
setTimeout(() => iteration(i, completeCb), wait * 1000);
} else {
await completeCb();
}
};
await iteration(iter, async () => {
if (iter > 1) {
const consolidate = report.results.reduce((accum, rep) => {
if (!(rep.name in accum)) {
accum[rep.name] = [];
}
accum[rep.name].push(rep);
return accum;
}, {});
report.results = Object.keys(consolidate).map((name) => {
const repList = consolidate[name];
const timeNsMean = repList.reduce((a, x) => a + x.timeNs, 0) / repList.length;
const timeNsStdDev = Math.sqrt(repList.map((x) => (x.timeNs - timeNsMean) ** 2).reduce((a, x) => a + x) / repList.length);
const timeNsStdDevRatio = timeNsStdDev / timeNsMean;
return {
name,
timeNsMean,
timeMeanHuman: `${(timeNsMean / 1e9).toFixed(T.p.tf)}s`,
timeNsStdDev,
timeStdDevHuman: `${(timeNsStdDev / 1e9).toFixed(T.p.tf+1)}s`,
timeNsStdDevRatio,
count: repList.length,
raw: repList
};
});
}
report.run.complete = true;
await cleanup();
redisConn.disconnect();
});
});
}
main();