({ len, duration, concurrent, encoding })
| 25 | }); |
| 26 | |
| 27 | function main({ len, duration, concurrent, encoding }) { |
| 28 | try { |
| 29 | fs.unlinkSync(filename); |
| 30 | } catch { |
| 31 | // Continue regardless of error. |
| 32 | } |
| 33 | let data = Buffer.alloc(len, 'x'); |
| 34 | fs.writeFileSync(filename, data); |
| 35 | data = null; |
| 36 | |
| 37 | let reads = 0; |
| 38 | let waitConcurrent = 0; |
| 39 | |
| 40 | const startedAt = Date.now(); |
| 41 | const endAt = startedAt + (duration * 1000); |
| 42 | |
| 43 | bench.start(); |
| 44 | |
| 45 | function stop() { |
| 46 | bench.end(reads); |
| 47 | |
| 48 | try { |
| 49 | fs.unlinkSync(filename); |
| 50 | } catch { |
| 51 | // Continue regardless of error. |
| 52 | } |
| 53 | |
| 54 | process.exit(0); |
| 55 | } |
| 56 | |
| 57 | function read() { |
| 58 | fs.readFile(filename, encoding, afterRead); |
| 59 | } |
| 60 | |
| 61 | function afterRead(er, data) { |
| 62 | if (er) { |
| 63 | throw er; |
| 64 | } |
| 65 | |
| 66 | if (data.length !== len) |
| 67 | throw new Error('wrong number of bytes returned'); |
| 68 | |
| 69 | reads++; |
| 70 | const benchEnded = Date.now() >= endAt; |
| 71 | |
| 72 | if (benchEnded && (++waitConcurrent) === concurrent) { |
| 73 | stop(); |
| 74 | } else if (!benchEnded) { |
| 75 | read(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | for (let i = 0; i < concurrent; i++) read(); |
| 80 | } |
nothing calls this directly
no test coverage detected