Skip to content

Commit 300a486

Browse files
committed
Add concurrency for bench
1 parent d334d37 commit 300a486

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

bench/index.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
'use strict'
22

33
const createBrowserless = require('..')
4+
5+
const { includes, reduce } = require('lodash')
6+
const processStats = require('process-stats')
47
const asciichart = require('asciichart')
58
const prettyMs = require('pretty-ms')
69
const prettyObj = require('fmt-obj')
710
const Measured = require('measured')
8-
const pSeries = require('p-series')
11+
const pAll = require('p-all')
912
const meow = require('meow')
1013

1114
const cli = meow(
@@ -19,6 +22,7 @@ const cli = meow(
1922
--iterations Number of iterations.
2023
--pool-min Mininum of instances in pool mode.
2124
--pool-max Maximum of instances in pool mode.
25+
--concurrency Define number of concurrent request.
2226
2327
Options
2428
The rest of parameters provided are passed as options.
@@ -33,6 +37,10 @@ const cli = meow(
3337
type: 'boolean',
3438
default: false
3539
},
40+
concurrency: {
41+
type: 'number',
42+
default: 1
43+
},
3644
iterations: {
3745
type: 'number',
3846
default: 10
@@ -41,7 +49,14 @@ const cli = meow(
4149
}
4250
)
4351

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+
}) => {
4560
const timer = new Measured.Timer()
4661
const promises = [...Array(iterations).keys()].map(n => {
4762
return async () => {
@@ -55,7 +70,7 @@ const benchmark = async ({ browserless, method, url, opts, iterations }) => {
5570
}
5671
})
5772

58-
const times = await pSeries(promises)
73+
const times = await pAll(promises, { concurrency })
5974
const histogram = timer.toJSON().histogram
6075
return { times, histogram }
6176
}
@@ -65,6 +80,7 @@ const benchmark = async ({ browserless, method, url, opts, iterations }) => {
6580

6681
const {
6782
method,
83+
concurrency,
6884
pool: isPool,
6985
poolMin,
7086
poolMax,
@@ -79,24 +95,32 @@ const benchmark = async ({ browserless, method, url, opts, iterations }) => {
7995
: createBrowserless(opts)
8096

8197
console.log(prettyObj(cli.flags))
98+
8299
const { times, histogram } = await benchmark({
100+
concurrency,
83101
browserless,
84102
iterations,
85103
method,
86104
url,
87105
opts
88106
})
89107

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+
)
94117

95118
const graph = asciichart.plot(times, { height: 6 })
119+
const { memUsed } = processStats.process()
96120

97121
console.log()
98122
console.log(graph)
99-
console.log(prettyObj(stats))
123+
console.log(prettyObj({ memUsed: memUsed.pretty, ...stats }))
100124

101125
process.exit()
102126
})()

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@
5656
"meow": "latest",
5757
"mocha": "latest",
5858
"nyc": "latest",
59-
"p-series": "latest",
59+
"p-all": "latest",
6060
"percentile": "latest",
6161
"prettier-standard": "latest",
6262
"pretty-ms": "latest",
63+
"process-stats": "latest",
6364
"puppeteer": "latest",
6465
"should": "latest",
6566
"standard": "latest",

src/pool/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const createPool = require('./create')
44

55
const POOL_OPTS = {
6-
max: 10,
6+
max: 15,
77
min: 2
88
}
99

0 commit comments

Comments
 (0)