-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathconvertToJSON.py
108 lines (101 loc) · 2.8 KB
/
convertToJSON.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# ===--- convertToJSON.py -------------------------------------------------===//
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See http://swift.org/LICENSE.txt for license information
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
# ===----------------------------------------------------------------------===//
# This script converts results from pre-commit benchmark tests to JSON.
# Usage: PrecommitBench_O | convertToJSON.py
#
# Input example:
# #,TEST,SAMPLES,MIN(ms),MAX(ms),MEAN(ms),SD(ms),MEDIAN(ms)
# 1,2Sum,1,1318,1318,1318,0,1318
# 2,Ackermann,1,805,805,805,0,805
#
# Totals,2,2123,2123,2123,0,0
#
# Output for this input:
# {
# "Machine": {},
# "Run": {},
# "Tests": [
# {
# "Data": [
# 1318
# ],
# "Info": {},
# "Name": [
# "2Sum"
# ]
# },
# {
# "Data": [
# 805
# ],
# "Info": {},
# "Name": [
# "Ackermann"
# ]
# },
# {
# "Data": [
# 2123
# ],
# "Info": {},
# "Name": [
# "Totals"
# ]
# }
# ]
# }
import sys
import json
import re
# Parse lines like this
# #,TEST,SAMPLES,MIN(ms),MAX(ms),MEAN(ms),SD(ms),MEDIAN(ms)
SCORERE=re.compile(r"(\d+),[ \t]*(\w+),[ \t]*([\d.]+),[ \t]*([\d.]+)")
# The Totals line would be parsed like this.
TOTALRE=re.compile(r"()(Totals),[ \t]*([\d.]+),[ \t]*([\d.]+)")
KEYGROUP=2
VALGROUP=4
def getScores(fname):
scores = {}
runs = 0
f = open(fname)
try:
for line in f:
if VERBOSE: print "Parsing", line,
m = SCORERE.match(line)
if not m:
continue
if not m.group(KEYGROUP) in scores:
scores[m.group(KEYGROUP)] = []
scores[m.group(KEYGROUP)].append(parseFloat(m.group(VALGROUP)))
if len(scores[m.group(KEYGROUP)]) > runs:
runs = len(scores[m.group(KEYGROUP)])
finally:
f.close()
return scores, runs
if __name__ == "__main__":
data = {}
data['Tests'] = []
data['Machine'] = {}
data['Run'] = {}
for line in sys.stdin:
m = SCORERE.match(line)
if not m:
m = TOTALRE.match(line)
if not m:
continue
test = {}
test['Data'] = [int(m.group(VALGROUP))]
test['Info'] = {}
test['Name'] = [m.group(KEYGROUP)]
data['Tests'].append(test)
print(json.dumps(data, sort_keys=True, indent=4))