Skip to content

Instantly share code, notes, and snippets.

@jesugmz
Last active January 16, 2020 16:00
Show Gist options
  • Save jesugmz/e9cdc9bfe12086768125af3c73944d55 to your computer and use it in GitHub Desktop.
Save jesugmz/e9cdc9bfe12086768125af3c73944d55 to your computer and use it in GitHub Desktop.
Benchmark Redis clients for Node

Benchmark Redis clients for Node

  1. node_redis
  2. ioredis

Result

Node Redis:  OK
ioredis:  OK
Bench start:(100000)


PING concurrency(full thread):
Node Redis: 2377ms 42069ops/sec 100%
ioredis: 3334ms 29994ops/sec 71.3%

PING concurrency(1000 thread):
Node Redis: 2218ms 45085ops/sec 100%
ioredis: 2921ms 34234ops/sec 75.9%

PING sequential 1 by 1:
Node Redis: 9017ms 11090ops/sec 100%
ioredis: 10061ms 9939ops/sec 89.6%

SET small string:
Node Redis: 2181ms 45850ops/sec 100%
ioredis: 3295ms 30349ops/sec 66.2%

GET small string:
Node Redis: 2326ms 42992ops/sec 100%
ioredis: 3269ms 30590ops/sec 71.2%

SET long string:
Node Redis: 2706ms 36954ops/sec 100%
ioredis: 3963ms 25233ops/sec 68.3%

GET long string:
Node Redis: 4503ms 22207ops/sec 100%
ioredis: 5037ms 19853ops/sec 89.4%

INCR:
Node Redis: 2179ms 45892ops/sec 100%
ioredis: 3443ms 29044ops/sec 63.3%

LPUSH:
Node Redis: 2285ms 43763ops/sec 100%
ioredis: 3442ms 29052ops/sec 66.4%

LRANGE 100:
Node Redis: 6225ms 16064ops/sec 100%
ioredis: 9139ms 10942ops/sec 68.1%

Script based on https://github.com/thunks/thunk-redis/blob/master/benchmark/index.js

'use strict';
const thunk = require('thunks')();
const nodeRedis = require('redis');
const IoRedis = require('ioredis');
// test in thunks(thunk base)
thunk(bench)(console.log.bind(console));
function * bench () {
let timeN = 0;
let timeI = 0;
const testLen = 100000;
const titleN = 'Node Redis:';
const titleI = 'ioredis:';
const clientN = nodeRedis.createClient(6379, 'cache1');
const clientI = new IoRedis(6379, 'cache2');
const queue = [];
while (queue.length < testLen) queue.push(queue.length);
const smallStr = 'teambition';
const longStr = (new Array(4097).join('-'));
function printResult (title, timeN, timeI) {
console.log(`\n${title}:`);
console.log(titleN, `${timeN}ms`, Math.floor(testLen * 1000 / timeN) + 'ops/sec', '100%');
console.log(titleI, `${timeI}ms`, Math.floor(testLen * 1000 / timeI) + 'ops/sec', ((timeN / timeI) * 100).toFixed(1) + '%');
}
console.log(titleN + ' ', yield function (done) { clientN.flushdb(done); });
console.log(titleI + ' ', yield clientI.flushdb());
console.log(`Bench start for ${testLen} loops\n`);
// PING concurrency(full thread)
yield thunk.delay(100);
timeN = Date.now();
yield queue.map(function () {
return function (done) { clientN.ping(done); }
})
timeN = Date.now() - timeN;
yield thunk.delay(100);
timeI = Date.now();
yield queue.map(function () {
return clientI.ping();
})
timeI = Date.now() - timeI;
printResult('PING concurrency(full thread)', timeN, timeI);
// PING concurrency(1000 thread)
yield thunk.delay(100);
timeN = Date.now();
yield function * () {
let count = queue.length;
yield queue.slice(0, 1000).map(function () {
return next;
})
function next (callback) {
if (count > 0) {
count--;
clientN.ping(function (err) {
if (!err) next(callback);
else callback(err);
})
} else callback();
}
}
timeN = Date.now() - timeN;
yield thunk.delay(100);
timeI = Date.now();
yield function * () {
let count = queue.length;
yield queue.slice(0, 1000).map(function () {
return next;
})
function next (callback) {
if (count > 0) {
count--;
clientI.ping()
.then(function () {
next(callback);
})
.catch(callback);
} else callback();
}
}
timeI = Date.now() - timeI;
printResult('PING concurrency(1000 thread)', timeN, timeI);
// PING sequential 1 by 1
yield thunk.delay(100);
timeN = Date.now();
for (let i = queue.length; i > 0; i--) {
yield function (done) { clientN.ping(done); }
}
timeN = Date.now() - timeN;
yield thunk.delay(100);
timeI = Date.now();
for (let i = queue.length; i > 0; i--) {
yield clientI.ping();
}
timeI = Date.now() - timeI;
printResult('PING sequential 1 by 1', timeN, timeI);
// SET small string
yield thunk.delay(100);
timeN = Date.now();
yield queue.map(function () {
return function (done) { clientN.set('zensh_thunks_00000001', smallStr, done); }
})
timeN = Date.now() - timeN;
yield thunk.delay(100);
timeI = Date.now();
yield queue.map(function () {
return clientI.set('zensh_thunks_00000001', smallStr);
})
timeI = Date.now() - timeI;
printResult('SET small string', timeN, timeI);
// GET small string
yield thunk.delay(100);
timeN = Date.now();
yield queue.map(function () {
return function (done) { clientN.get('zensh_thunks_00000001', done); }
})
timeN = Date.now() - timeN;
yield thunk.delay(100);
timeI = Date.now();
yield queue.map(function () {
return clientI.get('zensh_thunks_00000001');
})
timeI = Date.now() - timeI;
printResult('GET small string', timeN, timeI);
// SET long string
yield thunk.delay(100);
timeN = Date.now();
yield queue.map(function () {
return function (done) { clientN.set('zensh_thunks_00000002', longStr, done); }
})
timeN = Date.now() - timeN;
yield thunk.delay(100);
timeI = Date.now();
yield queue.map(function () {
return clientI.set('zensh_thunks_00000002', longStr);
})
timeI = Date.now() - timeI;
printResult('SET long string', timeN, timeI);
// GET long string
yield thunk.delay(100);
timeN = Date.now();
yield queue.map(function () {
return function (done) { clientN.get('zensh_thunks_00000002', done); }
})
timeN = Date.now() - timeN;
yield thunk.delay(100);
timeI = Date.now();
yield queue.map(function () {
return clientI.get('zensh_thunks_00000002');
})
timeI = Date.now() - timeI;
printResult('GET long string', timeN, timeI);
// INCR
yield thunk.delay(100);
timeN = Date.now();
yield queue.map(function () {
return function (done) { clientN.incr('zensh_thunks_00000003', done); }
})
timeN = Date.now() - timeN;
yield thunk.delay(100);
timeI = Date.now();
yield queue.map(function () {
return clientI.incr('zensh_thunks_00000003');
})
timeI = Date.now() - timeI;
printResult('INCR', timeN, timeI);
// LPUSH
yield thunk.delay(100);
timeN = Date.now();
yield queue.map(function () {
return function (done) { clientN.lpush('zensh_thunks_00000004', smallStr, done); }
})
timeN = Date.now() - timeN;
yield thunk.delay(100);
timeI = Date.now();
yield queue.map(function () {
return clientI.lpush('zensh_thunks_00000004', smallStr);
})
timeI = Date.now() - timeI;
printResult('LPUSH', timeN, timeI);
// LRANGE
yield thunk.delay(100);
timeN = Date.now();
yield queue.map(function () {
return function (done) { clientN.lrange('zensh_thunks_00000004', '0', '100', done); }
})
timeN = Date.now() - timeN;
yield thunk.delay(100);
timeI = Date.now();
yield queue.map(function () {
return clientI.lrange('zensh_thunks_00000004', '0', '100');
})
timeI = Date.now() - timeI;
printResult('LRANGE 100', timeN, timeI);
yield thunk.delay(100);
process.exit();
}
{
"dependencies": {
"ioredis": "^4.14.1",
"redis": "^2.8.0",
"thunks": "^4.9.5"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment