Skip to content

Instantly share code, notes, and snippets.

@ninjitaru
Last active March 13, 2019 16:32
Show Gist options
  • Save ninjitaru/a19cd78f26e1a517d6907d0a9e874c6a to your computer and use it in GitHub Desktop.
Save ninjitaru/a19cd78f26e1a517d6907d0a9e874c6a to your computer and use it in GitHub Desktop.
const { redis, getSubscriber, subscribeWithSubscriber, publish } = require('applib/redis');
const _ = require('lodash');
// 1 ~ 80ms
// 10 ~ 80ms
// 100 ~ 80ms
// 1000 ~ 90ms
// 10000 ~ 200ms
// spread across 10 client
// i am able to achieve ~80ms for 10000 sub with 1000 per client
(async () => {
const clients = [
redis.duplicate(),
redis.duplicate(),
redis.duplicate(),
redis.duplicate(),
redis.duplicate(),
redis.duplicate(),
redis.duplicate(),
redis.duplicate(),
redis.duplicate(),
redis.duplicate(),
];
const nameCount = 5000;
const messageCount = 1000;
let count = 0;
let start;
let cid = 0;
let subworks = [];
let total = 0;
for (const client of clients) {
client.setMaxListeners(10000);
cid = cid + 1;
const names = _.range(1, nameCount + 1).map(id => `abc${id}-c${cid}`);
const subs = names.map(name => {
if (name === 'abc1-c1') {
return subscribeWithSubscriber(client, name, (pattern, message) => {
total++;
if (pattern === 'abc1-c1') {
count++;
if (count >= messageCount) {
console.log(`took ${Date.now() - start}ms`);
console.log(`total received ${total}`);
clients.forEach(c => c.quit());
}
}
});
} else {
return subscribeWithSubscriber(client, name, (pattern, message) => {
// do nothing
total++;
});
}
});
subworks = subworks.concat(subs);
}
await Promise.all(subworks);
console.log(`completed sub ${nameCount * clients.length} names`);
start = Date.now();
await Promise.all(_.range(1, messageCount + 1).map(id => publish('abc1-c1', id)));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment