blob: b1d8d2e1f670520483f29407784353fc3a853eff [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301#!/bin/bash
2# Copyright 2012 Google, Inc. All rights reserved.
3
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.
11#
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
15# improvements are).
16#
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
19# expected.
20
21function Usage {
22 cat <<EOF
23USAGE: $0 [--benchmark regexp] [--root] [--gen] <git commit flags...>
24
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
28
29Note, some 'git commit' flags are necessary, if all else fails, pass in -a
30EOF
31 exit 1
32}
33
34BENCH=""
35GEN=""
36ROOT=""
37while [ ! -z "$1" ]; do
38 case "$1" in
39 "--benchmark")
40 BENCH="$2"
41 shift
42 shift
43 ;;
44 "--gen")
45 GEN="yes"
46 shift
47 ;;
48 "--root")
49 ROOT="yes"
50 shift
51 ;;
52 "--help")
53 Usage
54 ;;
55 "-h")
56 Usage
57 ;;
58 "help")
59 Usage
60 ;;
61 *)
62 break
63 ;;
64 esac
65done
66
67function Root {
68 if [ ! -z "$ROOT" ]; then
69 local exec="$1"
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"
74 cp "$exec" "$tmpfile"
75 chmod a+x "$tmpfile"
76 shift
77 sudo "$tmpfile" "$@"
78 fi
79}
80
81if [ "$#" -eq "0" ]; then
82 Usage
83fi
84
85cd $(dirname $0)
86
87# Check for copyright notices.
88for 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"
91 exit 1
92 fi
93done
94
95set -e
96set -x
97
98if [ ! -z "$ROOT" ]; then
99 echo "Running SUDO to get root priviledges for root tests"
100 sudo echo "have root"
101fi
102
103if [ ! -z "$GEN" ]; then
104 pushd macs
105 go run gen.go | gofmt > valid_mac_prefixes.go
106 popd
107 pushd layers
108 go run gen.go | gofmt > iana_ports.go
109 go run gen2.go | gofmt > enums_generated.go
110 popd
111fi
112
113# Make sure everything is formatted, compiles, and tests pass.
114go fmt ./...
115go test -i ./... 2>/dev/null >/dev/null || true
116go test
117go build
118pushd examples/bytediff
119go build
120popd
121if [ -f /usr/include/pcap.h ]; then
122 pushd pcap
123 go test ./...
124 go build ./...
125 go build pcap_tester.go
126 Root pcap_tester --mode=basic
127 Root pcap_tester --mode=filtered
128 Root pcap_tester --mode=timestamp || echo "You might not support timestamp sources"
129 popd
130 pushd examples/afpacket
131 go build
132 popd
133 pushd examples/pcapdump
134 go build
135 popd
136 pushd examples/arpscan
137 go build
138 popd
139 pushd examples/bidirectional
140 go build
141 popd
142 pushd examples/synscan
143 go build
144 popd
145 pushd examples/httpassembly
146 go build
147 popd
148 pushd examples/statsassembly
149 go build
150 popd
151fi
152pushd macs
153go test ./...
154gofmt -w gen.go
155go build gen.go
156popd
157pushd tcpassembly
158go test ./...
159popd
160pushd reassembly
161go test ./...
162popd
163pushd layers
164gofmt -w gen.go
165go build gen.go
166go test ./...
167popd
168pushd pcapgo
169go test ./...
170go build ./...
171popd
172if [ -f /usr/include/linux/if_packet.h ]; then
173 if grep -q TPACKET_V3 /usr/include/linux/if_packet.h; then
174 pushd afpacket
175 go build ./...
176 go test ./...
177 popd
178 fi
179fi
180if [ -f /usr/include/pfring.h ]; then
181 pushd pfring
182 go test ./...
183 go build ./...
184 popd
185 pushd examples/pfdump
186 go build
187 popd
188fi
189pushd ip4defrag
190go test ./...
191popd
192pushd defrag
193go test ./...
194popd
195
196for travis_script in `ls .travis.*.sh`; do
197 ./$travis_script
198done
199
200# Run our initial commit
201git commit "$@"
202
203if [ -z "$BENCH" ]; then
204 set +x
205 echo "We're not benchmarking and we've committed... we're done!"
206 exit
207fi
208
209### If we get here, we want to run benchmarks from current commit, and compare
210### then to benchmarks from the last --benchmark commit.
211
212# Get our current branch.
213BRANCH="$(git branch | grep '^*' | awk '{print $2}')"
214
215# File we're going to build our commit description in.
216COMMIT_FILE="$(mktemp /tmp/tmp.XXXXXXXX)"
217
218# Add the word "BENCH" to the start of the git commit.
219echo -n "BENCH " > $COMMIT_FILE
220
221# Get the current description... there must be an easier way.
222git log -n 1 | grep '^ ' | sed 's/^ //' >> $COMMIT_FILE
223
224# Get the commit sha for the last benchmark commit
225PREV=$(git log -n 1 --grep='BENCHMARK_MARKER_DO_NOT_CHANGE' | head -n 1 | awk '{print $2}')
226
227## Run current benchmarks
228
229cat >> $COMMIT_FILE <<EOF
230
231
232----------------------------------------------------------
233BENCHMARK_MARKER_DO_NOT_CHANGE
234----------------------------------------------------------
235
236Go version $(go version)
237
238
239TEST BENCHMARKS "$BENCH"
240EOF
241# go seems to have trouble with 'go test --bench=. ./...'
242go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
243pushd layers
244go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
245popd
246cat >> $COMMIT_FILE <<EOF
247
248
249PCAP BENCHMARK
250EOF
251if [ "$BENCH" -eq ".*" ]; then
252 go run pcap/gopacket_benchmark/*.go 2>&1 | tee -a $COMMIT_FILE
253fi
254
255
256
257## Reset to last benchmark commit, run benchmarks
258
259git checkout $PREV
260
261cat >> $COMMIT_FILE <<EOF
262----------------------------------------------------------
263BENCHMARKING AGAINST COMMIT $PREV
264----------------------------------------------------------
265
266
267OLD TEST BENCHMARKS
268EOF
269# go seems to have trouble with 'go test --bench=. ./...'
270go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
271pushd layers
272go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
273popd
274cat >> $COMMIT_FILE <<EOF
275
276
277OLD PCAP BENCHMARK
278EOF
279if [ "$BENCH" -eq ".*" ]; then
280 go run pcap/gopacket_benchmark/*.go 2>&1 | tee -a $COMMIT_FILE
281fi
282
283
284
285## Reset back to the most recent commit, edit the commit message by appending
286## benchmark results.
287git checkout $BRANCH
288git commit --amend -F $COMMIT_FILE