Last active
August 29, 2015 14:10
-
-
Save koszta/45d87663d2083e1a67e4 to your computer and use it in GitHub Desktop.
Sails model performance tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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