diff --git a/test/env.js b/test/env.js new file mode 100644 index 0000000..ac0d248 --- /dev/null +++ b/test/env.js @@ -0,0 +1,32 @@ +const { describe, it, before } = require('node:test'); +const assert = require('node:assert'); +const copyBench = require('./fixtures/copy'); + +describe('Same benchmark function', () => { + let results; + + before(async () => { + results = await copyBench.run(); + }); + + it('must have a similar benchmark result', () => { + for (let i = 0; i < results.length; i++) { + for (let j = 0; j < results.length; j++) { + if (i !== j) { + const opsSec1 = results[i].opsSec; + const opsSec2 = results[j].opsSec; + + // Calculate the percentage difference + const difference = Math.abs(opsSec1 - opsSec2); + const percentageDifference = (difference / Math.min(opsSec1, opsSec2)) * 100; + + // Check if the percentage difference is less than or equal to 10% + assert.ok( + percentageDifference <= 10, + `${opsSec1} too different from ${opsSec2} - ${results[i].name}` + ); + } + } + } + }); +}); diff --git a/test/fixtures/copy.js b/test/fixtures/copy.js index 869b75e..91ee516 100644 --- a/test/fixtures/copy.js +++ b/test/fixtures/copy.js @@ -1,16 +1,19 @@ const { Suite } = require('../../lib'); -const suite = new Suite(); +const suite = new Suite({ reporter: false }); suite .add('Using includes', function () { const text = 'text/html,application/xhtml+xml,application/xml;application/json;q=0.9,image/avif,image/webp,*/*;q=0.8' const r = text.includes('application/json') - return r; }) .add('Using includes 2', function () { const text = 'text/html,application/xhtml+xml,application/xml;application/json;q=0.9,image/avif,image/webp,*/*;q=0.8' const r = text.includes('application/json') - return r; }) - .run({ reporter: true }); + .add('Using includes 3', function () { + const text = 'text/html,application/xhtml+xml,application/xml;application/json;q=0.9,image/avif,image/webp,*/*;q=0.8' + const r = text.includes('application/json') + }) + +module.exports = suite;