1
0
forked from Mirror/wren
Files
wren/benchmark/run_bench

289 lines
7.4 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python
from __future__ import print_function
2013-11-22 08:55:22 -08:00
import argparse
2013-11-22 08:55:22 -08:00
import math
import os
2014-01-23 23:29:50 -08:00
import os.path
2013-11-22 08:55:22 -08:00
import re
import subprocess
import sys
2014-01-23 23:29:50 -08:00
# Runs the tests.
WREN_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
BENCHMARK_DIR = os.path.join(WREN_DIR, 'benchmark')
# How many times to run a given benchmark.
NUM_TRIALS = 10
BENCHMARKS = []
def BENCHMARK(name, pattern):
regex = re.compile(pattern + "\n" + r"elapsed: (\d+\.\d+)", re.MULTILINE)
BENCHMARKS.append([name, regex, None])
BENCHMARK("binary_trees", """stretch tree of depth 13 check: -1
8192 trees of depth 4 check: -8192
2048 trees of depth 6 check: -2048
512 trees of depth 8 check: -512
128 trees of depth 10 check: -128
32 trees of depth 12 check: -32
long lived tree of depth 12 check: -1""")
BENCHMARK("delta_blue", "7032700")
BENCHMARK("fib", r"""317811
317811
317811
317811
317811""")
2014-01-20 08:43:10 -08:00
BENCHMARK("for", r"""499999500000""")
2013-12-24 21:04:11 -08:00
BENCHMARK("method_call", r"""true
false""")
2013-11-22 08:55:22 -08:00
LANGUAGES = [
2014-01-23 23:29:50 -08:00
("wren", ["../wren"], ".wren"),
2014-01-13 07:29:47 -08:00
("lua", ["lua"], ".lua"),
("luajit (-joff)", ["luajit", "-joff"], ".lua"),
("python", ["python"], ".py"),
2014-02-12 17:22:42 -08:00
("python3", ["python3"], ".py"),
2014-01-13 07:29:47 -08:00
("ruby", ["ruby"], ".rb")
2013-11-22 08:55:22 -08:00
]
2014-04-20 21:04:41 -07:00
results = {}
2013-11-22 09:17:45 -08:00
def green(text):
if sys.platform == 'win32':
return text
return '\033[32m' + text + '\033[0m'
def red(text):
if sys.platform == 'win32':
return text
return '\033[31m' + text + '\033[0m'
def yellow(text):
if sys.platform == 'win32':
return text
return '\033[33m' + text + '\033[0m'
def get_score(time):
"""
Converts time into a "score". This is the inverse of the time with an
arbitrary scale applied to get the number in a nice range. The goal here is
to have benchmark results where faster = bigger number.
"""
return 1000.0 / time
2013-11-22 08:55:22 -08:00
def run_trial(benchmark, language):
"""Runs one benchmark one time for one language."""
2014-01-13 07:29:47 -08:00
args = []
args.extend(language[1])
2014-01-23 23:29:50 -08:00
args.append(os.path.join(BENCHMARK_DIR, benchmark[0] + language[2]))
2013-11-22 08:55:22 -08:00
out = subprocess.check_output(args, universal_newlines=True)
match = benchmark[1].match(out)
if match:
return float(match.group(1))
else:
print("Incorrect output:")
print(out)
2013-11-22 08:55:22 -08:00
return None
2014-04-20 21:04:41 -07:00
def run_benchmark_language(benchmark, language, benchmark_result):
"""
Runs one benchmark for a number of trials for one language.
Adds the result to benchmark_result, which is a map of language names to
results.
"""
name = "{0} - {1}".format(benchmark[0], language[0])
print("{0:30s}".format(name), end=' ')
2013-11-22 08:55:22 -08:00
2014-01-23 23:29:50 -08:00
if not os.path.exists(os.path.join(
BENCHMARK_DIR, benchmark[0] + language[2])):
print("No implementation for this language")
return
2013-11-22 08:55:22 -08:00
times = []
2013-11-22 09:17:45 -08:00
for i in range(0, NUM_TRIALS):
time = run_trial(benchmark, language)
if not time:
return
times.append(time)
2013-11-22 08:55:22 -08:00
sys.stdout.write(".")
best = min(times)
score = get_score(best)
2013-11-22 08:55:22 -08:00
comparison = ""
if language[0] == "wren":
if benchmark[2] != None:
ratio = 100 * score / benchmark[2]
comparison = "{:6.2f}% relative to baseline".format(ratio)
if ratio > 105:
comparison = green(comparison)
if ratio < 95:
comparison = red(comparison)
else:
comparison = "no baseline"
else:
2014-04-20 21:04:41 -07:00
# Hack: assumes wren gets run first.
wren_score = benchmark_result["wren"]["score"]
ratio = 100.0 * wren_score / score
comparison = "{:6.2f}%".format(ratio)
if ratio > 105:
comparison = green(comparison)
if ratio < 95:
comparison = red(comparison)
print(" {:5.0f} {:4.2f}s {:s}".format(score, best, comparison))
2014-04-20 21:04:41 -07:00
benchmark_result[language[0]] = {
"desc": name,
"times": times,
"score": score
}
return score
2013-11-22 09:17:45 -08:00
def run_benchmark(benchmark, languages):
"""Runs one benchmark for the given languages (or all of them)."""
2014-04-20 21:04:41 -07:00
benchmark_result = {}
results[benchmark[0]] = benchmark_result
num_languages = 0
for language in LANGUAGES:
if not languages or language[0] in languages:
num_languages += 1
2014-04-20 21:04:41 -07:00
run_benchmark_language(benchmark, language, benchmark_result)
if num_languages > 1:
2014-04-20 21:04:41 -07:00
graph_results(benchmark_result)
2014-04-20 21:04:41 -07:00
def graph_results(benchmark_result):
print()
2013-11-22 09:17:45 -08:00
INCREMENT = {
'-': 'o',
'o': 'O',
'O': '0',
'0': '0'
}
# Scale everything by the highest score.
2013-11-22 09:17:45 -08:00
highest = 0
2014-04-20 21:04:41 -07:00
for language, result in benchmark_result.items():
score = get_score(min(result["times"]))
if score > highest: highest = score
2013-11-22 09:17:45 -08:00
print("{0:30s}0 {1:66.0f}".format("", highest))
2014-04-20 21:04:41 -07:00
for language, result in benchmark_result.items():
line = ["-"] * 68
2014-04-20 21:04:41 -07:00
for time in result["times"]:
index = int(get_score(time) / highest * 67)
line[index] = INCREMENT[line[index]]
print("{0:30s}{1}".format(result["desc"], "".join(line)))
print()
2013-11-22 09:17:45 -08:00
def read_baseline():
if os.path.exists("baseline.txt"):
with open("baseline.txt") as f:
for line in f.readlines():
name, best = line.split(",")
for benchmark in BENCHMARKS:
if benchmark[0] == name:
benchmark[2] = float(best)
def generate_baseline():
print("generating baseline")
baseline_text = ""
for benchmark in BENCHMARKS:
2014-04-20 21:04:41 -07:00
best = run_benchmark_language(benchmark, LANGUAGES[0], {})
baseline_text += ("{},{}\n".format(benchmark[0], best))
# Write them to a file.
with open("baseline.txt", 'w') as out:
out.write(baseline_text)
2014-04-20 21:04:41 -07:00
def print_html():
'''Print the results as an HTML chart.'''
def print_benchmark(benchmark, name):
print('<h3>{}</h3>'.format(name))
print('<table class="chart">')
2014-04-20 21:04:41 -07:00
# Scale everything by the highest score.
highest = 0
for language, result in results[benchmark].items():
score = get_score(min(result["times"]))
if score > highest: highest = score
languages = sorted(results[benchmark].keys(),
key=lambda lang: results[benchmark][lang]["score"], reverse=True)
for language in languages:
result = results[benchmark][language]
score = int(result["score"])
ratio = int(100 * score / highest)
css_class = "chart-bar"
if language == "wren":
css_class += " wren"
print(' <tr>')
print(' <th>{}</th><td><div class="{}" style="width: {}%;">{}&nbsp;</div></td>'.format(
language, css_class, ratio, score))
print(' </tr>')
print('</table>')
2014-04-20 21:04:41 -07:00
print_benchmark("method_call", "Method Call")
print_benchmark("delta_blue", "DeltaBlue")
print_benchmark("binary_trees", "Binary Trees")
print_benchmark("fib", "Recursive Fibonacci")
def main():
parser = argparse.ArgumentParser(description="Run the benchmarks")
parser.add_argument("benchmark", nargs='?',
default="all",
help="The benchmark to run")
parser.add_argument("--generate-baseline",
action="store_true",
help="Generate a baseline file")
parser.add_argument("-l", "--language",
action="append",
help="Which language(s) to run benchmarks for")
2014-04-20 21:04:41 -07:00
parser.add_argument("--output-html",
action="store_true",
help="Output the results chart as HTML")
args = parser.parse_args()
if args.generate_baseline:
generate_baseline()
return
read_baseline()
2014-04-20 21:04:41 -07:00
# Run the benchmarks.
for benchmark in BENCHMARKS:
2014-04-20 21:04:41 -07:00
if benchmark[0] == args.benchmark or args.benchmark == "all":
run_benchmark(benchmark, args.language)
2013-11-22 09:17:45 -08:00
2014-04-20 21:04:41 -07:00
if args.output_html:
print_html()
main()