Skip to content

Commit ad29b4b

Browse files
Merge branch 'master' into Support_RedisTimeSeries
2 parents fb3d3df + 41bd136 commit ad29b4b

File tree

10 files changed

+917
-8
lines changed

10 files changed

+917
-8
lines changed

benchmark/lib/index.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import yargs from 'yargs';
2+
import { hideBin } from 'yargs/helpers';
3+
import { promises as fs } from 'fs';
4+
import { fork } from 'child_process';
5+
import { URL, fileURLToPath } from 'url';
6+
import { once } from 'events';
7+
8+
async function getPathChoices() {
9+
const dirents = await fs.readdir(new URL('.', import.meta.url), {
10+
withFileTypes: true
11+
});
12+
13+
const choices = [];
14+
for (const dirent of dirents) {
15+
if (!dirent.isDirectory()) continue;
16+
17+
choices.push(dirent.name);
18+
}
19+
20+
return choices;
21+
}
22+
23+
const argv = hideBin(process.argv);
24+
25+
async function getName() {
26+
return yargs(argv)
27+
.option('name', {
28+
demandOption: true,
29+
choices: await getPathChoices()
30+
})
31+
.parseSync().name;
32+
}
33+
34+
35+
const runnerPath = fileURLToPath(new URL('runner.js', import.meta.url)),
36+
path = new URL(`${await getName()}/`, import.meta.url),
37+
metadata = await import(new URL('index.js', path));
38+
39+
for (const file of await fs.readdir(path)) {
40+
if (file === 'index.js') continue;
41+
42+
const benchmarkProcess = fork(runnerPath, [
43+
...argv,
44+
'--path',
45+
fileURLToPath(path) + file
46+
]);
47+
48+
await once(benchmarkProcess, 'message');
49+
benchmarkProcess.send(metadata);
50+
await once(benchmarkProcess, 'close');
51+
}

benchmark/lib/runner.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import yargs from 'yargs';
2+
import { hideBin } from 'yargs/helpers';
3+
import { basename } from 'path';
4+
5+
const { path, times, concurrency } = yargs(hideBin(process.argv))
6+
.option('path', {
7+
type: 'string',
8+
demandOption: true
9+
})
10+
.option('times', {
11+
type: 'number',
12+
default: 1_000_000,
13+
demandOption: true
14+
})
15+
.option('concurrency', {
16+
type: 'number',
17+
default: 100,
18+
demandOption: true
19+
})
20+
.parseSync();
21+
22+
async function setup() {
23+
const module = await import(path);
24+
await module.setup();
25+
return module;
26+
}
27+
28+
function getMetadata() {
29+
return new Promise(resolve => {
30+
process.once('message', resolve);
31+
process.send('ready');
32+
});
33+
}
34+
35+
const [ { benchmark, teardown }, metadata ] = await Promise.all([
36+
setup(),
37+
getMetadata()
38+
]);
39+
40+
async function run(times) {
41+
return new Promise(resolve => {
42+
let num = 0,
43+
inProgress = 0;
44+
45+
function run() {
46+
++inProgress;
47+
++num;
48+
benchmark(metadata)
49+
.catch(err => console.error(err))
50+
.finally(() => {
51+
--inProgress;
52+
53+
if (num < times) {
54+
run();
55+
} else if (inProgress === 0) {
56+
resolve();
57+
}
58+
});
59+
}
60+
61+
for (let i = 0; i < concurrency; i++) {
62+
run();
63+
}
64+
});
65+
}
66+
67+
// warmup
68+
await run(Math.min(times * 0.1, 10_000));
69+
70+
// benchmark
71+
const start = process.hrtime.bigint();
72+
await run(times);
73+
const took = (process.hrtime.bigint() - start);
74+
console.log(`[${basename(path)}]: took ${took / 1_000_000n}ms, ${took / BigInt(times)}ns per operation`);
75+
76+
await teardown();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import yargs from 'yargs';
2+
import { hideBin } from 'yargs/helpers';
3+
import { randomBytes } from 'crypto';
4+
5+
const { size } = yargs(hideBin(process.argv))
6+
.option('size', {
7+
type: 'number',
8+
default: 1024,
9+
demandOption: true
10+
})
11+
.parseSync();
12+
13+
export const randomString = randomBytes(size).toString('ascii');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Redis from 'ioredis';
2+
3+
const client = new Redis({ lazyConnect: true });
4+
5+
export function setup() {
6+
return client.connect();
7+
}
8+
9+
export function benchmark({ randomString }) {
10+
return Promise.all([
11+
client.set(randomString, randomString),
12+
client.get(randomString),
13+
client.del(randomString)
14+
]);
15+
}
16+
17+
export function teardown() {
18+
return client.disconnect();
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { createClient } from '@node-redis/client-local';
2+
3+
const client = createClient();
4+
5+
export function setup() {
6+
return client.connect();
7+
}
8+
9+
export function benchmark({ randomString }) {
10+
return Promise.all([
11+
client.set(randomString, randomString),
12+
client.get(randomString),
13+
client.del(randomString)
14+
]);
15+
}
16+
17+
export function teardown() {
18+
return client.disconnect();
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { createClient } from '@node-redis/client-production';
2+
3+
const client = createClient();
4+
5+
export function setup() {
6+
return client.connect();
7+
}
8+
9+
export function benchmark({ randomString }) {
10+
return Promise.all([
11+
client.set(randomString, randomString),
12+
client.get(randomString),
13+
client.del(randomString)
14+
]);
15+
}
16+
17+
export function teardown() {
18+
return client.disconnect();
19+
}

0 commit comments

Comments
 (0)