Skip to content

Commit

Permalink
benchmark: reject samples that had involuntary context switches
Browse files Browse the repository at this point in the history
If context switch happens during benchmark it affects measurements in a
significant way, so just reject affected measurments.
  • Loading branch information
IvanGoncharov committed May 25, 2022
1 parent 3075967 commit b42eceb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion benchmark/introspectionFromSchema-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const document = parse(getIntrospectionQuery());

export const benchmark = {
name: 'Execute Introspection Query',
count: 30,
count: 20,
measure() {
executeSync({ schema, document });
},
Expand Down
18 changes: 18 additions & 0 deletions resources/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,25 @@ function prepareBenchmarkProjects(
}

async function collectSamples(modulePath: string) {
let numOfConsequentlyRejectedSamples = 0;
const samples = [];

// If time permits, increase sample size to reduce the margin of error.
const start = Date.now();
while (samples.length < minSamples || (Date.now() - start) / 1e3 < maxTime) {
const sample = await sampleModule(modulePath);

if (sample.involuntaryContextSwitches > 0) {
numOfConsequentlyRejectedSamples++;
if (numOfConsequentlyRejectedSamples > 5) {
throw Error(
'Can not sample benchmark due to 5 consequent runs beings rejected because of context switching',
);
}
continue;
}
numOfConsequentlyRejectedSamples = 0;

assert(sample.clocked > 0);
assert(sample.memUsed > 0);
samples.push(sample);
Expand Down Expand Up @@ -356,6 +369,7 @@ interface BenchmarkSample {
name: string;
clocked: number;
memUsed: number;
involuntaryContextSwitches: number;
}

function sampleModule(modulePath: string): Promise<BenchmarkSample> {
Expand All @@ -377,16 +391,20 @@ function sampleModule(modulePath: string): Promise<BenchmarkSample> {
const memBaseline = process.memoryUsage().heapUsed;
const resourcesStart = process.resourceUsage();
const startTime = process.hrtime.bigint();
for (let i = 0; i < benchmark.count; ++i) {
benchmark.measure();
}
const timeDiff = Number(process.hrtime.bigint() - startTime);
const resourcesEnd = process.resourceUsage();
process.send({
name: benchmark.name,
clocked: timeDiff / benchmark.count,
memUsed: (process.memoryUsage().heapUsed - memBaseline) / benchmark.count,
involuntaryContextSwitches:
resourcesEnd.involuntaryContextSwitches - resourcesStart.involuntaryContextSwitches,
});
`;

Expand Down

0 comments on commit b42eceb

Please sign in to comment.