| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
# Works with python2.6 |
| 6 |
|
| 7 |
import json |
| 8 |
import math |
| 9 |
import os |
| 10 |
import sys |
| 11 |
from operator import itemgetter |
| 12 |
from subprocess import PIPE, Popen |
| 13 |
|
| 14 |
|
| 15 |
class Test: |
| 16 |
def __init__(self, path, name): |
| 17 |
self.path = path |
| 18 |
self.name = name |
| 19 |
|
| 20 |
@classmethod |
| 21 |
def from_file(cls, path, name, options): |
| 22 |
return cls(path, name) |
| 23 |
|
| 24 |
|
| 25 |
def find_tests(dir, substring=None): |
| 26 |
ans = [] |
| 27 |
for dirpath, dirnames, filenames in os.walk(dir): |
| 28 |
if dirpath == ".": |
| 29 |
continue |
| 30 |
for filename in filenames: |
| 31 |
if not filename.endswith(".js"): |
| 32 |
continue |
| 33 |
test = os.path.join(dirpath, filename) |
| 34 |
if substring is None or substring in os.path.relpath(test, dir): |
| 35 |
ans.append([test, filename]) |
| 36 |
return ans |
| 37 |
|
| 38 |
|
| 39 |
def get_test_cmd(path): |
| 40 |
return [JS, "-f", path] |
| 41 |
|
| 42 |
|
| 43 |
def avg(seq): |
| 44 |
return sum(seq) / len(seq) |
| 45 |
|
| 46 |
|
| 47 |
def stddev(seq, mean): |
| 48 |
diffs = ((float(item) - mean) ** 2 for item in seq) |
| 49 |
return math.sqrt(sum(diffs) / len(seq)) |
| 50 |
|
| 51 |
|
| 52 |
def run_test(test): |
| 53 |
env = os.environ.copy() |
| 54 |
env["MOZ_GCTIMER"] = "stderr" |
| 55 |
cmd = get_test_cmd(test.path) |
| 56 |
total = [] |
| 57 |
mark = [] |
| 58 |
sweep = [] |
| 59 |
close_fds = sys.platform != "win32" |
| 60 |
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=close_fds, env=env) |
| 61 |
out, err = p.communicate() |
| 62 |
out, err = out.decode(), err.decode() |
| 63 |
|
| 64 |
float_array = [float(_) for _ in err.split()] |
| 65 |
|
| 66 |
if len(float_array) == 0: |
| 67 |
print("Error: No data from application. Configured with --enable-gctimer?") |
| 68 |
sys.exit(1) |
| 69 |
|
| 70 |
for i, currItem in enumerate(float_array): |
| 71 |
if i % 3 == 0: |
| 72 |
total.append(currItem) |
| 73 |
else: |
| 74 |
if i % 3 == 1: |
| 75 |
mark.append(currItem) |
| 76 |
else: |
| 77 |
sweep.append(currItem) |
| 78 |
|
| 79 |
return max(total), avg(total), max(mark), avg(mark), max(sweep), avg(sweep) |
| 80 |
|
| 81 |
|
| 82 |
def run_tests(tests, test_dir): |
| 83 |
bench_map = {} |
| 84 |
|
| 85 |
try: |
| 86 |
for i, test in enumerate(tests): |
| 87 |
filename_str = '"%s"' % test.name |
| 88 |
TMax, TAvg, MMax, MAvg, SMax, SAvg = run_test(test) |
| 89 |
bench_map[test.name] = [TMax, TAvg, MMax, MAvg, SMax, SAvg] |
| 90 |
fmt = '%20s: {"TMax": %4.1f, "TAvg": %4.1f, "MMax": %4.1f, "MAvg": %4.1f, "SMax": %4.1f, "SAvg": %4.1f}' # NOQA: E501 |
| 91 |
if i != len(tests) - 1: |
| 92 |
fmt += "," |
| 93 |
print(fmt % (filename_str, TMax, TAvg, MMax, MAvg, SMax, MAvg)) |
| 94 |
except KeyboardInterrupt: |
| 95 |
print("fail") |
| 96 |
|
| 97 |
return dict( |
| 98 |
( |
| 99 |
filename, |
| 100 |
dict(TMax=TMax, TAvg=TAvg, MMax=MMax, MAvg=MAvg, SMax=SMax, SAvg=SAvg), |
| 101 |
) |
| 102 |
for filename, (TMax, TAvg, MMax, MAvg, SMax, SAvg) in bench_map.iteritems() |
| 103 |
) |
| 104 |
|
| 105 |
|
| 106 |
def compare(current, baseline): |
| 107 |
percent_speedups = [] |
| 108 |
for key, current_result in current.iteritems(): |
| 109 |
try: |
| 110 |
baseline_result = baseline[key] |
| 111 |
except KeyError: |
| 112 |
print(key, "missing from baseline") |
| 113 |
continue |
| 114 |
|
| 115 |
val_getter = itemgetter("TMax", "TAvg", "MMax", "MAvg", "SMax", "SAvg") |
| 116 |
BTMax, BTAvg, BMMax, BMAvg, BSMax, BSAvg = val_getter(baseline_result) |
| 117 |
CTMax, CTAvg, CMMax, CMAvg, CSMax, CSAvg = val_getter(current_result) |
| 118 |
|
| 119 |
if CTAvg <= BTAvg: |
| 120 |
speedup = (CTAvg / BTAvg - 1) * 100 |
| 121 |
result = "faster: %6.2f < baseline %6.2f (%+6.2f%%)" % ( |
| 122 |
CTAvg, |
| 123 |
BTAvg, |
| 124 |
speedup, |
| 125 |
) |
| 126 |
percent_speedups.append(speedup) |
| 127 |
else: |
| 128 |
slowdown = (CTAvg / BTAvg - 1) * 100 |
| 129 |
result = "SLOWER: %6.2f > baseline %6.2f (%+6.2f%%) " % ( |
| 130 |
CTAvg, |
| 131 |
BTAvg, |
| 132 |
slowdown, |
| 133 |
) |
| 134 |
percent_speedups.append(slowdown) |
| 135 |
print("%30s: %s" % (key, result)) |
| 136 |
if percent_speedups: |
| 137 |
print("Average speedup: %.2f%%" % avg(percent_speedups)) |
| 138 |
|
| 139 |
|
| 140 |
if __name__ == "__main__": |
| 141 |
script_path = os.path.abspath(__file__) |
| 142 |
script_dir = os.path.dirname(script_path) |
| 143 |
test_dir = os.path.join(script_dir, "tests") |
| 144 |
|
| 145 |
from optparse import OptionParser |
| 146 |
|
| 147 |
op = OptionParser(usage="%prog [options] JS_SHELL [TESTS]") |
| 148 |
|
| 149 |
op.add_option( |
| 150 |
"-b", |
| 151 |
"--baseline", |
| 152 |
metavar="JSON_PATH", |
| 153 |
dest="baseline_path", |
| 154 |
help="json file with baseline values to " "compare against", |
| 155 |
) |
| 156 |
|
| 157 |
(OPTIONS, args) = op.parse_args() |
| 158 |
if len(args) < 1: |
| 159 |
op.error("missing JS_SHELL argument") |
| 160 |
# We need to make sure we are using backslashes on Windows. |
| 161 |
JS, test_args = os.path.normpath(args[0]), args[1:] |
| 162 |
|
| 163 |
test_list = [] |
| 164 |
bench_map = {} |
| 165 |
|
| 166 |
test_list = find_tests(test_dir) |
| 167 |
|
| 168 |
if not test_list: |
| 169 |
print >> sys.stderr, "No tests found matching command line arguments." |
| 170 |
sys.exit(0) |
| 171 |
|
| 172 |
test_list = [Test.from_file(tst, name, OPTIONS) for tst, name in test_list] |
| 173 |
|
| 174 |
try: |
| 175 |
print("{") |
| 176 |
bench_map = run_tests(test_list, test_dir) |
| 177 |
print("}") |
| 178 |
|
| 179 |
except OSError: |
| 180 |
if not os.path.exists(JS): |
| 181 |
print >> sys.stderr, "JS shell argument: file does not exist: '%s'" % JS |
| 182 |
sys.exit(1) |
| 183 |
else: |
| 184 |
raise |
| 185 |
|
| 186 |
if OPTIONS.baseline_path: |
| 187 |
baseline_map = [] |
| 188 |
fh = open(OPTIONS.baseline_path) |
| 189 |
baseline_map = json.load(fh) |
| 190 |
fh.close() |
| 191 |
compare(current=bench_map, baseline=baseline_map) |