2 # Copyright 2012 Google, Inc. All rights reserved.
4 # This script provides a simple way to run benchmarks against previous code and
5 # keep a log of how benchmarks change over time. When used with the --benchmark
6 # flag, it runs benchmarks from the current code and from the last commit run
7 # with --benchmark, then stores the results in the git commit description. We
8 # rerun the old benchmarks along with the new ones, since there's no guarantee
9 # that git commits will happen on the same machine, so machine differences could
10 # cause wildly inaccurate results.
12 # If you're making changes to 'gopacket' which could cause performance changes,
13 # you may be requested to use this commit script to make sure your changes don't
14 # have large detrimental effects (or to show off how awesome your performance
17 # If not run with the --benchmark flag, this script is still very useful... it
18 # makes sure all the correct go formatting, building, and testing work as
23 USAGE: $0 [--benchmark regexp] [--root] [--gen] <git commit flags...>
25 --benchmark: Run benchmark comparisons against last benchmark'd commit
26 --root: Run tests that require root priviledges
27 --gen: Generate code for MACs/ports by pulling down external data
29 Note, some 'git commit' flags are necessary, if all else fails, pass in -a
37 while [ ! -z "$1" ]; do
68 if [ ! -z "$ROOT" ]; then
70 # Some folks (like me) keep source code in places inaccessible by root (like
71 # NFS), so to make sure things run smoothly we copy them to a /tmp location.
72 local tmpfile="$(mktemp -t gopacket_XXXXXXXX)"
73 echo "Running root test executable $exec as $tmpfile"
81 if [ "$#" -eq "0" ]; then
87 # Check for copyright notices.
88 for filename in $(find ./ -type f -name '*.go'); do
89 if ! head -n 1 "$filename" | grep -q Copyright; then
90 echo "File '$filename' may not have copyright notice"
98 if [ ! -z "$ROOT" ]; then
99 echo "Running SUDO to get root priviledges for root tests"
100 sudo echo "have root"
103 if [ ! -z "$GEN" ]; then
105 go run gen.go | gofmt > valid_mac_prefixes.go
108 go run gen.go | gofmt > iana_ports.go
112 # Make sure everything is formatted, compiles, and tests pass.
114 go test -i ./... 2>/dev/null >/dev/null || true
117 pushd examples/bytediff
120 if [ -f /usr/include/pcap.h ]; then
124 go build pcap_tester.go
125 Root pcap_tester --mode=basic
126 Root pcap_tester --mode=filtered
127 Root pcap_tester --mode=timestamp || echo "You might not support timestamp sources"
129 pushd examples/pcapdump
132 pushd examples/arpscan
135 pushd examples/bidirectional
138 pushd examples/synscan
141 pushd examples/httpassembly
144 pushd examples/statsassembly
168 if [ -f /usr/include/linux/if_packet.h ]; then
169 if grep -q TPACKET_V3 /usr/include/linux/if_packet.h; then
176 if [ -f /usr/include/pfring.h ]; then
181 pushd examples/pfdump
186 for travis_script in `ls .travis.*.sh`; do
190 # Run our initial commit
193 if [ -z "$BENCH" ]; then
195 echo "We're not benchmarking and we've committed... we're done!"
199 ### If we get here, we want to run benchmarks from current commit, and compare
200 ### then to benchmarks from the last --benchmark commit.
202 # Get our current branch.
203 BRANCH="$(git branch | grep '^*' | awk '{print $2}')"
205 # File we're going to build our commit description in.
206 COMMIT_FILE="$(mktemp /tmp/tmp.XXXXXXXX)"
208 # Add the word "BENCH" to the start of the git commit.
209 echo -n "BENCH " > $COMMIT_FILE
211 # Get the current description... there must be an easier way.
212 git log -n 1 | grep '^ ' | sed 's/^ //' >> $COMMIT_FILE
214 # Get the commit sha for the last benchmark commit
215 PREV=$(git log -n 1 --grep='BENCHMARK_MARKER_DO_NOT_CHANGE' | head -n 1 | awk '{print $2}')
217 ## Run current benchmarks
219 cat >> $COMMIT_FILE <<EOF
222 ----------------------------------------------------------
223 BENCHMARK_MARKER_DO_NOT_CHANGE
224 ----------------------------------------------------------
226 Go version $(go version)
229 TEST BENCHMARKS "$BENCH"
231 # go seems to have trouble with 'go test --bench=. ./...'
232 go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
234 go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
236 cat >> $COMMIT_FILE <<EOF
241 if [ "$BENCH" -eq ".*" ]; then
242 go run pcap/gopacket_benchmark/*.go 2>&1 | tee -a $COMMIT_FILE
247 ## Reset to last benchmark commit, run benchmarks
251 cat >> $COMMIT_FILE <<EOF
252 ----------------------------------------------------------
253 BENCHMARKING AGAINST COMMIT $PREV
254 ----------------------------------------------------------
259 # go seems to have trouble with 'go test --bench=. ./...'
260 go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
262 go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
264 cat >> $COMMIT_FILE <<EOF
269 if [ "$BENCH" -eq ".*" ]; then
270 go run pcap/gopacket_benchmark/*.go 2>&1 | tee -a $COMMIT_FILE
275 ## Reset back to the most recent commit, edit the commit message by appending
276 ## benchmark results.
278 git commit --amend -F $COMMIT_FILE