-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #269 from lo1tuma/runtime-bench
Add benchmarks for runtime with many files
- Loading branch information
Showing
5 changed files
with
148 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
const os = require('os'); | ||
const { performance } = require('perf_hooks'); | ||
const times = require('ramda/src/times'); | ||
const median = require('ramda/src/median'); | ||
const map = require('ramda/src/map'); | ||
const prop = require('ramda/src/prop'); | ||
|
||
const [ { speed: cpuSpeed } ] = os.cpus(); | ||
|
||
function clearRequireCache() { | ||
Object.keys(require.cache).forEach(function (key) { | ||
delete require.cache[key]; | ||
}); | ||
} | ||
|
||
function runBenchmark(fn, count) { | ||
const results = []; | ||
|
||
times(() => { | ||
const startTime = performance.now(); | ||
const startMemory = process.memoryUsage().rss; | ||
fn(); | ||
const endTime = performance.now(); | ||
const endMemory = process.memoryUsage().rss; | ||
const duration = endTime - startTime; | ||
const memory = endMemory - startMemory; | ||
|
||
results.push({ duration, memory }); | ||
}, count); | ||
|
||
const medianDuration = median(map(prop('duration'), results)); | ||
const medianMemory = median(map(prop('memory'), results)); | ||
|
||
return { medianDuration, medianMemory }; | ||
} | ||
|
||
module.exports = { runBenchmark, clearRequireCache, cpuSpeed }; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
'use strict'; | ||
|
||
const { expect } = require('chai'); | ||
const { Linter } = require('eslint'); | ||
const times = require('ramda/src/times'); | ||
const toPairs = require('ramda/src/toPairs'); | ||
const fromPairs = require('ramda/src/fromPairs'); | ||
const { runBenchmark, cpuSpeed } = require('./measure'); | ||
const mochaPlugin = require('../'); | ||
|
||
const recommendedRules = mochaPlugin.configs.recommended.rules; | ||
|
||
function lintManyFilesWithAllRecommendedRules({ numberOfFiles }) { | ||
const linter = new Linter(); | ||
|
||
linter.defineRules(mochaPlugin.rules); | ||
|
||
const config = { | ||
rules: fromPairs(toPairs(recommendedRules).map(([ ruleName, ruleSettings ]) => { | ||
const [ , ruleNameWithoutPrefix ] = ruleName.split('/'); | ||
|
||
return [ ruleNameWithoutPrefix, ruleSettings ]; | ||
})), | ||
parserOptions: { | ||
ecmaVersion: 2018 | ||
} | ||
}; | ||
|
||
times((index) => { | ||
const codeToLint = ` | ||
'use strict'; | ||
const assert = require('assert'); | ||
const sinon = require('sinon'); | ||
const sut = require('./sut'); | ||
describe('SUT ${index}', function () { | ||
let fooStub; | ||
before(() => { | ||
fooStub = sinon.stub(sut, 'foo'); | ||
}); | ||
after(() => { | ||
fooStub.restore(); | ||
}); | ||
beforeEach(function (done) { | ||
done(); | ||
}); | ||
afterEach(() => { | ||
fooStub.reset(); | ||
}); | ||
it('should work', async function () { | ||
const bar = {}; | ||
await sut(bar); | ||
assert(fooStub.callCount === 42); | ||
}); | ||
describe('nested suite', async () => { | ||
beforeEach(async function () { | ||
await sleep(200); | ||
}); | ||
xit('doesn’t work yet', function () { | ||
sut(); | ||
}); | ||
}); | ||
context('more context', function () { | ||
it.only('only here it works', () => { | ||
sut(); | ||
assert(true); | ||
}); | ||
}); | ||
}); | ||
`; | ||
|
||
linter.verify(codeToLint, config); | ||
}, numberOfFiles); | ||
} | ||
|
||
describe('runtime', () => { | ||
it('should not take longer as the defined budget to lint many files with the recommended config', () => { | ||
const budget = 80000000 / cpuSpeed; | ||
|
||
const { medianDuration } = runBenchmark(() => { | ||
lintManyFilesWithAllRecommendedRules({ numberOfFiles: 350 }); | ||
}, 5); | ||
|
||
expect(medianDuration).to.be.below(budget); | ||
}); | ||
|
||
it('should not consume more memory as the defined budget to lint many files with the recommended config', () => { | ||
const budget = 3000000; | ||
|
||
const { medianMemory } = runBenchmark(() => { | ||
lintManyFilesWithAllRecommendedRules({ numberOfFiles: 350 }); | ||
}, 5); | ||
|
||
expect(medianMemory).to.be.below(budget); | ||
}); | ||
}); |
This file contains 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
This file contains 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