Skip to content

Commit

Permalink
perf test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Jul 8, 2016
1 parent 1b5a221 commit 35ccbfc
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ examples/*/*.db
*.iml
*.DS_Store
lib
profiling
122 changes: 122 additions & 0 deletions perf/mockMocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
var _ = require('lodash');
var Promise = require('bluebird');

module.exports = function () {
var rootScope = new Scope('root');
var currentScope = rootScope;
var onlyTest = null;

function Scope(name) {
this.name = name;
this.childScopes = {};

this.tests = {};
this.beforeEach = [];
this.before = [];
this.afterEach = [];
this.after = [];

var self = this;
this.run = function () {
var promise = Promise.resolve();

_.each(self.before, function (before) {
promise = promise.then(function () {
return before();
});
});

if (onlyTest == null || _.includes(self.tests, onlyTest)) {
console.log('scope:', self.name);

_.each(self.tests, function (test, testName) {
if (onlyTest !== null && test !== onlyTest) {
return;
}

promise = promise.then(function () {
var innerPromise = Promise.resolve();

_.each(self.beforeEach, function (beforeEach) {
innerPromise = innerPromise.then(function () {
return beforeEach();
});
});

innerPromise = innerPromise.then(function () {
console.log('test:', testName);
return test();
});

_.each(self.afterEach, function (afterEach) {
innerPromise = innerPromise.then(function () {
return afterEach();
});
});

return innerPromise;
});
});
}

_.each(this.childScopes, function (childScope) {
promise = promise.then(function () {
return childScope.run();
});
});

_.each(self.after, function (after) {
promise = promise.then(function () {
return after();
});
});

return promise;
}
}

function it(testName, func) {
currentScope.tests[testName] = func;
}

it.only = function (testName, func) {
currentScope.tests[testName] = func;
onlyTest = func;
};

return {
describe: function describe(description, func) {
var scope = new Scope(description);
var oldCurrentScope = currentScope;

currentScope.childScopes[description] = scope;
currentScope = scope;

func();

currentScope = oldCurrentScope;
},

before: function before(func) {
currentScope.before.push(func);
},

beforeEach: function beforeEach(func) {
currentScope.beforeEach.push(func);
},

after: function after(func) {
currentScope.after.push(func);
},

afterEach: function afterEach(func) {
currentScope.afterEach.push(func);
},

it: it,

run: function () {
return rootScope.run();
}
};
};
29 changes: 21 additions & 8 deletions perf/perfTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@ var _ = require('lodash')
, expect = require('expect.js')
, Promise = require('bluebird')
, mockKnexBuilder = require('../testUtils/mockKnex')
, mockMochaFactory = require('./mockMocha')
, Model = require('../').Model;

if (typeof describe == 'undefined') {
global.mockMocha = mockMochaFactory();
global.describe = global.mockMocha.describe;
global.before = global.mockMocha.before;
global.beforeEach = global.mockMocha.beforeEach;
global.after = global.mockMocha.after;
global.afterEach = global.mockMocha.afterEach;
global.it = global.mockMocha.it;
}

describe('Performance tests', function () {
var mockKnex = null;
var knex = null;
Expand Down Expand Up @@ -216,8 +227,8 @@ describe('Performance tests', function () {
});

perfTest({
name: '32000 traverse calls for the dataset',
runCount: 32000,
name: '16000 traverse calls for the dataset',
runCount: 16000,
runtimeGoal: 1000,
beforeTest: function () {
return _.map(data, function (person) {
Expand Down Expand Up @@ -457,7 +468,7 @@ describe('Performance tests', function () {
});

function perfTest(opt) {
it(opt.name + ' [goal ' + opt.runtimeGoal + ' ms]', function () {
(opt.only ? it.only : it)(opt.name + ' [goal ' + opt.runtimeGoal + ' ms]', function () {
var beforeTest = opt.beforeTest || _.noop;

var ctx = beforeTest();
Expand All @@ -474,11 +485,7 @@ describe('Performance tests', function () {
throw new Error('runtime ' + runtime + ' ms exceeds the runtimeGoal ' + opt.runtimeGoal + " ms");
}

Promise.delay(100).then(function () {
console.log(' runtime: ' + runtime + ' ms, ' + (runtime / opt.runCount).toFixed(3) + ' ms / run');
});

return Promise.delay(50);
console.log(' runtime: ' + runtime + ' ms, ' + (runtime / opt.runCount).toFixed(3) + ' ms / run');
});
});
}
Expand All @@ -497,3 +504,9 @@ describe('Performance tests', function () {
return Promise.all(promises);
}
});

if (global.mockMocha) {
global.mockMocha.run();
}


0 comments on commit 35ccbfc

Please sign in to comment.