@@ -14,7 +14,6 @@ const filename = path.resolve(__dirname,
1414 `.removeme-benchmark-garbage-${ process . pid } ` ) ;
1515const fs = require ( 'fs' ) ;
1616const zlib = require ( 'zlib' ) ;
17- const assert = require ( 'assert' ) ;
1817
1918const bench = common . createBenchmark ( main , {
2019 duration : [ 5 ] ,
@@ -35,41 +34,49 @@ function main({ len, duration, concurrent, encoding }) {
3534
3635 const zipData = Buffer . alloc ( 1024 , 'a' ) ;
3736
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+
3844 let reads = 0 ;
3945 let zips = 0 ;
40- let benchEnded = false ;
46+
4147 bench . start ( ) ;
42- setTimeout ( ( ) => {
48+
49+ function stop ( ) {
4350 const totalOps = reads + zips ;
44- benchEnded = true ;
4551 bench . end ( totalOps ) ;
52+
4653 try {
4754 fs . unlinkSync ( filename ) ;
4855 } catch {
4956 // Continue regardless of error.
5057 }
51- } , duration * 1000 ) ;
58+ }
5259
5360 function read ( ) {
5461 fs . readFile ( filename , encoding , afterRead ) ;
5562 }
5663
5764 function afterRead ( er , data ) {
5865 if ( er ) {
59- if ( er . code === 'ENOENT' ) {
60- // Only OK if unlinked by the timer from main.
61- assert . ok ( benchEnded ) ;
62- return ;
63- }
6466 throw er ;
6567 }
6668
6769 if ( data . length !== len )
6870 throw new Error ( 'wrong number of bytes returned' ) ;
6971
7072 reads ++ ;
71- if ( ! benchEnded )
73+ const benchEnded = Date . now ( ) >= endAt ;
74+
75+ if ( benchEnded && ( ++ waitConcurrent ) === targetConcurrency ) {
76+ stop ( ) ;
77+ } else if ( ! benchEnded ) {
7278 read ( ) ;
79+ }
7380 }
7481
7582 function zip ( ) {
@@ -81,12 +88,17 @@ function main({ len, duration, concurrent, encoding }) {
8188 throw er ;
8289
8390 zips ++ ;
84- if ( ! benchEnded )
91+ const benchEnded = Date . now ( ) >= endAt ;
92+
93+ if ( benchEnded && ( ++ waitConcurrent ) === targetConcurrency ) {
94+ stop ( ) ;
95+ } else if ( ! benchEnded ) {
8596 zip ( ) ;
97+ }
8698 }
8799
88100 // Start reads
89- while ( concurrent -- > 0 ) read ( ) ;
101+ for ( let i = 0 ; i < concurrent ; i ++ ) read ( ) ;
90102
91103 // Start a competing zip
92104 zip ( ) ;
0 commit comments