-
Notifications
You must be signed in to change notification settings - Fork 65
/
benchmark.js
57 lines (51 loc) · 1.61 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* eslint-disable no-console */
import phantom from 'phantom'
import http from 'http'
import createPhantomPool from '../src'
const startServer = () => new Promise((resolve, reject) => {
const server = http.createServer((req, res) => {
res.end('test')
}).listen((err) => {
if (err) return reject(err)
resolve(server)
})
})
const pool = createPhantomPool()
/* eslint-disable no-unused-vars */
const noPool = async (url) => {
const instance = await phantom.create()
const page = await instance.createPage()
const status = await page.open(url, { operation: 'GET' })
if (status !== 'success') throw new Error(status)
const content = await page.property('content')
// console.log(content)
await instance.exit()
}
const withPool = (url) => pool.use(async (instance) => {
const page = await instance.createPage()
const status = await page.open(url, { operation: 'GET' })
if (status !== 'success') throw new Error(status)
const content = await page.property('content')
// console.log(content)
})
const benchmark = async (iters) => {
const server = await startServer()
const url = `http://localhost:${server.address().port}`
console.log('Starting benchmark without pool')
for (let i = 0; i < iters; i++) {
console.time(`noPool-${i}`)
await noPool(`${url}/${i}`)
console.timeEnd(`noPool-${i}`)
}
console.log('')
console.log('Starting benchmark with pool')
for (let i = 0; i < iters; i++) {
console.time(`pool-${i}`)
await withPool(`${url}/${i}`)
console.timeEnd(`pool-${i}`)
}
console.log('Done')
}
benchmark(10).then(() => {
process.exit(0)
}).catch(console.error)