| 18 | }); |
| 19 | |
| 20 | function main({ encodingType, duration, concurrent, size }) { |
| 21 | let encoding; |
| 22 | let chunk; |
| 23 | switch (encodingType) { |
| 24 | case 'buf': |
| 25 | chunk = Buffer.alloc(size, 'b'); |
| 26 | break; |
| 27 | case 'asc': |
| 28 | chunk = 'a'.repeat(size); |
| 29 | encoding = 'ascii'; |
| 30 | break; |
| 31 | case 'utf': |
| 32 | chunk = 'ü'.repeat(Math.ceil(size / 2)); |
| 33 | encoding = 'utf8'; |
| 34 | break; |
| 35 | default: |
| 36 | throw new Error(`invalid encodingType: ${encodingType}`); |
| 37 | } |
| 38 | |
| 39 | let writes = 0; |
| 40 | let waitConcurrent = 0; |
| 41 | |
| 42 | let startedAt = Date.now(); |
| 43 | let endAt = startedAt + duration * 1000; |
| 44 | |
| 45 | // fs warmup |
| 46 | for (let i = 0; i < concurrent; i++) write(); |
| 47 | |
| 48 | writes = 0; |
| 49 | waitConcurrent = 0; |
| 50 | |
| 51 | startedAt = Date.now(); |
| 52 | endAt = startedAt + duration * 1000; |
| 53 | |
| 54 | bench.start(); |
| 55 | |
| 56 | function stop() { |
| 57 | bench.end(writes); |
| 58 | |
| 59 | for (let i = 0; i < filesWritten; i++) { |
| 60 | try { |
| 61 | fs.unlinkSync(`${filename}-${i}`); |
| 62 | } catch { |
| 63 | // Continue regardless of error. |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | process.exit(0); |
| 68 | } |
| 69 | |
| 70 | function write() { |
| 71 | fs.promises |
| 72 | .writeFile(`${filename}-${filesWritten++}`, chunk, encoding) |
| 73 | .then(() => afterWrite()) |
| 74 | .catch((err) => afterWrite(err)); |
| 75 | } |
| 76 | |
| 77 | function afterWrite(er) { |