Skip to content

Instantly share code, notes, and snippets.

@RWOverdijk
Forked from koszta/sails_performace.js
Last active August 29, 2015 14:10
Show Gist options
  • Save RWOverdijk/29fcf1deb6f597f715ab to your computer and use it in GitHub Desktop.
Save RWOverdijk/29fcf1deb6f597f715ab to your computer and use it in GitHub Desktop.
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
var assert = require('chai').assert,
userId,
Thread;
describe('Thread', function () {
describe('performance', function () {
before(function (done) {
this.timeout(1200000);
Thread = sails.models.thread;
console.time('create indexes');
async.parallel([
function (callback) {
Thread.query('alter table thread add KEY `updatedAt` (`updatedAt`);', callback);
},
function (callback) {
Thread.query('alter table thread add key `to_toArchived` (`to`, `toArchived`);', callback);
},
function (callback) {
Thread.query('alter table thread add key `from_fromArchived` (`from`, `fromArchived`);', callback);
}
], function (error) {
var count = 0;
if (error) {
return done(error);
}
console.timeEnd('create indexes');
console.time('fill db');
async.times(200000, function (n, callback) {
Thread.create({
from: getRandomInt(1, 100),
to: getRandomInt(1, 100),
subject: 'test subject ' + n
}, function (error) {
if(error) {
return callback(error);
}
count++;
if(count % 1000 === 0) {
console.log(count + ' created');
}
callback();
});
}, function (error) {
if (error) {
return done(err);
}
console.timeEnd('fill db');
done();
});
});
});
beforeEach(function () {
userId = getRandomInt(1, 100);
});
it('should be fast', function (done) {
console.time('separate query');
async.timesSeries(10, function (n, callback) {
async.parallel({
to : function (callback) {
Thread.count({
to: userId,
toArchived: false
}, callback);
},
from : function (callback) {
Thread.count({
from: userId,
fromArchived: false
}, callback);
}
}, function (error, results) {
assert.isNull(error);
console.log(results.to + results.from);
console.timeEnd('separate query');
callback();
});
}, done);
});
it('should be even faster', function (done) {
console.time('one query');
async.timesSeries(10, function (n, callback) {
Thread.count({
or: [
{
to : userId,
toArchived: false
},
{
from : userId,
fromArchived: false
}
]
}, function (error, result) {
assert.isNull(error);
console.log(result);
console.timeEnd('one query');
callback();
});
}, done);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment