shithub: mc

Download patch

ref: 8441b63d07bc96852b3ac986adb6d816d1412954
parent: a7f23ccc3e024fd71451c285d9afec55f220d8df
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Sep 3 15:05:42 EDT 2017

Add benchmark output to testr.

--- a/lib/testr/testr.myr
+++ b/lib/testr/testr.myr
@@ -13,6 +13,7 @@
 	;;
 
 	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,19 @@
 	const softfail	: (ctx : ctx#, msg : byte[:], args : ... -> void)
 ;;
 
+const Nsamp = 1000;
+
+const bench = {specs
+	std.put("MBENCH {}\n", specs.len)
+	for s : specs
+		benchspec(&s)
+	;;
+}
+
 const run = {specs
 	std.put("MTEST {}\n", specs.len)
 	for s : specs
-		runspec(&s)
+		testspec(&s)
 	;;
 }
 
@@ -64,9 +74,10 @@
 	ctx.reason = std.fmtv(msg, ap)
 }
 
-const runspec = {ts
+const benchspec = {ts
+	var avg, m, d, n
+	var start, dt
 	var ctx : ctx
-	var status, reason
 	var jmpbuf
 
 	ctx.ok = true
@@ -73,6 +84,38 @@
 	ctx.reason = ""
 	ctx.jmpbuf = &jmpbuf
 
+	avg = 0.0;
+	m = 0.0;
+	n = 0.0;
+	std.put("bench {} <<{{!\n", ts.name)
+	if !std.setjmp(&jmpbuf)
+		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("!}}>> timings {} {} {}\n", Nsamp, avg, m)
+	else
+		std.put("!}}>> fail {}\n", ctx.reason)
+		std.slfree(ctx.reason)
+	;;
+}
+
+const testspec = {ts
+	var ctx : ctx
+	var jmpbuf
+
+	ctx.ok = true
+	ctx.reason = ""
+	ctx.jmpbuf = &jmpbuf
+
 	std.put("test {} <<{{!\n", ts.name)
 	if !std.setjmp(&jmpbuf)
 		ts.fn(&ctx)
@@ -79,12 +122,9 @@
 	;;
 
 	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)
 }