GeezerVison is showing github.com — benchmark: fix race condition on fs benchs · nodejs/node@73757a5
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: benchmark/fs/readfile-partitioned.js +25-13Lines changed: 25 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,7 +14,6 @@ const filename = path.resolve(__dirname, | 14 | 14 | `.removeme-benchmark-garbage-${process.pid}`); | 15 | 15 | const fs = require('fs'); | 16 | 16 | const zlib = require('zlib'); | 17 | | -const assert = require('assert'); | 18 | 17 |
| 19 | 18 | const bench = common.createBenchmark(main, { | 20 | 19 | duration: [5], | @@ -35,41 +34,49 @@ function main({ len, duration, concurrent, encoding }) { | 35 | 34 |
| 36 | 35 | const zipData = Buffer.alloc(1024, 'a'); | 37 | 36 |
| | 37 | + let waitConcurrent = 0; | | 38 | + | | 39 | + // Plus one because of zip | | 40 | + const targetConcurrency = concurrent + 1; | | 41 | + const startedAt = Date.now(); | | 42 | + const endAt = startedAt + (duration * 1000); | | 43 | + | 38 | 44 | let reads = 0; | 39 | 45 | let zips = 0; | 40 | | - let benchEnded = false; | | 46 | + | 41 | 47 | bench.start(); | 42 | | - setTimeout(() => { | | 48 | + | | 49 | + function stop() { | 43 | 50 | const totalOps = reads + zips; | 44 | | - benchEnded = true; | 45 | 51 | bench.end(totalOps); | | 52 | + | 46 | 53 | try { | 47 | 54 | fs.unlinkSync(filename); | 48 | 55 | } catch { | 49 | 56 | // Continue regardless of error. | 50 | 57 | } | 51 | | - }, duration * 1000); | | 58 | + } | 52 | 59 |
| 53 | 60 | function read() { | 54 | 61 | fs.readFile(filename, encoding, afterRead); | 55 | 62 | } | 56 | 63 |
| 57 | 64 | function afterRead(er, data) { | 58 | 65 | if (er) { | 59 | | - if (er.code === 'ENOENT') { | 60 | | - // Only OK if unlinked by the timer from main. | 61 | | - assert.ok(benchEnded); | 62 | | - return; | 63 | | - } | 64 | 66 | throw er; | 65 | 67 | } | 66 | 68 |
| 67 | 69 | if (data.length !== len) | 68 | 70 | throw new Error('wrong number of bytes returned'); | 69 | 71 |
| 70 | 72 | reads++; | 71 | | - if (!benchEnded) | | 73 | + const benchEnded = Date.now() >= endAt; | | 74 | + | | 75 | + if (benchEnded && (++waitConcurrent) === targetConcurrency) { | | 76 | + stop(); | | 77 | + } else if (!benchEnded) { | 72 | 78 | read(); | | 79 | + } | 73 | 80 | } | 74 | 81 |
| 75 | 82 | function zip() { | @@ -81,12 +88,17 @@ function main({ len, duration, concurrent, encoding }) { | 81 | 88 | throw er; | 82 | 89 |
| 83 | 90 | zips++; | 84 | | - if (!benchEnded) | | 91 | + const benchEnded = Date.now() >= endAt; | | 92 | + | | 93 | + if (benchEnded && (++waitConcurrent) === targetConcurrency) { | | 94 | + stop(); | | 95 | + } else if (!benchEnded) { | 85 | 96 | zip(); | | 97 | + } | 86 | 98 | } | 87 | 99 |
| 88 | 100 | // Start reads | 89 | | - while (concurrent-- > 0) read(); | | 101 | + for (let i = 0; i < concurrent; i++) read(); | 90 | 102 |
| 91 | 103 | // Start a competing zip | 92 | 104 | zip(); | |
Collapse file Copy file name to clipboardExpand all lines: benchmark/fs/readfile-permission-enabled.js +17-12Lines changed: 17 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 |
| 6 | 6 | const common = require('../common.js'); | 7 | 7 | const fs = require('fs'); | 8 | | -const assert = require('assert'); | 9 | 8 |
| 10 | 9 | const tmpdir = require('../../test/common/tmpdir'); | 11 | 10 | tmpdir.refresh(); | @@ -36,40 +35,46 @@ function main({ len, duration, concurrent, encoding }) { | 36 | 35 | data = null; | 37 | 36 |
| 38 | 37 | let reads = 0; | 39 | | - let benchEnded = false; | | 38 | + let waitConcurrent = 0; | | 39 | + | | 40 | + const startedAt = Date.now(); | | 41 | + const endAt = startedAt + (duration * 1000); | | 42 | + | 40 | 43 | bench.start(); | 41 | | - setTimeout(() => { | 42 | | - benchEnded = true; | | 44 | + | | 45 | + function stop() { | 43 | 46 | bench.end(reads); | | 47 | + | 44 | 48 | try { | 45 | 49 | fs.unlinkSync(filename); | 46 | 50 | } catch { | 47 | 51 | // Continue regardless of error. | 48 | 52 | } | | 53 | + | 49 | 54 | process.exit(0); | 50 | | - }, duration * 1000); | | 55 | + } | 51 | 56 |
| 52 | 57 | function read() { | 53 | 58 | fs.readFile(filename, encoding, afterRead); | 54 | 59 | } | 55 | 60 |
| 56 | 61 | function afterRead(er, data) { | 57 | 62 | if (er) { | 58 | | - if (er.code === 'ENOENT') { | 59 | | - // Only OK if unlinked by the timer from main. | 60 | | - assert.ok(benchEnded); | 61 | | - return; | 62 | | - } | 63 | 63 | throw er; | 64 | 64 | } | 65 | 65 |
| 66 | 66 | if (data.length !== len) | 67 | 67 | throw new Error('wrong number of bytes returned'); | 68 | 68 |
| 69 | 69 | reads++; | 70 | | - if (!benchEnded) | | 70 | + const benchEnded = Date.now() >= endAt; | | 71 | + | | 72 | + if (benchEnded && (++waitConcurrent) === concurrent) { | | 73 | + stop(); | | 74 | + } else if (!benchEnded) { | 71 | 75 | read(); | | 76 | + } | 72 | 77 | } | 73 | 78 |
| 74 | | - while (concurrent--) read(); | | 79 | + for (let i = 0; i < concurrent; i++) read(); | 75 | 80 | } |
Collapse file Copy file name to clipboardExpand all lines: benchmark/fs/readfile-promises.js +20-15Lines changed: 20 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 |
| 6 | 6 | const common = require('../common.js'); | 7 | 7 | const fs = require('fs'); | 8 | | -const assert = require('assert'); | 9 | 8 |
| 10 | 9 | const tmpdir = require('../../test/common/tmpdir'); | 11 | 10 | tmpdir.refresh(); | @@ -35,19 +34,25 @@ function main({ len, duration, concurrent, encoding }) { | 35 | 34 | fs.writeFileSync(filename, data); | 36 | 35 | data = null; | 37 | 36 |
| 38 | | - let writes = 0; | 39 | | - let benchEnded = false; | | 37 | + let reads = 0; | | 38 | + let waitConcurrent = 0; | | 39 | + | | 40 | + const startedAt = Date.now(); | | 41 | + const endAt = startedAt + (duration * 1000); | | 42 | + | 40 | 43 | bench.start(); | 41 | | - setTimeout(() => { | 42 | | - benchEnded = true; | 43 | | - bench.end(writes); | | 44 | + | | 45 | + function stop() { | | 46 | + bench.end(reads); | | 47 | + | 44 | 48 | try { | 45 | 49 | fs.unlinkSync(filename); | 46 | 50 | } catch { | 47 | 51 | // Continue regardless of error. | 48 | 52 | } | | 53 | + | 49 | 54 | process.exit(0); | 50 | | - }, duration * 1000); | | 55 | + } | 51 | 56 |
| 52 | 57 | function read() { | 53 | 58 | fs.promises.readFile(filename, encoding) | @@ -57,21 +62,21 @@ function main({ len, duration, concurrent, encoding }) { | 57 | 62 |
| 58 | 63 | function afterRead(er, data) { | 59 | 64 | if (er) { | 60 | | - if (er.code === 'ENOENT') { | 61 | | - // Only OK if unlinked by the timer from main. | 62 | | - assert.ok(benchEnded); | 63 | | - return; | 64 | | - } | 65 | 65 | throw er; | 66 | 66 | } | 67 | 67 |
| 68 | 68 | if (data.length !== len) | 69 | 69 | throw new Error('wrong number of bytes returned'); | 70 | 70 |
| 71 | | - writes++; | 72 | | - if (!benchEnded) | | 71 | + reads++; | | 72 | + const benchEnded = Date.now() >= endAt; | | 73 | + | | 74 | + if (benchEnded && (++waitConcurrent) === concurrent) { | | 75 | + stop(); | | 76 | + } else if (!benchEnded) { | 73 | 77 | read(); | | 78 | + } | 74 | 79 | } | 75 | 80 |
| 76 | | - while (concurrent--) read(); | | 81 | + for (let i = 0; i < concurrent; i++) read(); | 77 | 82 | } |
Collapse file Copy file name to clipboardExpand all lines: benchmark/fs/readfile.js +20-15Lines changed: 20 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 |
| 6 | 6 | const common = require('../common.js'); | 7 | 7 | const fs = require('fs'); | 8 | | -const assert = require('assert'); | 9 | 8 |
| 10 | 9 | const tmpdir = require('../../test/common/tmpdir'); | 11 | 10 | tmpdir.refresh(); | @@ -29,40 +28,46 @@ function main({ len, duration, concurrent, encoding }) { | 29 | 28 | data = null; | 30 | 29 |
| 31 | 30 | let reads = 0; | 32 | | - let benchEnded = false; | | 31 | + let waitConcurrent = 0; | | 32 | + | | 33 | + const startedAt = Date.now(); | | 34 | + const endAt = startedAt + (duration * 1000); | | 35 | + | 33 | 36 | bench.start(); | 34 | | - setTimeout(() => { | 35 | | - benchEnded = true; | | 37 | + | | 38 | + function read() { | | 39 | + fs.readFile(filename, encoding, afterRead); | | 40 | + } | | 41 | + | | 42 | + function stop() { | 36 | 43 | bench.end(reads); | | 44 | + | 37 | 45 | try { | 38 | 46 | fs.unlinkSync(filename); | 39 | 47 | } catch { | 40 | 48 | // Continue regardless of error. | 41 | 49 | } | 42 | | - process.exit(0); | 43 | | - }, duration * 1000); | 44 | 50 |
| 45 | | - function read() { | 46 | | - fs.readFile(filename, encoding, afterRead); | | 51 | + process.exit(0); | 47 | 52 | } | 48 | 53 |
| 49 | 54 | function afterRead(er, data) { | 50 | 55 | if (er) { | 51 | | - if (er.code === 'ENOENT') { | 52 | | - // Only OK if unlinked by the timer from main. | 53 | | - assert.ok(benchEnded); | 54 | | - return; | 55 | | - } | 56 | 56 | throw er; | 57 | 57 | } | 58 | 58 |
| 59 | 59 | if (data.length !== len) | 60 | 60 | throw new Error('wrong number of bytes returned'); | 61 | 61 |
| 62 | 62 | reads++; | 63 | | - if (!benchEnded) | | 63 | + const benchEnded = Date.now() >= endAt; | | 64 | + | | 65 | + if (benchEnded && (++waitConcurrent) === concurrent) { | | 66 | + stop(); | | 67 | + } else if (!benchEnded) { | 64 | 68 | read(); | | 69 | + } | 65 | 70 | } | 66 | 71 |
| 67 | | - while (concurrent--) read(); | | 72 | + for (let i = 0; i < concurrent; i++) read(); | 68 | 73 | } |
Collapse file Copy file name to clipboardExpand all lines: benchmark/fs/writefile-promises.js +17-12Lines changed: 17 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 |
| 6 | 6 | const common = require('../common.js'); | 7 | 7 | const fs = require('fs'); | 8 | | -const assert = require('assert'); | 9 | 8 | const tmpdir = require('../../test/common/tmpdir'); | 10 | 9 |
| 11 | 10 | tmpdir.refresh(); | @@ -38,20 +37,26 @@ function main({ encodingType, duration, concurrent, size }) { | 38 | 37 | } | 39 | 38 |
| 40 | 39 | let writes = 0; | 41 | | - let benchEnded = false; | | 40 | + let waitConcurrent = 0; | | 41 | + | | 42 | + const startedAt = Date.now(); | | 43 | + const endAt = startedAt + (duration * 1000); | | 44 | + | 42 | 45 | bench.start(); | 43 | | - setTimeout(() => { | 44 | | - benchEnded = true; | | 46 | + | | 47 | + function stop() { | 45 | 48 | bench.end(writes); | | 49 | + | 46 | 50 | for (let i = 0; i < filesWritten; i++) { | 47 | 51 | try { | 48 | 52 | fs.unlinkSync(`${filename}-${i}`); | 49 | 53 | } catch { | 50 | 54 | // Continue regardless of error. | 51 | 55 | } | 52 | 56 | } | | 57 | + | 53 | 58 | process.exit(0); | 54 | | - }, duration * 1000); | | 59 | + } | 55 | 60 |
| 56 | 61 | function write() { | 57 | 62 | fs.promises.writeFile(`${filename}-${filesWritten++}`, chunk, encoding) | @@ -61,18 +66,18 @@ function main({ encodingType, duration, concurrent, size }) { | 61 | 66 |
| 62 | 67 | function afterWrite(er) { | 63 | 68 | if (er) { | 64 | | - if (er.code === 'ENOENT') { | 65 | | - // Only OK if unlinked by the timer from main. | 66 | | - assert.ok(benchEnded); | 67 | | - return; | 68 | | - } | 69 | 69 | throw er; | 70 | 70 | } | 71 | 71 |
| 72 | 72 | writes++; | 73 | | - if (!benchEnded) | | 73 | + const benchEnded = Date.now() >= endAt; | | 74 | + | | 75 | + if (benchEnded && (++waitConcurrent) === concurrent) { | | 76 | + stop(); | | 77 | + } else if (!benchEnded) { | 74 | 78 | write(); | | 79 | + } | 75 | 80 | } | 76 | 81 |
| 77 | | - while (concurrent--) write(); | | 82 | + for (let i = 0; i < concurrent; i++) write(); | 78 | 83 | } |
|