@@ -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 ( ) ;
0 commit comments