aboutsummaryrefslogtreecommitdiff
path: root/tests/bench/bench.sh
blob: 68232c60e0221611b83ad2c384b059ec20b5c859 (plain) (blame)
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
#!/bin/bash
# o3 against plain code: each kernel in tests/programs/bench.zc on each backend, with
# the time to start up and compile (the 'z' run) taken off. needs bc.
cd "$(dirname "$0")/../.."; T=$(mktemp -d); trap 'rm -rf $T' EXIT
CC=${CC:-$(sh src/tools/findcc.sh)} || exit 1
make -s build/core/genome.bin || exit 1
# the interpreter core only with INTERP=1 (it is slow, and the rows are rarely wanted)
vms=(); names=()
if [ "$INTERP" = 1 ]; then
	$CC -O2 -w -DJTYPE=int -DZYINTERP src/core/core.c src/core/io_linux.c -Isrc/core -Ibuild/core/map --embed-dir=build/core -o $T/zy || exit 1
	vms+=("$T/zy"); names+=("interp")
fi
# the host's own jit, where there is one (x86-64 linux: blocks are translated, o3 regions compiled; ZYO3INTERP=1 runs them on the region interpreter)
if [ "$(uname -s)" = Linux ] && [ "$(uname -m)" = x86_64 ]; then
	$CC -O2 -w -DJTYPE=int src/core/core.c src/core/io_linux.c -Isrc/core -Ibuild/core/map --embed-dir=build/core -o $T/zyn && { vms+=("$T/zyn"); names+=("x86-64 jit"); }
fi
# the llvm: LLVM_CONFIG when given, else llvm-config, else llvm-config-19 (as in the Makefile)
LC=${LLVM_CONFIG:-$(command -v llvm-config || command -v llvm-config-19)}
if [ -n "$LC" ]; then
	$CC -O2 -w -DJTYPE=int -DZYLLVM $($LC --cflags) src/core/core.c src/core/io_linux.c -Isrc/core -Ibuild/core/map --embed-dir=build/core -o $T/zyl $($LC --ldflags --libs) && { vms+=("$T/zyl"); names+=("llvm"); }
fi
if command -v aarch64-linux-gnu-gcc >/dev/null && command -v qemu-aarch64 >/dev/null; then
	aarch64-linux-gnu-gcc -O2 -w -DJTYPE=int -static src/core/core.c src/core/io_linux.c -Isrc/core -Ibuild/core/map --embed-dir=build/core -o $T/zyj && { vms+=("qemu-aarch64 $T/zyj"); names+=("arm64 (qemu)"); }
fi
# seconds per run of kernel $2 on vm $1: the slope between $3 runs and 1 run,
# best of three each, so starting up and compiling cancel out
# a nonzero zy exit (1 compile error, 2 runtime trap) fails the whole bench, with the tail of
# stderr and stdout (zy reports a trap on stdout)
t(){
	local s e c err=$(mktemp); s=$(date +%s.%N)
	(cat tests/programs/bench.zc; printf '`%s%s' $2 $3) | $1 >$err.out 2>$err; c=$?
	e=$(date +%s.%N)
	if [ $c != 0 ]; then echo "bench.sh: tests/programs/bench.zc kernel $2 ($3 reps) on $1: zy exited $c ($([ $c = 2 ] && echo runtime trap || { [ $c = 1 ] && echo compile error || echo failed; })); stderr and stdout tail:" >&2; tail -n 8 $err $err.out >&2; rm -f $err $err.out; return 1; fi
	rm -f $err $err.out; echo "$e - $s" | bc
}
best(){ local b=999 x i; for i in 1 2 3; do x=$(t "$1" $2 $3) || return 1; b=$(echo "if ($x < $b) $x else $b" | bc); done; echo $b; }
# grows the run count until the difference is at least a second, so the noise
# of starting up is small next to it
per(){
	local one n=4 many
	one=$(best "$1" $2 1) || return 1
	while :; do
		many=$(best "$1" $2 $n) || return 1
		if [ "$(echo "$many - $one > 1" | bc)" = 1 ] || [ $n -ge 4096 ]; then break; fi
		n=$((n * 4))
	done
	echo "scale=6; ($many - $one) / ($n - 1)" | bc
}
printf "%-14s %-8s %10s %10s %8s\n" backend kernel "plain ms" "o3 ms" speedup
for i in "${!vms[@]}"; do
	for k in "collatz a b" "sieve c d" "digits e f" "fib g h"; do
		set -- $k
		p=$(per "${vms[$i]}" $2) || exit 1; o=$(per "${vms[$i]}" $3) || exit 1
		printf "%-14s %-8s %10.2f %10.2f %7.1fx\n" "${names[$i]}" $1 $(echo "$p * 1000" | bc) $(echo "$o * 1000" | bc) $(echo "scale=2; $p / $o" | bc)
	done
done