File: bench

package info (click to toggle)
dnss 0.0~git20180721.0.2de63ab0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 232 kB
  • sloc: sh: 246; makefile: 5
file content (119 lines) | stat: -rwxr-xr-x 2,189 bytes parent folder | download | duplicates (2)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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.
#   ./tests/bench
#
#   # Diff between two recorded commits.
#   ./tests/bench diff 8b25916 HEAD
#
#   # Run the benchmarks without recording, and compare against a commit.
#   ./tests/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 tests/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