1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-17 19:41:06 +03:00
Files
node-redis/benchmark/index.js
2021-05-12 14:27:02 -04:00

55 lines
1.5 KiB
JavaScript

import * as oldRedis from 'redis-old';
import * as newRedis from 'redis-new';
import { promisify } from 'util';
import { once } from 'events';
const ITERATIONS = 1_000_000;
function benchmarkCallback(name, fn) {
return new Promise(resolve => {
const start = process.hrtime.bigint();
let counter = 0;
function iterationCallback(err) {
if (err) {
console.error(err);
}
if (++counter === ITERATIONS) {
console.log(`${name} took ${process.hrtime.bigint() - start} nanoseconds`);
resolve();
}
}
for (let i = 0; i < ITERATIONS; i++) {
fn(iterationCallback);
}
});
}
async function benchmarkPromise(name, fn) {
const start = process.hrtime.bigint();
const promises = [];
for (let i = 0; i < ITERATIONS; i++) {
promises.push(fn());
}
await Promise.all(promises);
console.log(`${name} took ${process.hrtime.bigint() - start} nanoseconds`);
}
const oldClient = oldRedis.createClient();
oldClient.flushallAsync = promisify(oldClient.flushall).bind(oldClient);
await once(oldClient, 'connect');
await oldClient.flushallAsync();
await benchmarkCallback('oldClient.ping', cb => {
oldClient.ping('key', cb);
});
const newClient = newRedis.createClient();
await newClient.connect();
await newClient.flushAll();
await benchmarkPromise('newClient.ping', () => {
return newClient.ping();
});