Skip to content

Commit

Permalink
test: add scenario for optimized managed benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGSS committed Oct 15, 2024
1 parent fa57188 commit 74c4db1
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 27 deletions.
84 changes: 57 additions & 27 deletions test/env.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
const { describe, it, before } = require('node:test');
const assert = require('node:assert');
const copyBench = require('./fixtures/copy');
const { managedBench, managedOptBench } = require('./fixtures/opt-managed');

function assertMinBenchmarkDifference(results, { percentageLimit, ciPercentageLimit }) {
assertBenchmarkDifference(results, { percentageLimit, ciPercentageLimit, greaterThan: true });
}

function assertMaxBenchmarkDifference(results, { percentageLimit, ciPercentageLimit }) {
assertBenchmarkDifference(results, { percentageLimit, ciPercentageLimit, greaterThan: false });
}

function assertBenchmarkDifference(results, { percentageLimit, ciPercentageLimit, greaterThan }) {
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%
if (process.env.CI) {
// CI runs in a shared-env so the percentage of difference
// must be greather there due to high variance of hardware
assert.ok(
greaterThan ? percentageLimit >= ciPercentageLimit : percentageDifference <= ciPercentageLimit,
`"${results[i].name}" too different from "${results[j].name}" - ${percentageDifference} != ${ciPercentageLimit}`
);
} else {
assert.ok(
greaterThan ? percentageLimit >= percentageLimit : percentageDifference <= percentageLimit,
`${results[i].name} too different from ${results[j].name} - ${percentageDifference} != ${percentageLimit}`
);
}
}
}
}
}

describe('Same benchmark function', () => {
let results;
Expand All @@ -10,32 +49,23 @@ describe('Same benchmark function', () => {
});

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%
if (process.env.CI) {
// CI runs in a shared-env so the percentage of difference
// must be greather there due to high variance of hardware
assert.ok(
percentageDifference <= 30,
`${opsSec1} too different from ${opsSec2} - ${results[i].name}`
);
} else {
assert.ok(
percentageDifference <= 10,
`${opsSec1} too different from ${opsSec2} - ${results[i].name}`
);
}
}
}
}
assertMaxBenchmarkDifference(results, { percentageLimit: 10, ciPercentageLimit: 30 });
});
});

describe('Managed can be V8 optimized', () => {
let optResults, results;

before(async () => {
optResults = await managedOptBench.run();
results = await managedBench.run();
});

it('should be more than 50% different from unmanaged', () => {
assertMinBenchmarkDifference(optResults, { percentageLimit: 50, ciPercentageLimit: 30 });
});

// it('should be similar when avoiding V8 optimizatio', () => {
// assertBenchmarkDifference(results, 50, 30);
// });
});
45 changes: 45 additions & 0 deletions test/fixtures/opt-managed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { Suite } = require('../../lib');
const assert = require('node:assert');

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')
assert.ok(r)
})
.add('[Managed] Using includes', function (timer) {
timer.start()
for (let i = 0; i < timer.count; i++) {
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')
assert.ok(r)
}
timer.end(timer.count)
})

suite.run();

const optSuite = new Suite({ reporter: false });

optSuite
.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')
// assert.ok(r)
})
.add('[Managed] Using includes', function (timer) {
timer.start()
for (let i = 0; i < timer.count; i++) {
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')
// assert.ok(r)
}
timer.end(timer.count)
})

module.exports = {
managedBench: suite,
managedOptBench: optSuite,
};

0 comments on commit 74c4db1

Please sign in to comment.