({ dur, encodingType, size })
| 15 | }); |
| 16 | |
| 17 | function main({ dur, encodingType, size }) { |
| 18 | let encoding; |
| 19 | |
| 20 | let chunk; |
| 21 | switch (encodingType) { |
| 22 | case 'buf': |
| 23 | chunk = Buffer.alloc(size, 'b'); |
| 24 | break; |
| 25 | case 'asc': |
| 26 | chunk = 'a'.repeat(size); |
| 27 | encoding = 'ascii'; |
| 28 | break; |
| 29 | case 'utf': |
| 30 | chunk = 'ü'.repeat(Math.ceil(size / 2)); |
| 31 | encoding = 'utf8'; |
| 32 | break; |
| 33 | default: |
| 34 | throw new Error(`invalid encodingType: ${encodingType}`); |
| 35 | } |
| 36 | |
| 37 | try { |
| 38 | fs.unlinkSync(filename); |
| 39 | } catch { |
| 40 | // Continue regardless of error. |
| 41 | } |
| 42 | |
| 43 | let started = false; |
| 44 | let ended = false; |
| 45 | |
| 46 | const f = fs.createWriteStream(filename); |
| 47 | f.on('drain', write); |
| 48 | f.on('open', write); |
| 49 | f.on('close', done); |
| 50 | f.on('finish', () => { |
| 51 | ended = true; |
| 52 | const written = fs.statSync(filename).size / 1024; |
| 53 | try { |
| 54 | fs.unlinkSync(filename); |
| 55 | } catch { |
| 56 | // Continue regardless of error. |
| 57 | } |
| 58 | bench.end(written / 1024); |
| 59 | }); |
| 60 | |
| 61 | |
| 62 | function write() { |
| 63 | if (!started) { |
| 64 | started = true; |
| 65 | setTimeout(() => { |
| 66 | f.end(); |
| 67 | }, dur * 1000); |
| 68 | bench.start(); |
| 69 | } |
| 70 | |
| 71 | while (false !== f.write(chunk, encoding)); |
| 72 | } |
| 73 | |
| 74 | function done() { |
nothing calls this directly
no test coverage detected
searching dependent graphs…