git » dnss » commit d8e5ab7

tools: Add an utility tool for caching and diffing benchmarks

author Alberto Bertogli
2015-10-24 09:35:57 UTC
committer Alberto Bertogli
2015-10-24 15:48:31 UTC
parent 8b2591613965d5e275f7c11a3548c8443ccc7d63

tools: Add an utility tool for caching and diffing benchmarks

This patch adds a small utility that helps run and diff benchmarks, using
"go test -bench" and "benchcmp".

It's only used for development and not meant to be portable, or have a
stable interface.

tools/bench +119 -0

diff --git a/tools/bench b/tools/bench
new file mode 100755
index 0000000..45a007e
--- /dev/null
+++ b/tools/bench
@@ -0,0 +1,119 @@
+#!/bin/bash
+#
+# This is a small utility that helps run and diff benchmarks, using
+# "go test -bench" and "benchcmp".
+#
+# It's only used for development and not meant to be portable, or have a
+# stable interface.
+#
+# Examples:
+#   # Run the benchmarks, recording the output IFF the tree is not dirty.
+#   ./tools/bench
+#
+#   # Diff between two recorded commits.
+#   ./tools/bench diff 8b25916 HEAD
+#
+#   # Run the benchmarks without recording, and compare against a commit.
+#   ./tools/bench rundiff 8b25916
+#
+
+set -e
+
+cd "$(git rev-parse --show-toplevel)"
+
+BDIR=".bench-history"
+
+# Get a filename based on the current commit.
+function commit_fname() {
+	git log --date=format:"%F-%H:%M" --pretty=format:"%cd__%h__%f" -1 $1
+}
+
+
+MODE=bench
+RUN_COUNT=3
+BEST=
+NO_RECORD=
+
+# Don't record results for a dirty tree.
+# Note this tool is explicitly excluded so we can easily test old commits.
+DIRTY=$(git status --porcelain | grep -v tools/bench | grep -v "^??" | wc -l)
+if [ "$DIRTY" -gt 0 ]; then
+		echo "Dirty tree, not recording results"
+		NO_RECORD=1
+fi
+
+while getopts "m:c:1rbn" OPT ; do
+	case $OPT in
+		m)
+			MODE=$OPTARG
+			;;
+		1)
+			RUN_COUNT=1
+			;;
+		c)
+			RUN_COUNT=$OPTARG
+			;;
+		b)
+			BEST="-best"
+			;;
+		n)
+			NO_RECORD=1
+			;;
+		\?)
+			exit 1
+			;;
+	esac
+done
+
+shift $((OPTIND-1))
+
+if [ $1 ]; then
+	MODE=$1
+	shift
+fi
+
+if [ $MODE == bench ]; then
+	FNAME=$BDIR/$(commit_fname)
+	RAWFNAME=$BDIR/.$(commit_fname).raw
+
+	if [ $NO_RECORD ]; then
+		go test -run=NONE -bench=. -benchmem ./...
+		exit
+	fi
+
+	echo -n "Running: "
+	echo > "$RAWFNAME"
+	for i in `seq $RUN_COUNT`; do
+		go test -run=NONE -bench=. -benchmem ./... >> "$RAWFNAME"
+		echo -n "$i "
+	done
+	echo
+
+	# Filter and sort the results to make them more succint and easier to
+	# compare.
+	cat "$RAWFNAME" | grep allocs | sort > "$FNAME"
+
+	cat "$FNAME"
+
+elif [ $MODE == diff ]; then
+	F1=$BDIR/$(commit_fname $1)
+	F2=$BDIR/$(commit_fname $2)
+	benchcmp $BEST "$F1" "$F2"
+
+elif [ $MODE == rundiff ]; then
+	TMPF=$(mktemp)
+	F1=$BDIR/$(commit_fname $1)
+
+	go test -run=NONE -bench=. -benchmem ./... > $TMPF
+	benchcmp -best "$F1" "$TMPF"
+
+	rm $TMPF
+
+elif [ $MODE == ls ]; then
+	cd $BDIR
+	ls -1
+else
+	echo "Unknown mode $MODE"
+	exit 1
+fi
+