Fix unit tests
[govpp.git] / vendor / github.com / google / gopacket / gc
1 #!/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
21 function Usage {
22   cat <<EOF
23 USAGE:  $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
29 Note, some 'git commit' flags are necessary, if all else fails, pass in -a
30 EOF
31   exit 1
32 }
33
34 BENCH=""
35 GEN=""
36 ROOT=""
37 while [ ! -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
65 done
66
67 function 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
81 if [ "$#" -eq "0" ]; then
82   Usage
83 fi
84
85 cd $(dirname $0)
86
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"
91     exit 1
92   fi
93 done
94
95 set -e
96 set -x
97
98 if [ ! -z "$ROOT" ]; then
99   echo "Running SUDO to get root priviledges for root tests"
100   sudo echo "have root"
101 fi
102
103 if [ ! -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   popd
110 fi
111
112 # Make sure everything is formatted, compiles, and tests pass.
113 go fmt ./...
114 go test -i ./... 2>/dev/null >/dev/null || true
115 go test
116 go build
117 pushd examples/bytediff
118 go build
119 popd
120 if [ -f /usr/include/pcap.h ]; then
121   pushd pcap
122   go test ./...
123   go build ./...
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"
128   popd
129   pushd examples/pcapdump
130   go build
131   popd
132   pushd examples/arpscan
133   go build
134   popd
135   pushd examples/bidirectional
136   go build
137   popd
138   pushd examples/synscan
139   go build
140   popd
141   pushd examples/httpassembly
142   go build
143   popd
144   pushd examples/statsassembly
145   go build
146   popd
147 fi
148 pushd macs
149 go test ./...
150 gofmt -w gen.go
151 go build gen.go
152 popd
153 pushd tcpassembly
154 go test ./...
155 popd
156 pushd reassembly
157 go test ./...
158 popd
159 pushd layers
160 gofmt -w gen.go
161 go build gen.go
162 go test ./...
163 popd
164 pushd pcapgo
165 go test ./...
166 go build ./...
167 popd
168 if [ -f /usr/include/linux/if_packet.h ]; then
169   if grep -q TPACKET_V3 /usr/include/linux/if_packet.h; then
170     pushd afpacket
171     go build ./...
172     go test ./...
173     popd
174   fi
175 fi
176 if [ -f /usr/include/pfring.h ]; then
177   pushd pfring
178   go test ./...
179   go build ./...
180   popd
181   pushd examples/pfdump
182   go build
183   popd
184 fi
185
186 for travis_script in `ls .travis.*.sh`; do
187   ./$travis_script
188 done
189
190 # Run our initial commit
191 git commit "$@"
192
193 if [ -z "$BENCH" ]; then
194   set +x
195   echo "We're not benchmarking and we've committed... we're done!"
196   exit
197 fi
198
199 ### If we get here, we want to run benchmarks from current commit, and compare
200 ### then to benchmarks from the last --benchmark commit.
201
202 # Get our current branch.
203 BRANCH="$(git branch | grep '^*' | awk '{print $2}')"
204
205 # File we're going to build our commit description in.
206 COMMIT_FILE="$(mktemp /tmp/tmp.XXXXXXXX)"
207
208 # Add the word "BENCH" to the start of the git commit.
209 echo -n "BENCH " > $COMMIT_FILE
210
211 # Get the current description... there must be an easier way.
212 git log -n 1 | grep '^ ' | sed 's/^    //' >> $COMMIT_FILE
213
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}')
216
217 ## Run current benchmarks
218
219 cat >> $COMMIT_FILE <<EOF
220
221
222 ----------------------------------------------------------
223 BENCHMARK_MARKER_DO_NOT_CHANGE
224 ----------------------------------------------------------
225
226 Go version $(go version)
227
228
229 TEST BENCHMARKS "$BENCH"
230 EOF
231 # go seems to have trouble with 'go test --bench=. ./...'
232 go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
233 pushd layers
234 go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
235 popd
236 cat >> $COMMIT_FILE <<EOF
237
238
239 PCAP BENCHMARK
240 EOF
241 if [ "$BENCH" -eq ".*" ]; then
242   go run pcap/gopacket_benchmark/*.go 2>&1 | tee -a $COMMIT_FILE
243 fi
244
245
246
247 ## Reset to last benchmark commit, run benchmarks
248
249 git checkout $PREV
250
251 cat >> $COMMIT_FILE <<EOF
252 ----------------------------------------------------------
253 BENCHMARKING AGAINST COMMIT $PREV
254 ----------------------------------------------------------
255
256
257 OLD TEST BENCHMARKS
258 EOF
259 # go seems to have trouble with 'go test --bench=. ./...'
260 go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
261 pushd layers
262 go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
263 popd
264 cat >> $COMMIT_FILE <<EOF
265
266
267 OLD PCAP BENCHMARK
268 EOF
269 if [ "$BENCH" -eq ".*" ]; then
270   go run pcap/gopacket_benchmark/*.go 2>&1 | tee -a $COMMIT_FILE
271 fi
272
273
274
275 ## Reset back to the most recent commit, edit the commit message by appending
276 ## benchmark results.
277 git checkout $BRANCH
278 git commit --amend -F $COMMIT_FILE