Skip to content

Commit

Permalink
fixup! benchmark: fix race condition on fs benchs
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Oct 8, 2023
1 parent dad1702 commit 07d29e0
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 21 deletions.
10 changes: 5 additions & 5 deletions benchmark/fs/readfile-partitioned.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ function main({ len, duration, concurrent, encoding }) {
const zipData = Buffer.alloc(1024, 'a');

let waitConcurrent = 0;
// plus one because of zip
let targetConcurrency = concurrent + 1;

// Plus one because of zip
const targetConcurrency = concurrent + 1;
const startedAt = Date.now();
const endAt = startedAt + (duration * 1000);

let reads = 0;
let zips = 0;

bench.start();

function stop() {
const totalOps = reads + zips;
bench.end(totalOps);
Expand All @@ -70,7 +70,7 @@ function main({ len, duration, concurrent, encoding }) {
throw new Error('wrong number of bytes returned');

reads++;
let benchEnded = Date.now() >= endAt;
const benchEnded = Date.now() >= endAt;

if (benchEnded && (++waitConcurrent) === targetConcurrency) {
stop();
Expand All @@ -88,7 +88,7 @@ function main({ len, duration, concurrent, encoding }) {
throw er;

zips++;
let benchEnded = Date.now() >= endAt;
const benchEnded = Date.now() >= endAt;

if (benchEnded && (++waitConcurrent) === targetConcurrency) {
stop();
Expand Down
5 changes: 2 additions & 3 deletions benchmark/fs/readfile-permission-enabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

const common = require('../common.js');
const fs = require('fs');
const assert = require('assert');

const tmpdir = require('../../test/common/tmpdir');
tmpdir.refresh();
Expand Down Expand Up @@ -42,7 +41,7 @@ function main({ len, duration, concurrent, encoding }) {
const endAt = startedAt + (duration * 1000);

bench.start();

function stop() {
bench.end(reads);

Expand All @@ -68,7 +67,7 @@ function main({ len, duration, concurrent, encoding }) {
throw new Error('wrong number of bytes returned');

reads++;
let benchEnded = Date.now() >= endAt;
const benchEnded = Date.now() >= endAt;

if (benchEnded && (++waitConcurrent) === concurrent) {
stop();
Expand Down
3 changes: 1 addition & 2 deletions benchmark/fs/readfile-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

const common = require('../common.js');
const fs = require('fs');
const assert = require('assert');

const tmpdir = require('../../test/common/tmpdir');
tmpdir.refresh();
Expand Down Expand Up @@ -70,7 +69,7 @@ function main({ len, duration, concurrent, encoding }) {
throw new Error('wrong number of bytes returned');

reads++;
let benchEnded = Date.now() >= endAt;
const benchEnded = Date.now() >= endAt;

if (benchEnded && (++waitConcurrent) === concurrent) {
stop();
Expand Down
4 changes: 1 addition & 3 deletions benchmark/fs/readfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

const common = require('../common.js');
const fs = require('fs');
const assert = require('assert');
const { performance } = require('perf_hooks');

const tmpdir = require('../../test/common/tmpdir');
tmpdir.refresh();
Expand Down Expand Up @@ -62,7 +60,7 @@ function main({ len, duration, concurrent, encoding }) {
throw new Error('wrong number of bytes returned');

reads++;
let benchEnded = Date.now() >= endAt;
const benchEnded = Date.now() >= endAt;

if (benchEnded && (++waitConcurrent) === concurrent) {
stop();
Expand Down
10 changes: 2 additions & 8 deletions benchmark/fs/writefile-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

const common = require('../common.js');
const fs = require('fs');
const assert = require('assert');
const tmpdir = require('../../test/common/tmpdir');

tmpdir.refresh();
Expand Down Expand Up @@ -44,7 +43,7 @@ function main({ encodingType, duration, concurrent, size }) {
const endAt = startedAt + (duration * 1000);

bench.start();

function stop() {
bench.end(writes);

Expand All @@ -67,16 +66,11 @@ function main({ encodingType, duration, concurrent, size }) {

function afterWrite(er) {
if (er) {
if (er.code === 'ENOENT') {
// Only OK if unlinked by the timer from main.
assert.ok(benchEnded);
return;
}
throw er;
}

writes++;
let benchEnded = Date.now() >= endAt;
const benchEnded = Date.now() >= endAt;

if (benchEnded && (++waitConcurrent) === concurrent) {
stop();
Expand Down

0 comments on commit 07d29e0

Please sign in to comment.