2019-06-26 00:23:03 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
# The MIT License (MIT)
|
|
|
|
# Copyright (c) 2019 Damien P. George
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2023-03-17 11:49:47 -04:00
|
|
|
import time
|
2019-06-26 00:23:03 -04:00
|
|
|
import argparse
|
|
|
|
from glob import glob
|
2023-03-17 11:49:47 -04:00
|
|
|
from rich.live import Live
|
|
|
|
from rich.console import Console
|
|
|
|
from rich.table import Table
|
2019-06-26 00:23:03 -04:00
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
sys.path.append("../tools")
|
2019-06-26 00:23:03 -04:00
|
|
|
import pyboard
|
|
|
|
|
|
|
|
# Paths for host executables
|
2020-03-22 22:26:08 -04:00
|
|
|
if os.name == "nt":
|
|
|
|
CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3.exe")
|
|
|
|
MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/windows/micropython.exe")
|
2019-06-26 00:23:03 -04:00
|
|
|
else:
|
2020-03-22 22:26:08 -04:00
|
|
|
CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3")
|
|
|
|
MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/micropython")
|
2019-06-26 00:23:03 -04:00
|
|
|
|
|
|
|
PYTHON_TRUTH = CPYTHON3
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
BENCH_SCRIPT_DIR = "perf_bench/"
|
|
|
|
|
2019-06-26 00:23:03 -04:00
|
|
|
|
|
|
|
def compute_stats(lst):
|
|
|
|
avg = 0
|
|
|
|
var = 0
|
|
|
|
for x in lst:
|
|
|
|
avg += x
|
|
|
|
var += x * x
|
|
|
|
avg /= len(lst)
|
2022-01-29 17:14:01 -05:00
|
|
|
var = max(0, var / len(lst) - avg**2)
|
|
|
|
return avg, var**0.5
|
2019-06-26 00:23:03 -04:00
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2023-03-17 11:49:47 -04:00
|
|
|
def run_script_on_target(target, script, run_command=None):
|
2020-03-22 22:26:08 -04:00
|
|
|
output = b""
|
2019-06-26 00:23:03 -04:00
|
|
|
err = None
|
|
|
|
|
|
|
|
if isinstance(target, pyboard.Pyboard):
|
|
|
|
# Run via pyboard interface
|
|
|
|
try:
|
|
|
|
target.enter_raw_repl()
|
2023-03-17 11:49:47 -04:00
|
|
|
start_ts = time.monotonic_ns()
|
2019-06-26 00:23:03 -04:00
|
|
|
output = target.exec_(script)
|
2023-03-17 11:49:47 -04:00
|
|
|
if run_command:
|
|
|
|
start_ts = time.monotonic_ns()
|
|
|
|
output = target.exec_(run_command)
|
|
|
|
end_ts = time.monotonic_ns()
|
2019-06-26 00:23:03 -04:00
|
|
|
except pyboard.PyboardError as er:
|
2023-03-17 11:49:47 -04:00
|
|
|
end_ts = time.monotonic_ns()
|
2019-06-26 00:23:03 -04:00
|
|
|
err = er
|
2023-03-17 11:49:47 -04:00
|
|
|
finally:
|
|
|
|
target.exit_raw_repl()
|
2019-06-26 00:23:03 -04:00
|
|
|
else:
|
|
|
|
# Run local executable
|
|
|
|
try:
|
2023-03-17 11:49:47 -04:00
|
|
|
if run_command:
|
|
|
|
script += run_command
|
|
|
|
start_ts = time.monotonic_ns()
|
2020-03-22 22:26:08 -04:00
|
|
|
p = subprocess.run(
|
|
|
|
target, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, input=script
|
|
|
|
)
|
2023-03-17 11:49:47 -04:00
|
|
|
end_ts = time.monotonic_ns()
|
2019-06-26 00:23:03 -04:00
|
|
|
output = p.stdout
|
|
|
|
except subprocess.CalledProcessError as er:
|
2023-03-17 11:49:47 -04:00
|
|
|
end_ts = time.monotonic_ns()
|
2019-06-26 00:23:03 -04:00
|
|
|
err = er
|
|
|
|
|
2023-03-17 11:49:47 -04:00
|
|
|
return str(output.strip(), "ascii"), err, (end_ts - start_ts) // 1000
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-06-26 00:23:03 -04:00
|
|
|
|
|
|
|
def run_feature_test(target, test):
|
2020-03-22 22:26:08 -04:00
|
|
|
with open("feature_check/" + test + ".py", "rb") as f:
|
2019-06-26 00:23:03 -04:00
|
|
|
script = f.read()
|
2023-03-17 11:49:47 -04:00
|
|
|
output, err, _ = run_script_on_target(target, script)
|
2019-06-26 00:23:03 -04:00
|
|
|
if err is None:
|
|
|
|
return output
|
|
|
|
else:
|
2020-03-22 22:26:08 -04:00
|
|
|
return "CRASH: %r" % err
|
|
|
|
|
2019-06-26 00:23:03 -04:00
|
|
|
|
2023-03-17 11:49:47 -04:00
|
|
|
def run_benchmark_on_target(target, script, run_command=None):
|
|
|
|
output, err, runtime_us = run_script_on_target(target, script, run_command)
|
2019-06-26 00:23:03 -04:00
|
|
|
if err is None:
|
|
|
|
time, norm, result = output.split(None, 2)
|
|
|
|
try:
|
2023-03-17 11:49:47 -04:00
|
|
|
return int(time), int(norm), result, runtime_us
|
2019-06-26 00:23:03 -04:00
|
|
|
except ValueError:
|
2023-03-17 11:49:47 -04:00
|
|
|
return -1, -1, "CRASH: %r" % output, runtime_us
|
2019-06-26 00:23:03 -04:00
|
|
|
else:
|
2023-03-17 11:49:47 -04:00
|
|
|
return -1, -1, "CRASH: %r" % err, runtime_us
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-06-26 00:23:03 -04:00
|
|
|
|
2023-03-17 11:49:47 -04:00
|
|
|
def run_benchmarks(console, target, param_n, param_m, n_average, test_list):
|
2020-03-22 22:26:08 -04:00
|
|
|
skip_complex = run_feature_test(target, "complex") != "complex"
|
2021-05-11 09:45:36 -04:00
|
|
|
skip_native = run_feature_test(target, "native_check") != "native"
|
2019-06-26 00:23:03 -04:00
|
|
|
|
2023-03-17 11:49:47 -04:00
|
|
|
table = Table(show_header=True)
|
|
|
|
table.add_column("Test")
|
|
|
|
table.add_column("Time", justify="right")
|
|
|
|
table.add_column("Score", justify="right")
|
|
|
|
table.add_column("Ref Time", justify="right")
|
|
|
|
|
|
|
|
live = Live(table, console=console)
|
|
|
|
live.start()
|
|
|
|
|
2019-06-26 00:23:03 -04:00
|
|
|
for test_file in sorted(test_list):
|
2023-03-17 11:49:47 -04:00
|
|
|
# print(test_file + ": ", end="")
|
2019-06-26 00:23:03 -04:00
|
|
|
|
|
|
|
# Check if test should be skipped
|
2019-10-15 01:46:06 -04:00
|
|
|
skip = (
|
2020-03-22 22:26:08 -04:00
|
|
|
skip_complex
|
|
|
|
and test_file.find("bm_fft") != -1
|
|
|
|
or skip_native
|
|
|
|
and test_file.find("viper_") != -1
|
2019-10-15 01:46:06 -04:00
|
|
|
)
|
2019-06-26 00:23:03 -04:00
|
|
|
if skip:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("skip")
|
2023-03-17 11:49:47 -04:00
|
|
|
table.add_row(test_file, *(["skip"] * 6))
|
2019-06-26 00:23:03 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Create test script
|
2020-03-22 22:26:08 -04:00
|
|
|
with open(test_file, "rb") as f:
|
2019-06-26 00:23:03 -04:00
|
|
|
test_script = f.read()
|
2020-03-22 22:26:08 -04:00
|
|
|
with open(BENCH_SCRIPT_DIR + "benchrun.py", "rb") as f:
|
2019-06-26 00:23:03 -04:00
|
|
|
test_script += f.read()
|
2023-03-17 11:49:47 -04:00
|
|
|
bm_run = b"bm_run(%u, %u)\n" % (param_n, param_m)
|
2019-06-26 00:23:03 -04:00
|
|
|
|
|
|
|
# Write full test script if needed
|
|
|
|
if 0:
|
2020-03-22 22:26:08 -04:00
|
|
|
with open("%s.full" % test_file, "wb") as f:
|
2019-06-26 00:23:03 -04:00
|
|
|
f.write(test_script)
|
|
|
|
|
|
|
|
# Run MicroPython a given number of times
|
|
|
|
times = []
|
2023-03-17 11:49:47 -04:00
|
|
|
runtimes = []
|
2019-06-26 00:23:03 -04:00
|
|
|
scores = []
|
|
|
|
error = None
|
|
|
|
result_out = None
|
|
|
|
for _ in range(n_average):
|
2023-03-17 11:49:47 -04:00
|
|
|
self_time, norm, result, runtime_us = run_benchmark_on_target(
|
|
|
|
target, test_script, bm_run
|
|
|
|
)
|
|
|
|
if self_time < 0 or norm < 0:
|
2019-06-26 00:23:03 -04:00
|
|
|
error = result
|
|
|
|
break
|
|
|
|
if result_out is None:
|
|
|
|
result_out = result
|
|
|
|
elif result != result_out:
|
2020-03-22 22:26:08 -04:00
|
|
|
error = "FAIL self"
|
2019-06-26 00:23:03 -04:00
|
|
|
break
|
2023-03-17 11:49:47 -04:00
|
|
|
times.append(self_time)
|
|
|
|
runtimes.append(runtime_us)
|
|
|
|
scores.append(1e6 * norm / self_time)
|
2019-06-26 00:23:03 -04:00
|
|
|
|
|
|
|
# Check result against truth if needed
|
2020-03-22 22:26:08 -04:00
|
|
|
if error is None and result_out != "None":
|
2023-03-17 11:49:47 -04:00
|
|
|
_, _, result_exp, _ = run_benchmark_on_target(PYTHON_TRUTH, test_script, bm_run)
|
2019-06-26 00:23:03 -04:00
|
|
|
if result_out != result_exp:
|
2020-03-22 22:26:08 -04:00
|
|
|
error = "FAIL truth"
|
2019-06-26 00:23:03 -04:00
|
|
|
|
|
|
|
if error is not None:
|
2023-03-17 11:49:47 -04:00
|
|
|
print(test_file, error)
|
|
|
|
if error == "no matching params":
|
|
|
|
table.add_row(test_file, *([None] * 3))
|
|
|
|
else:
|
|
|
|
table.add_row(test_file, *(["error"] * 3))
|
2019-06-26 00:23:03 -04:00
|
|
|
else:
|
|
|
|
t_avg, t_sd = compute_stats(times)
|
2023-03-17 11:49:47 -04:00
|
|
|
r_avg, r_sd = compute_stats(runtimes)
|
2019-06-26 00:23:03 -04:00
|
|
|
s_avg, s_sd = compute_stats(scores)
|
2023-03-17 11:49:47 -04:00
|
|
|
# print(
|
|
|
|
# "{:.2f} {:.4f} {:.2f} {:.4f} {:.2f} {:.4f}".format(
|
|
|
|
# t_avg, 100 * t_sd / t_avg, s_avg, 100 * s_sd / s_avg, r_avg, 100 * r_sd / r_avg
|
|
|
|
# )
|
|
|
|
# )
|
|
|
|
table.add_row(
|
|
|
|
test_file,
|
|
|
|
f"{t_avg:.2f}±{100 * t_sd / t_avg:.1f}%",
|
|
|
|
f"{s_avg:.2f}±{100 * s_sd / s_avg:.1f}%",
|
|
|
|
f"{r_avg:.2f}±{100 * r_sd / r_avg:.1f}%",
|
2020-03-22 22:26:08 -04:00
|
|
|
)
|
2019-06-26 00:23:03 -04:00
|
|
|
if 0:
|
2020-03-22 22:26:08 -04:00
|
|
|
print(" times: ", times)
|
|
|
|
print(" scores:", scores)
|
2019-06-26 00:23:03 -04:00
|
|
|
|
2023-03-17 11:49:47 -04:00
|
|
|
live.update(table, refresh=True)
|
|
|
|
live.stop()
|
2019-06-26 00:23:03 -04:00
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-06-26 00:23:03 -04:00
|
|
|
def parse_output(filename):
|
|
|
|
with open(filename) as f:
|
|
|
|
params = f.readline()
|
|
|
|
n, m, _ = params.strip().split()
|
2020-03-22 22:26:08 -04:00
|
|
|
n = int(n.split("=")[1])
|
|
|
|
m = int(m.split("=")[1])
|
2019-06-26 00:23:03 -04:00
|
|
|
data = []
|
|
|
|
for l in f:
|
2020-03-22 22:26:08 -04:00
|
|
|
if l.find(": ") != -1 and l.find(": skip") == -1 and l.find("CRASH: ") == -1:
|
|
|
|
name, values = l.strip().split(": ")
|
2019-06-26 00:23:03 -04:00
|
|
|
values = tuple(float(v) for v in values.split())
|
|
|
|
data.append((name,) + values)
|
|
|
|
return n, m, data
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-06-26 00:23:03 -04:00
|
|
|
def compute_diff(file1, file2, diff_score):
|
|
|
|
# Parse output data from previous runs
|
|
|
|
n1, m1, d1 = parse_output(file1)
|
|
|
|
n2, m2, d2 = parse_output(file2)
|
|
|
|
|
|
|
|
# Print header
|
|
|
|
if diff_score:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("diff of scores (higher is better)")
|
2019-06-26 00:23:03 -04:00
|
|
|
else:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("diff of microsecond times (lower is better)")
|
2019-06-26 00:23:03 -04:00
|
|
|
if n1 == n2 and m1 == m2:
|
2020-03-22 22:26:08 -04:00
|
|
|
hdr = "N={} M={}".format(n1, m1)
|
2019-06-26 00:23:03 -04:00
|
|
|
else:
|
2020-03-22 22:26:08 -04:00
|
|
|
hdr = "N={} M={} vs N={} M={}".format(n1, m1, n2, m2)
|
|
|
|
print(
|
|
|
|
"{:24} {:>10} -> {:>10} {:>10} {:>7}% (error%)".format(
|
|
|
|
hdr, file1, file2, "diff", "diff"
|
|
|
|
)
|
|
|
|
)
|
2019-06-26 00:23:03 -04:00
|
|
|
|
|
|
|
# Print entries
|
|
|
|
while d1 and d2:
|
|
|
|
if d1[0][0] == d2[0][0]:
|
|
|
|
# Found entries with matching names
|
|
|
|
entry1 = d1.pop(0)
|
|
|
|
entry2 = d2.pop(0)
|
2020-03-22 22:26:08 -04:00
|
|
|
name = entry1[0].rsplit("/")[-1]
|
2019-06-26 00:23:03 -04:00
|
|
|
av1, sd1 = entry1[1 + 2 * diff_score], entry1[2 + 2 * diff_score]
|
|
|
|
av2, sd2 = entry2[1 + 2 * diff_score], entry2[2 + 2 * diff_score]
|
2020-03-22 22:26:08 -04:00
|
|
|
sd1 *= av1 / 100 # convert from percent sd to absolute sd
|
|
|
|
sd2 *= av2 / 100 # convert from percent sd to absolute sd
|
2019-06-26 00:23:03 -04:00
|
|
|
av_diff = av2 - av1
|
2022-01-29 17:14:01 -05:00
|
|
|
sd_diff = (sd1**2 + sd2**2) ** 0.5
|
2019-06-26 00:23:03 -04:00
|
|
|
percent = 100 * av_diff / av1
|
|
|
|
percent_sd = 100 * sd_diff / av1
|
2020-03-22 22:26:08 -04:00
|
|
|
print(
|
|
|
|
"{:24} {:10.2f} -> {:10.2f} : {:+10.2f} = {:+7.3f}% (+/-{:.2f}%)".format(
|
|
|
|
name, av1, av2, av_diff, percent, percent_sd
|
|
|
|
)
|
|
|
|
)
|
2019-06-26 00:23:03 -04:00
|
|
|
elif d1[0][0] < d2[0][0]:
|
|
|
|
d1.pop(0)
|
|
|
|
else:
|
|
|
|
d2.pop(0)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-06-26 00:23:03 -04:00
|
|
|
def main():
|
2020-03-22 22:26:08 -04:00
|
|
|
cmd_parser = argparse.ArgumentParser(description="Run benchmarks for MicroPython")
|
|
|
|
cmd_parser.add_argument(
|
|
|
|
"-t", "--diff-time", action="store_true", help="diff time outputs from a previous run"
|
|
|
|
)
|
|
|
|
cmd_parser.add_argument(
|
|
|
|
"-s", "--diff-score", action="store_true", help="diff score outputs from a previous run"
|
|
|
|
)
|
|
|
|
cmd_parser.add_argument(
|
|
|
|
"-p", "--pyboard", action="store_true", help="run tests via pyboard.py"
|
|
|
|
)
|
|
|
|
cmd_parser.add_argument(
|
|
|
|
"-d", "--device", default="/dev/ttyACM0", help="the device for pyboard.py"
|
|
|
|
)
|
|
|
|
cmd_parser.add_argument("-a", "--average", default="8", help="averaging number")
|
|
|
|
cmd_parser.add_argument(
|
|
|
|
"--emit", default="bytecode", help="MicroPython emitter to use (bytecode or native)"
|
|
|
|
)
|
|
|
|
cmd_parser.add_argument("N", nargs=1, help="N parameter (approximate target CPU frequency)")
|
|
|
|
cmd_parser.add_argument("M", nargs=1, help="M parameter (approximate target heap in kbytes)")
|
|
|
|
cmd_parser.add_argument("files", nargs="*", help="input test files")
|
2019-06-26 00:23:03 -04:00
|
|
|
args = cmd_parser.parse_args()
|
|
|
|
|
|
|
|
if args.diff_time or args.diff_score:
|
|
|
|
compute_diff(args.N[0], args.M[0], args.diff_score)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
# N, M = 50, 25 # esp8266
|
|
|
|
# N, M = 100, 100 # pyboard, esp32
|
|
|
|
# N, M = 1000, 1000 # PC
|
|
|
|
N = int(args.N[0])
|
|
|
|
M = int(args.M[0])
|
|
|
|
n_average = int(args.average)
|
|
|
|
|
|
|
|
if args.pyboard:
|
|
|
|
target = pyboard.Pyboard(args.device)
|
|
|
|
target.enter_raw_repl()
|
|
|
|
else:
|
2020-03-22 22:26:08 -04:00
|
|
|
target = [MICROPYTHON, "-X", "emit=" + args.emit]
|
2019-06-26 00:23:03 -04:00
|
|
|
|
|
|
|
if len(args.files) == 0:
|
2020-03-22 22:26:08 -04:00
|
|
|
tests_skip = ("benchrun.py",)
|
2019-06-26 00:23:03 -04:00
|
|
|
if M <= 25:
|
|
|
|
# These scripts are too big to be compiled by the target
|
2020-03-22 22:26:08 -04:00
|
|
|
tests_skip += ("bm_chaos.py", "bm_hexiom.py", "misc_raytrace.py")
|
2019-06-26 00:23:03 -04:00
|
|
|
tests = sorted(
|
2020-03-22 22:26:08 -04:00
|
|
|
BENCH_SCRIPT_DIR + test_file
|
|
|
|
for test_file in os.listdir(BENCH_SCRIPT_DIR)
|
|
|
|
if test_file.endswith(".py") and test_file not in tests_skip
|
2019-06-26 00:23:03 -04:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
tests = sorted(args.files)
|
|
|
|
|
2023-03-17 11:49:47 -04:00
|
|
|
console = Console()
|
2020-03-22 22:26:08 -04:00
|
|
|
print("N={} M={} n_average={}".format(N, M, n_average))
|
2019-06-26 00:23:03 -04:00
|
|
|
|
2023-03-17 11:49:47 -04:00
|
|
|
run_benchmarks(console, target, N, M, n_average, tests)
|
2019-06-26 00:23:03 -04:00
|
|
|
|
|
|
|
if isinstance(target, pyboard.Pyboard):
|
|
|
|
target.exit_raw_repl()
|
|
|
|
target.close()
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-06-26 00:23:03 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|