Skip to content

Instantly share code, notes, and snippets.

@1yx
Last active January 12, 2017 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1yx/303b1cccc629e6c5adc335366dd537d6 to your computer and use it in GitHub Desktop.
Save 1yx/303b1cccc629e6c5adc335366dd537d6 to your computer and use it in GitHub Desktop.
检查几种zset的插入方式 所需要的耗时
/**
* @link https://github.com/NodeRedis/node_redis/issues/659#issuecomment-56067673
* @link https://github.com/NodeRedis/node_redis/issues/539#issuecomment-32203325
*/
'use strict';
const crypto = require('crypto');
const _ = require('lodash');
const Promise = require('bluebird');
const redis = require('redis');
const redisConfig = {
host: '127.0.0.1',
port: 6379,
db: 2
};
const syncClient = redis.createClient(redisConfig);
const asyncClient = Promise.promisifyAll(redis).createClient(redisConfig);
const key = 'test:zset';
const md5 = (str) => {
if (!_.isString(str)) {
str = str.toString();
}
return crypto.createHash('md5').update(str).digest('hex');
};
let now = Date.now();
_.each(_.range(50000), (score) => {
syncClient.zadd(key + ':1', score, md5(score));
});
console.log('use pipeline cost: \t' + (Date.now() - now) / 1000, 's');
now = Date.now();
Promise.mapSeries(_.range(50000), (score) => {
return asyncClient.zaddAsync(key + ':2', score, md5(score));
}).then((row) => {
console.log('direct cost:\t\t' + (Date.now() - now) / 1000, 's');
let multi = asyncClient.multi();
now = Date.now();
_.each(_.range(50000), (score) => {
return multi.zadd(key + ':3', score, md5(score));
});
return multi.execAsync();
}).then((rst, row) => {
console.log('multi cost:\t\t' + (Date.now() - now) / 1000, 's');
let batch = asyncClient.batch();
now = Date.now();
_.each(_.range(50000), (score) => {
return batch.zadd(key + ':4', score, md5(score));
});
return batch.execAsync();
}).then(() => {
console.log('batch cost:\t\t' + (Date.now() - now) / 1000, 's');
now = Date.now();
return Promise.mapSeries(_.range(5), (i) => {
let multi = asyncClient.multi();
_.each(_.range(i, (i + 1) * 10000), (score) => {
return multi.zadd(key + ':5', score, md5(score));
});
return multi.execAsync();
});
}).then((rst, row) => {
console.log('group multi cost:\t' + (Date.now() - now) / 1000, 's');
now = Date.now();
return Promise.mapSeries(_.range(5), (i) => {
let batch = asyncClient.batch();
_.each(_.range(i, (i + 1) * 10000), (score) => {
return batch.zadd(key + ':6', score, md5(score));
});
return batch.execAsync();
});
}).then((rst, row) => {
console.log('group bratch cost:\t' + (Date.now() - now) / 1000, 's');
return;
}).catch((err) => {
console.log(err);
process.exit(1);
}).done(() => {
process.exit(0);
});
/*
use pipeline cost: 0.295 s
direct cost: 10.206 s
multi cost: 1.145 s
batch cost: 1.685 s
group multi cost: 3.606 s
group bratch cost: 3.979 s
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment