summaryrefslogtreecommitdiff
path: root/lib/testr/testr.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/testr/testr.myr')
-rw-r--r--lib/testr/testr.myr76
1 files changed, 65 insertions, 11 deletions
diff --git a/lib/testr/testr.myr b/lib/testr/testr.myr
index 22a271c..19cdad6 100644
--- a/lib/testr/testr.myr
+++ b/lib/testr/testr.myr
@@ -13,6 +13,7 @@ pkg testr =
;;
const run : (specs : spec[:] -> void)
+ const bench : (specs : spec[:] -> void)
const ok : (ctx : ctx# -> void)
const fail : (ctx : ctx#, msg : byte[:], args : ... -> void)
const check : (ctx : ctx#, cond : bool, msg : byte[:], args : ... -> void)
@@ -20,10 +21,17 @@ pkg testr =
const softfail : (ctx : ctx#, msg : byte[:], args : ... -> void)
;;
+const bench = {specs
+ std.put("MTEST {}\n", specs.len)
+ for s : specs
+ benchspec(&s)
+ ;;
+}
+
const run = {specs
std.put("MTEST {}\n", specs.len)
for s : specs
- runspec(&s)
+ testspec(&s)
;;
}
@@ -60,13 +68,62 @@ const softfail = {ctx, msg, args
}
const softfailv = {ctx, msg, ap
- ctx.ok = false
- ctx.reason = std.fmtv(msg, ap)
+ /* keep the first failure */
+ if ctx.ok
+ ctx.ok = false
+ ctx.reason = std.fmtv(msg, ap)
+ ;;
+}
+
+const benchspec = {ts
+ var avg, m, d, n, nsamp
+ var start, dt
+ var ctx : ctx
+ var jmpbuf
+
+ ctx.ok = true
+ ctx.reason = ""
+ ctx.jmpbuf = &jmpbuf
+
+ avg = 0.0;
+ m = 0.0;
+ n = 0.0;
+ nsamp = 0
+ std.put("test {} <<{{!\n", ts.name)
+ if !std.setjmp(&jmpbuf)
+ /* estimate samples */
+ start = std.now()
+ ts.fn(&ctx)
+ dt = (std.now() - start : flt64)
+
+ if dt == 0.0
+ nsamp = 1000_000
+ else
+ nsamp = (100_000.0/dt : int64)
+ nsamp = std.max(10, nsamp)
+ ;;
+
+ for var i = 0; i < nsamp; i++
+ n +=1.0;
+ start = std.now()
+ ts.fn(&ctx)
+ dt = (std.now() - start : flt64)/1_000_000.0
+ d = (dt - avg);
+ avg = avg + d/n;
+ m = m + d*(dt - avg);
+ ;;
+ ;;
+
+ if ctx.ok
+ std.put("!}}>> timing {} {} {}\n", nsamp, avg, m)
+ else
+ std.put("!}}>> fail {}\n", ctx.reason)
+ std.slfree(ctx.reason)
+ ;;
}
-const runspec = {ts
+const testspec = {ts
var ctx : ctx
- var status, reason
var jmpbuf
ctx.ok = true
@@ -79,12 +136,9 @@ const runspec = {ts
;;
if ctx.ok
- status = "ok"
- reason = ""
+ std.put("!}}>> ok\n")
else
- status = "fail"
- reason = ctx.reason
+ std.put("!}}>> fail {}\n", ctx.reason)
+ std.slfree(ctx.reason)
;;
- std.put("!}}>> {} {}\n", status, reason)
- std.slfree(reason)
}