2 # Copyright 2015 the V8 project authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
8 Convert a perf trybot JSON file into a pleasing HTML page. It can read
9 from standard input or via the --filename option. Examples:
11 cat results.json | %prog --title "ia32 results"
12 %prog -f results.json -t "ia32 results" -o results.html
18 from optparse import OptionParser
24 PERCENT_CONSIDERED_SIGNIFICANT = 0.5
25 PROBABILITY_CONSIDERED_SIGNIFICANT = 0.02
26 PROBABILITY_CONSIDERED_MEANINGLESS = 0.05
29 def ComputeZ(baseline_avg, baseline_sigma, mean, n):
30 if baseline_sigma == 0:
32 return abs((mean - baseline_avg) / (baseline_sigma / math.sqrt(n)))
35 # Values from http://www.fourmilab.ch/rpkp/experiments/analysis/zCalc.html
36 def ComputeProbability(z):
37 if z > 2.575829: # p 0.005: two sided < 0.01
39 if z > 2.326348: # p 0.010
41 if z > 2.170091: # p 0.015
43 if z > 2.053749: # p 0.020
45 if z > 1.959964: # p 0.025: two sided < 0.05
47 if z > 1.880793: # p 0.030
49 if z > 1.811910: # p 0.035
51 if z > 1.750686: # p 0.040
53 if z > 1.695397: # p 0.045
55 if z > 1.644853: # p 0.050: two sided < 0.10
57 if z > 1.281551: # p 0.100: two sided < 0.20
59 return 0.20 # two sided p >= 0.20
63 def __init__(self, test_name, count, hasScoreUnits, result, sigma,
64 master_result, master_sigma):
65 self.result_ = float(result)
66 self.sigma_ = float(sigma)
67 self.master_result_ = float(master_result)
68 self.master_sigma_ = float(master_sigma)
69 self.significant_ = False
71 self.percentage_string_ = ""
72 # compute notability and significance.
74 compare_num = 100*self.result_/self.master_result_ - 100
76 compare_num = 100*self.master_result_/self.result_ - 100
77 if abs(compare_num) > 0.1:
78 self.percentage_string_ = "%3.1f" % (compare_num)
79 z = ComputeZ(self.master_result_, self.master_sigma_, self.result_, count)
80 p = ComputeProbability(z)
81 if p < PROBABILITY_CONSIDERED_SIGNIFICANT:
82 self.significant_ = True
83 if compare_num >= PERCENT_CONSIDERED_SIGNIFICANT:
85 elif compare_num <= -PERCENT_CONSIDERED_SIGNIFICANT:
94 def master_result(self):
95 return self.master_result_
97 def master_sigma(self):
98 return self.master_sigma_
100 def percentage_string(self):
101 return self.percentage_string_;
103 def isSignificant(self):
104 return self.significant_
106 def isNotablyPositive(self):
107 return self.notable_ > 0
109 def isNotablyNegative(self):
110 return self.notable_ < 0
114 def __init__(self, name, data):
118 # strip off "<name>/" prefix
119 test_name = test.split("/")[1]
120 self.appendResult(test_name, data[test])
122 # tests is a dictionary of Results
126 def SortedTestKeys(self):
127 keys = self.tests_.keys()
138 def appendResult(self, test_name, test_data):
139 with_string = test_data["result with patch "]
140 data = with_string.split()
141 master_string = test_data["result without patch"]
142 master_data = master_string.split()
143 runs = int(test_data["runs"])
144 units = test_data["units"]
145 hasScoreUnits = units == "score"
146 self.tests_[test_name] = Result(test_name,
150 master_data[0], master_data[2])
153 class BenchmarkRenderer:
154 def __init__(self, output_file):
155 self.print_output_ = []
156 self.output_file_ = output_file
158 def Print(self, str_data):
159 self.print_output_.append(str_data)
161 def FlushOutput(self):
162 string_data = "\n".join(self.print_output_)
164 if self.output_file_:
166 with open(self.output_file_, "w") as text_file:
167 text_file.write(string_data)
171 def RenderOneBenchmark(self, benchmark):
173 self.Print("<a name=\"" + benchmark.name() + "\">")
174 self.Print(benchmark.name() + "</a> <a href=\"#top\">(top)</a>")
176 self.Print("<table class=\"benchmark\">")
177 self.Print("<thead>")
178 self.Print(" <th>Test</th>")
179 self.Print(" <th>Result</th>")
180 self.Print(" <th>Master</th>")
181 self.Print(" <th>%</th>")
182 self.Print("</thead>")
183 self.Print("<tbody>")
184 tests = benchmark.tests()
185 for test in benchmark.SortedTestKeys():
188 self.Print(" <td>" + test + "</td>")
189 self.Print(" <td>" + str(t.result()) + "</td>")
190 self.Print(" <td>" + str(t.master_result()) + "</td>")
192 res = t.percentage_string()
193 if t.isSignificant():
195 if t.isNotablyPositive():
196 res = self.green(res)
197 elif t.isNotablyNegative():
199 self.Print(" <td>" + res + "</td>")
201 self.Print("</tbody>")
202 self.Print("</table>")
204 def ProcessJSONData(self, data, title):
205 self.Print("<h1>" + title + "</h1>")
207 for benchmark in data:
208 if benchmark != "errors":
209 self.Print("<li><a href=\"#" + benchmark + "\">" + benchmark + "</a></li>")
211 for benchmark in data:
212 if benchmark != "errors":
213 benchmark_object = Benchmark(benchmark, data[benchmark])
214 self.RenderOneBenchmark(benchmark_object)
216 def bold(self, data):
217 return "<b>" + data + "</b>"
220 return "<font color=\"red\">" + data + "</font>"
223 def green(self, data):
224 return "<font color=\"green\">" + data + "</font>"
226 def PrintHeader(self):
229 <title>Output</title>
230 <style type="text/css">
232 Style inspired by Andy Ferra's gist at https://gist.github.com/andyferra/2554919
235 font-family: Helvetica, arial, sans-serif;
239 padding-bottom: 10px;
240 background-color: white;
243 h1, h2, h3, h4, h5, h6 {
247 -webkit-font-smoothing: antialiased;
258 border-bottom: 1px solid #cccccc;
279 p, blockquote, ul, ol, dl, li, table, pre {
284 display: inline-block;
291 ul :first-child, ol :first-child {
295 ul :last-child, ol :last-child {
304 border-top: 1px solid #cccccc;
305 background-color: white;
310 table tr:nth-child(2n) {
311 background-color: #f8f8f8;
316 border: 1px solid #cccccc;
322 border: 1px solid #cccccc;
327 table tr th :first-child, table tr td :first-child {
330 table tr th :last-child, table tr td :last-child {
339 def PrintFooter(self):
346 def Render(opts, args):
348 with open(opts.filename) as json_data:
349 data = json.load(json_data)
351 # load data from stdin
352 data = json.load(sys.stdin)
357 title = opts.filename
359 title = "Benchmark results"
360 renderer = BenchmarkRenderer(opts.output)
361 renderer.PrintHeader()
362 renderer.ProcessJSONData(data, title)
363 renderer.PrintFooter()
364 renderer.FlushOutput()
367 if __name__ == '__main__':
368 parser = OptionParser(usage=__doc__)
369 parser.add_option("-f", "--filename", dest="filename",
370 help="Specifies the filename for the JSON results "
371 "rather than reading from stdin.")
372 parser.add_option("-t", "--title", dest="title",
373 help="Optional title of the web page.")
374 parser.add_option("-o", "--output", dest="output",
375 help="Write html output to this file rather than stdout.")
377 (opts, args) = parser.parse_args()