perf-to-html.py 8.98 KB
Newer Older
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 109 110 111 112 113 114 115 116 117
#!/usr/bin/env python
# Copyright 2015 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
'''
python %prog

Convert a perf trybot JSON file into a pleasing HTML page. It can read
from standard input or via the --filename option. Examples:

  cat results.json | %prog --title "ia32 results"
  %prog -f results.json -t "ia32 results" -o results.html
'''

import commands
import json
import math
from optparse import OptionParser
import os
import shutil
import sys
import tempfile

PERCENT_CONSIDERED_SIGNIFICANT = 0.5
PROBABILITY_CONSIDERED_SIGNIFICANT = 0.02
PROBABILITY_CONSIDERED_MEANINGLESS = 0.05


def ComputeZ(baseline_avg, baseline_sigma, mean, n):
  if baseline_sigma == 0:
    return 1000.0;
  return abs((mean - baseline_avg) / (baseline_sigma / math.sqrt(n)))


# Values from http://www.fourmilab.ch/rpkp/experiments/analysis/zCalc.html
def ComputeProbability(z):
  if z > 2.575829: # p 0.005: two sided < 0.01
    return 0
  if z > 2.326348: # p 0.010
    return 0.01
  if z > 2.170091: # p 0.015
    return 0.02
  if z > 2.053749: # p 0.020
    return 0.03
  if z > 1.959964: # p 0.025: two sided < 0.05
    return 0.04
  if z > 1.880793: # p 0.030
    return 0.05
  if z > 1.811910: # p 0.035
    return 0.06
  if z > 1.750686: # p 0.040
    return 0.07
  if z > 1.695397: # p 0.045
    return 0.08
  if z > 1.644853: # p 0.050: two sided < 0.10
    return 0.09
  if z > 1.281551: # p 0.100: two sided < 0.20
    return 0.10
  return 0.20 # two sided p >= 0.20


class Result:
  def __init__(self, test_name, count, hasScoreUnits, result, sigma,
               master_result, master_sigma):
    self.result_ = float(result)
    self.sigma_ = float(sigma)
    self.master_result_ = float(master_result)
    self.master_sigma_ = float(master_sigma)
    self.significant_ = False
    self.notable_ = 0
    self.percentage_string_ = ""
    # compute notability and significance.
    if hasScoreUnits:
      compare_num = 100*self.result_/self.master_result_ - 100
    else:
      compare_num = 100*self.master_result_/self.result_ - 100
    if abs(compare_num) > 0.1:
      self.percentage_string_ = "%3.1f" % (compare_num)
      z = ComputeZ(self.master_result_, self.master_sigma_, self.result_, count)
      p = ComputeProbability(z)
      if p < PROBABILITY_CONSIDERED_SIGNIFICANT:
        self.significant_ = True
      if compare_num >= PERCENT_CONSIDERED_SIGNIFICANT:
        self.notable_ = 1
      elif compare_num <= -PERCENT_CONSIDERED_SIGNIFICANT:
        self.notable_ = -1

  def result(self):
    return self.result_

  def sigma(self):
    return self.sigma_

  def master_result(self):
    return self.master_result_

  def master_sigma(self):
    return self.master_sigma_

  def percentage_string(self):
    return self.percentage_string_;

  def isSignificant(self):
    return self.significant_

  def isNotablyPositive(self):
    return self.notable_ > 0

  def isNotablyNegative(self):
    return self.notable_ < 0


class Benchmark:
  def __init__(self, name, data):
    self.name_ = name
    self.tests_ = {}
    for test in data:
118 119
      # strip off "<name>/" prefix, allowing for subsequent "/"s
      test_name = test.split("/", 1)[1]
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
      self.appendResult(test_name, data[test])

  # tests is a dictionary of Results
  def tests(self):
    return self.tests_

  def SortedTestKeys(self):
    keys = self.tests_.keys()
    keys.sort()
    t = "Total"
    if t in keys:
      keys.remove(t)
      keys.append(t)
    return keys

  def name(self):
    return self.name_

  def appendResult(self, test_name, test_data):
    with_string = test_data["result with patch   "]
    data = with_string.split()
    master_string = test_data["result without patch"]
    master_data = master_string.split()
    runs = int(test_data["runs"])
    units = test_data["units"]
    hasScoreUnits = units == "score"
    self.tests_[test_name] = Result(test_name,
                                    runs,
                                    hasScoreUnits,
                                    data[0], data[2],
                                    master_data[0], master_data[2])


class BenchmarkRenderer:
  def __init__(self, output_file):
    self.print_output_ = []
    self.output_file_ = output_file

  def Print(self, str_data):
    self.print_output_.append(str_data)

  def FlushOutput(self):
    string_data = "\n".join(self.print_output_)
    print_output = []
    if self.output_file_:
      # create a file
      with open(self.output_file_, "w") as text_file:
        text_file.write(string_data)
    else:
      print(string_data)

  def RenderOneBenchmark(self, benchmark):
    self.Print("<h2>")
    self.Print("<a name=\"" + benchmark.name() + "\">")
    self.Print(benchmark.name() + "</a> <a href=\"#top\">(top)</a>")
    self.Print("</h2>");
    self.Print("<table class=\"benchmark\">")
    self.Print("<thead>")
    self.Print("  <th>Test</th>")
    self.Print("  <th>Result</th>")
    self.Print("  <th>Master</th>")
    self.Print("  <th>%</th>")
    self.Print("</thead>")
    self.Print("<tbody>")
    tests = benchmark.tests()
    for test in benchmark.SortedTestKeys():
      t = tests[test]
      self.Print("  <tr>")
      self.Print("    <td>" + test + "</td>")
      self.Print("    <td>" + str(t.result()) + "</td>")
      self.Print("    <td>" + str(t.master_result()) + "</td>")
      t = tests[test]
      res = t.percentage_string()
      if t.isSignificant():
        res = self.bold(res)
      if t.isNotablyPositive():
        res = self.green(res)
      elif t.isNotablyNegative():
        res = self.red(res)
      self.Print("    <td>" + res + "</td>")
      self.Print("  </tr>")
    self.Print("</tbody>")
    self.Print("</table>")

  def ProcessJSONData(self, data, title):
    self.Print("<h1>" + title + "</h1>")
    self.Print("<ul>")
    for benchmark in data:
     if benchmark != "errors":
       self.Print("<li><a href=\"#" + benchmark + "\">" + benchmark + "</a></li>")
    self.Print("</ul>")
    for benchmark in data:
      if benchmark != "errors":
        benchmark_object = Benchmark(benchmark, data[benchmark])
        self.RenderOneBenchmark(benchmark_object)

  def bold(self, data):
    return "<b>" + data + "</b>"

  def red(self, data):
    return "<font color=\"red\">" + data + "</font>"


  def green(self, data):
    return "<font color=\"green\">" + data + "</font>"

  def PrintHeader(self):
    data = """<html>
<head>
<title>Output</title>
<style type="text/css">
/*
Style inspired by Andy Ferra's gist at https://gist.github.com/andyferra/2554919
*/
body {
  font-family: Helvetica, arial, sans-serif;
  font-size: 14px;
  line-height: 1.6;
  padding-top: 10px;
  padding-bottom: 10px;
  background-color: white;
  padding: 30px;
}
h1, h2, h3, h4, h5, h6 {
  margin: 20px 0 10px;
  padding: 0;
  font-weight: bold;
  -webkit-font-smoothing: antialiased;
  cursor: text;
  position: relative;
}
h1 {
  font-size: 28px;
  color: black;
}

h2 {
  font-size: 24px;
  border-bottom: 1px solid #cccccc;
  color: black;
}

h3 {
  font-size: 18px;
}

h4 {
  font-size: 16px;
}

h5 {
  font-size: 14px;
}

h6 {
  color: #777777;
  font-size: 14px;
}

p, blockquote, ul, ol, dl, li, table, pre {
  margin: 15px 0;
}

li p.first {
  display: inline-block;
}

ul, ol {
  padding-left: 30px;
}

ul :first-child, ol :first-child {
  margin-top: 0;
}

ul :last-child, ol :last-child {
  margin-bottom: 0;
}

table {
  padding: 0;
}

table tr {
  border-top: 1px solid #cccccc;
  background-color: white;
  margin: 0;
  padding: 0;
}

table tr:nth-child(2n) {
  background-color: #f8f8f8;
}

table tr th {
  font-weight: bold;
  border: 1px solid #cccccc;
  text-align: left;
  margin: 0;
  padding: 6px 13px;
}
table tr td {
  border: 1px solid #cccccc;
  text-align: left;
  margin: 0;
  padding: 6px 13px;
}
table tr th :first-child, table tr td :first-child {
  margin-top: 0;
}
table tr th :last-child, table tr td :last-child {
  margin-bottom: 0;
}
</style>
</head>
<body>
"""
    self.Print(data)

  def PrintFooter(self):
    data = """</body>
</html>
"""
    self.Print(data)


def Render(opts, args):
  if opts.filename:
    with open(opts.filename) as json_data:
      data = json.load(json_data)
  else:
    # load data from stdin
    data = json.load(sys.stdin)

  if opts.title:
    title = opts.title
  elif opts.filename:
    title = opts.filename
  else:
    title = "Benchmark results"
  renderer = BenchmarkRenderer(opts.output)
  renderer.PrintHeader()
  renderer.ProcessJSONData(data, title)
  renderer.PrintFooter()
  renderer.FlushOutput()


if __name__ == '__main__':
  parser = OptionParser(usage=__doc__)
  parser.add_option("-f", "--filename", dest="filename",
                    help="Specifies the filename for the JSON results "
                         "rather than reading from stdin.")
  parser.add_option("-t", "--title", dest="title",
                    help="Optional title of the web page.")
  parser.add_option("-o", "--output", dest="output",
                    help="Write html output to this file rather than stdout.")

  (opts, args) = parser.parse_args()
  Render(opts, args)