1 |
|
2 |
|
3 | const pickup = require('../')
|
4 | const fs = require('fs')
|
5 | const path = require('path')
|
6 |
|
7 | module.exports.run = run
|
8 |
|
9 | const dir = path.dirname(module.filename)
|
10 | const file = path.join(dir, 'df.xml')
|
11 |
|
12 | function _run (n, cb, times) {
|
13 | const time = process.hrtime()
|
14 | const reader = fs.createReadStream(file)
|
15 | const parser = pickup({ objectMode: true })
|
16 | parser.on('finish', () => {
|
17 | if (n--) {
|
18 | const ns = process.hrtime(time)
|
19 | const ms = (ns[0] * 1e9 + ns[1]) / 1e6
|
20 | times.push(ms)
|
21 | _run(n, cb, times)
|
22 | } else {
|
23 | const r = times.reduce((a, b) => {
|
24 | return a + b
|
25 | })
|
26 | const avg = r / times.length
|
27 | const sorted = times.sort((a, b) => {
|
28 | return a - b
|
29 | })
|
30 | const l = sorted.length
|
31 | const med = sorted[Math.round(l / 2)]
|
32 | const min = sorted[0]
|
33 | const max = sorted[l - 1]
|
34 | cb(null, { med: med, min: min, max: max, avg: avg })
|
35 | }
|
36 | })
|
37 | let ok = true
|
38 | function write () {
|
39 | if (!ok) return
|
40 | let chunk
|
41 | while ((chunk = reader.read()) !== null) {
|
42 | ok = parser.write(chunk)
|
43 | }
|
44 | if (!ok) {
|
45 | parser.once('drain', () => {
|
46 | ok = true
|
47 | write()
|
48 | })
|
49 | }
|
50 | }
|
51 | reader.on('readable', write)
|
52 | reader.once('end', () => {
|
53 | reader.removeListener('readable', write)
|
54 | parser.end()
|
55 | })
|
56 | }
|
57 |
|
58 | function run (n, cb) {
|
59 | _run(n, cb, [])
|
60 | }
|
61 |
|
62 | if (require.main === module) {
|
63 | console.log('One moment, please.')
|
64 | run(100, (er, z) => {
|
65 | Object.getOwnPropertyNames(z).forEach((name) => {
|
66 | z[name] = z[name].toFixed(2)
|
67 | })
|
68 | console.log(
|
69 | 'Range: %d - %d ms\n' +
|
70 | 'Median: %d ms\n' +
|
71 | 'Average: %d ms'
|
72 | , z.min, z.max, z.med, z.avg)
|
73 | })
|
74 | }
|