1
1
'use strict'
2
2
3
3
const createBrowserless = require ( '..' )
4
+
5
+ const { includes, reduce } = require ( 'lodash' )
6
+ const processStats = require ( 'process-stats' )
4
7
const asciichart = require ( 'asciichart' )
5
8
const prettyMs = require ( 'pretty-ms' )
6
9
const prettyObj = require ( 'fmt-obj' )
7
10
const Measured = require ( 'measured' )
8
- const pSeries = require ( 'p-series ' )
11
+ const pAll = require ( 'p-all ' )
9
12
const meow = require ( 'meow' )
10
13
11
14
const cli = meow (
@@ -19,6 +22,7 @@ const cli = meow(
19
22
--iterations Number of iterations.
20
23
--pool-min Mininum of instances in pool mode.
21
24
--pool-max Maximum of instances in pool mode.
25
+ --concurrency Define number of concurrent request.
22
26
23
27
Options
24
28
The rest of parameters provided are passed as options.
@@ -33,6 +37,10 @@ const cli = meow(
33
37
type : 'boolean' ,
34
38
default : false
35
39
} ,
40
+ concurrency : {
41
+ type : 'number' ,
42
+ default : 1
43
+ } ,
36
44
iterations : {
37
45
type : 'number' ,
38
46
default : 10
@@ -41,7 +49,14 @@ const cli = meow(
41
49
}
42
50
)
43
51
44
- const benchmark = async ( { browserless, method, url, opts, iterations } ) => {
52
+ const benchmark = async ( {
53
+ browserless,
54
+ method,
55
+ url,
56
+ opts,
57
+ iterations,
58
+ concurrency
59
+ } ) => {
45
60
const timer = new Measured . Timer ( )
46
61
const promises = [ ...Array ( iterations ) . keys ( ) ] . map ( n => {
47
62
return async ( ) => {
@@ -55,7 +70,7 @@ const benchmark = async ({ browserless, method, url, opts, iterations }) => {
55
70
}
56
71
} )
57
72
58
- const times = await pSeries ( promises )
73
+ const times = await pAll ( promises , { concurrency } )
59
74
const histogram = timer . toJSON ( ) . histogram
60
75
return { times, histogram }
61
76
}
@@ -65,6 +80,7 @@ const benchmark = async ({ browserless, method, url, opts, iterations }) => {
65
80
66
81
const {
67
82
method,
83
+ concurrency,
68
84
pool : isPool ,
69
85
poolMin,
70
86
poolMax,
@@ -79,24 +95,32 @@ const benchmark = async ({ browserless, method, url, opts, iterations }) => {
79
95
: createBrowserless ( opts )
80
96
81
97
console . log ( prettyObj ( cli . flags ) )
98
+
82
99
const { times, histogram } = await benchmark ( {
100
+ concurrency,
83
101
browserless,
84
102
iterations,
85
103
method,
86
104
url,
87
105
opts
88
106
} )
89
107
90
- const stats = Object . keys ( histogram ) . reduce ( ( acc , key ) => {
91
- const value = histogram [ key ]
92
- return { ...acc , [ key ] : prettyMs ( value ) }
93
- } , { } )
108
+ const stats = reduce (
109
+ histogram ,
110
+ ( acc , value , key ) => {
111
+ const newValue = ! includes ( [ 'count' ] , key ) ? prettyMs ( value ) : value
112
+
113
+ return { ...acc , [ key ] : newValue }
114
+ } ,
115
+ { }
116
+ )
94
117
95
118
const graph = asciichart . plot ( times , { height : 6 } )
119
+ const { memUsed } = processStats . process ( )
96
120
97
121
console . log ( )
98
122
console . log ( graph )
99
- console . log ( prettyObj ( stats ) )
123
+ console . log ( prettyObj ( { memUsed : memUsed . pretty , ... stats } ) )
100
124
101
125
process . exit ( )
102
126
} ) ( )
0 commit comments