summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--bench/Makefile2
-rw-r--r--bench/bld.sub17
-rw-r--r--bench/copious-allocs.myr2
-rw-r--r--bench/runbench.myr68
-rw-r--r--bld.proj1
6 files changed, 88 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index 1ba6637..3451ef4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -27,8 +27,5 @@ parse/gram.h
6/6m
muse/muse
myrbuild/myrbuild
-bench/*
-!bench/Makefile
-!bench/*.myr
*~
tags
diff --git a/bench/Makefile b/bench/Makefile
index 362a3db..685b0cf 100644
--- a/bench/Makefile
+++ b/bench/Makefile
@@ -1,6 +1,6 @@
BIN=runner
OBJ=runner.o
-BENCHSRC=intsort.myr \
+BENCHSRC= intsort.myr \
copious-allocs.myr \
sha1-compute.myr \
bigfactorial.myr \
diff --git a/bench/bld.sub b/bench/bld.sub
new file mode 100644
index 0000000..c6f6365
--- /dev/null
+++ b/bench/bld.sub
@@ -0,0 +1,17 @@
+bin intsort = intsort.myr ;;
+bin copious-allocs = copious-allocs.myr ;;
+bin sha1-compute = sha1-compute.myr ;;
+bin bigfactorial = bigfactorial.myr ;;
+bin mandelbrot = mandelbrot.myr ;;
+bin regex-match = regex-match.myr ;;
+bin runbench = runbench.myr ;;
+
+cmd benchit =
+ ./runbench
+ intsort
+ copious-allocs
+ sha1-compute
+ bigfactorial
+ mandelbrot
+ regex-match
+;;
diff --git a/bench/copious-allocs.myr b/bench/copious-allocs.myr
index 886adde..1237b7f 100644
--- a/bench/copious-allocs.myr
+++ b/bench/copious-allocs.myr
@@ -43,7 +43,7 @@ const shuffle = {a
rng = std.mksrng(123)
for i = 0; i < a.len - 1; i++
- j = std.rand(rng, i, a.len)
+ j = std.rngrand(rng, i, a.len)
t = a[j]
a[j] = a[i]
a[i] = t
diff --git a/bench/runbench.myr b/bench/runbench.myr
new file mode 100644
index 0000000..74ddfa5
--- /dev/null
+++ b/bench/runbench.myr
@@ -0,0 +1,68 @@
+use std
+
+const Nsamp = 10
+
+const main = {args : byte[:][:]
+ var tot : flt64
+
+ std.put("Running benchmarks: {} samples per binary\n", Nsamp);
+ tot = 0.0;
+ for arg in args
+ tot = tot + timeit(arg)
+ ;;
+ std.put("total:\t{}s\n", tot);
+}
+
+
+generic perror = {a : @a::(numeric,integral), msg : byte[:] -> @a
+ if a < 0
+ std.fatal("{}", msg)
+ else
+ -> a
+ ;;
+}
+
+const timeit = {prog -> flt64
+ var avg, m, d, x, n : flt64
+
+ avg = 0.0;
+ m = 0.0;
+ n = 0.0;
+ for var i = 0; i < Nsamp; i++
+ n = n + 1.0;
+ x = run(prog);
+ d = (x - avg);
+ avg = avg + d/n;
+ m = m + d*(x - avg);
+ ;;
+ std.put("{}:\t{}s (σ^2: {})\n", prog, avg, m/(n-1.0));
+ -> avg;
+}
+
+const run = {prog -> flt64
+ var infd, outfd
+ var pid
+ var tm
+
+ tm = std.now()
+ pid = std.fork();
+ if pid < 0
+ std.fatal("Could not fork\n");
+ elif pid == 0
+ infd = perror(std.open("/dev/zero", std.Ordonly), "could not open /dev/zero")
+ outfd = perror(std.open("/dev/null", std.Owronly), "could not open /dev/null")
+ perror(std.dup2(infd, 0), "could not redirect stdin")
+ perror(std.dup2(outfd, 1), "could not redirect stdout")
+ std.execv(prog, [prog][:])
+ std.fatal("Failed to exec\n")
+ else
+ match std.wait(pid)
+ | `std.Wfailure: std.fatal("could not wait\n")
+ | `std.Waiterror: std.fatal("running benchmark failed\n")
+ | `std.Wsignalled: std.fatal("running benchmark failed\n")
+ | `std.Wsuccess: /* nothing */
+ ;;
+ ;;
+ -> (std.now() - tm castto(flt64)) / 1_000_000.0
+}
+
diff --git a/bld.proj b/bld.proj
index 2cb5ff9..bbcd931 100644
--- a/bld.proj
+++ b/bld.proj
@@ -2,5 +2,6 @@ sub =
mbld
lib
test
+ bench
;;