UNPKG

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