Skip to content

Commit

Permalink
Merge pull request #11 from AikidoSec/dev
Browse files Browse the repository at this point in the history
Add GitHub action to verify that difference is less than 8%
  • Loading branch information
hansott authored Feb 14, 2024
2 parents 790e8dc + 072ef76 commit c00f706
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 8 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Benchmark
on: push
jobs:
build:
runs-on: ubuntu-latest
services:
mongodb:
image: mongo:5
env:
"MONGO_INITDB_ROOT_USERNAME": "root"
"MONGO_INITDB_ROOT_PASSWORD": "password"
ports:
- 27017:27017
timeout-minutes: 5
strategy:
matrix:
node-version: [18.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: make install
- run: make build
- name: Run Benchmark
id: benchmark
run: cd benchmarks/mongodb && node benchmark.js
56 changes: 56 additions & 0 deletions benchmarks/mongodb/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { exec } = require("child_process");

async function runScript(scriptPath) {
return new Promise((resolve, reject) => {
exec(`node ${scriptPath}`, (error, stdout, stderr) => {
if (error) {
reject(error);
}
if (stderr) {
reject(stderr);
}
resolve(stdout);
});
});
}

async function main(times = 10, maxDiffInPercentage = 10) {
const results = [];
for (let i = 0; i < times; i++) {
const withGuard = await runScript("withGuard.js");
const { averageTimeInMS: withGuardTimeInMS } = JSON.parse(withGuard);
const withoutGuard = await runScript("withoutGuard.js");
const { averageTimeInMS: withoutGuardTimeInMS } = JSON.parse(withoutGuard);
results.push({
withGuardTimeInMS,
withoutGuardTimeInMS,
differenceInPercentage: Math.abs(
((withGuardTimeInMS - withoutGuardTimeInMS) / withoutGuardTimeInMS) *
100
),
});
}

const averageDifferenceInPercentage =
results
.map((r) => r.differenceInPercentage)
.reduce((acc, diff) => acc + diff, 0) / results.length;

if (averageDifferenceInPercentage > maxDiffInPercentage) {
console.error(
`The difference between the two benchmarks is too high: ${averageDifferenceInPercentage.toFixed(
3
)}%`
);
console.error(results);
process.exit(1);
} else {
console.log(
`The difference between the two benchmarks is acceptable: ${averageDifferenceInPercentage.toFixed(
3
)}%`
);
}
}

main();
5 changes: 1 addition & 4 deletions benchmarks/mongodb/measure.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,5 @@ module.exports = async function measureFunctionPerformance(
totalExecutionTime += end - start;
}

const averageTime = totalExecutionTime / measuredIterations;
console.log(
`Average execution time over ${measuredIterations} iterations: ${averageTime.toFixed(2)} milliseconds`
);
return totalExecutionTime / measuredIterations;
};
5 changes: 3 additions & 2 deletions benchmarks/mongodb/withGuard.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const { runWithContext } = require("@aikidosec/guard");

async function main() {
const client = await getClient();

await measure(async () => {
const averageTimeInMS = await measure(async () => {
await runWithContext(
{ body: { email: "email", password: "password" } },
async () => {
Expand All @@ -21,6 +20,8 @@ async function main() {
});

await client.close();

console.log(JSON.stringify({ averageTimeInMS }));
}

main();
5 changes: 3 additions & 2 deletions benchmarks/mongodb/withoutGuard.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ const getClient = require("./getClient");

async function main() {
const client = await getClient();

await measure(async () => {
const averageTimeInMS = await measure(async () => {
await getUser(client, {
email: "email",
password: "password",
});
});

await client.close();

console.log(JSON.stringify({ averageTimeInMS }));
}

main();

0 comments on commit c00f706

Please sign in to comment.