-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathparse.js
87 lines (74 loc) · 2.09 KB
/
parse.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module.exports = function parse (data) {
const lines = data.toString().trim().split('\n')
.map(function (line) {
return line.trim()
})
.filter(function (line) {
return line
})
const output = {}
while (lines.length && !output.type) {
const header = lines.shift().match(/^NANOBENCH(?: version (\d+))?$/)
if (!header) continue
output.type = 'NANOBENCH'
output.version = Number(header[1] || 1)
}
if (!output.type) {
throw new Error('No NANOBENCH header')
}
if (output.version !== 2) {
throw new Error('Can only parse version 2')
}
output.command = null
output.benchmarks = []
output.error = null
output.time = null
let benchmark = null
while (lines.length) {
const next = lines.shift()
if (next[0] === '>') {
output.command = next.slice(1).trim()
continue
}
if (!benchmark && next[0] === '#') {
benchmark = { name: null, output: [], error: null, time: null }
benchmark.name = next.slice(1).trim()
continue
}
if (benchmark && next[0] === '#') {
benchmark.output.push(next.slice(1).trim())
continue
}
if (!benchmark && /^fail /.test(next)) {
output.error = next.slice(5).trim()
continue
}
if (!benchmark && /^ok /.test(next)) {
output.error = null
output.time = time(next)
continue
}
if (benchmark && /^ok /.test(next)) {
benchmark.error = null
benchmark.time = time(next)
output.benchmarks.push(benchmark)
benchmark = null
continue
}
if (benchmark && /^fail /.test(next)) {
output.error = next.slice(5).trim()
output.benchmarks.push(benchmark)
benchmark = null
continue
}
}
return output
}
function time (line) {
const i = line.lastIndexOf('(')
const j = line.lastIndexOf(')')
if (i === -1 || j === -1 || j < i) throw new Error('Could not parse benchmark time')
const parsed = line.slice(i + 1, j).match(/^(\d+)\s*s\s*\+\s*(\d+)\s*ns$/)
if (!parsed) throw new Error('Could not parse benchmark time')
return [Number(parsed[1]), Number(parsed[2])]
}