|
'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(); |
|
} |