gc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #!/bin/bash
  2. # Copyright 2012 Google, Inc. All rights reserved.
  3. # This script provides a simple way to run benchmarks against previous code and
  4. # keep a log of how benchmarks change over time. When used with the --benchmark
  5. # flag, it runs benchmarks from the current code and from the last commit run
  6. # with --benchmark, then stores the results in the git commit description. We
  7. # rerun the old benchmarks along with the new ones, since there's no guarantee
  8. # that git commits will happen on the same machine, so machine differences could
  9. # cause wildly inaccurate results.
  10. #
  11. # If you're making changes to 'gopacket' which could cause performance changes,
  12. # you may be requested to use this commit script to make sure your changes don't
  13. # have large detrimental effects (or to show off how awesome your performance
  14. # improvements are).
  15. #
  16. # If not run with the --benchmark flag, this script is still very useful... it
  17. # makes sure all the correct go formatting, building, and testing work as
  18. # expected.
  19. function Usage {
  20. cat <<EOF
  21. USAGE: $0 [--benchmark regexp] [--root] [--gen] <git commit flags...>
  22. --benchmark: Run benchmark comparisons against last benchmark'd commit
  23. --root: Run tests that require root priviledges
  24. --gen: Generate code for MACs/ports by pulling down external data
  25. Note, some 'git commit' flags are necessary, if all else fails, pass in -a
  26. EOF
  27. exit 1
  28. }
  29. BENCH=""
  30. GEN=""
  31. ROOT=""
  32. while [ ! -z "$1" ]; do
  33. case "$1" in
  34. "--benchmark")
  35. BENCH="$2"
  36. shift
  37. shift
  38. ;;
  39. "--gen")
  40. GEN="yes"
  41. shift
  42. ;;
  43. "--root")
  44. ROOT="yes"
  45. shift
  46. ;;
  47. "--help")
  48. Usage
  49. ;;
  50. "-h")
  51. Usage
  52. ;;
  53. "help")
  54. Usage
  55. ;;
  56. *)
  57. break
  58. ;;
  59. esac
  60. done
  61. function Root {
  62. if [ ! -z "$ROOT" ]; then
  63. local exec="$1"
  64. # Some folks (like me) keep source code in places inaccessible by root (like
  65. # NFS), so to make sure things run smoothly we copy them to a /tmp location.
  66. local tmpfile="$(mktemp -t gopacket_XXXXXXXX)"
  67. echo "Running root test executable $exec as $tmpfile"
  68. cp "$exec" "$tmpfile"
  69. chmod a+x "$tmpfile"
  70. shift
  71. sudo "$tmpfile" "[email protected]"
  72. fi
  73. }
  74. if [ "$#" -eq "0" ]; then
  75. Usage
  76. fi
  77. cd $(dirname $0)
  78. # Check for copyright notices.
  79. for filename in $(find ./ -type f -name '*.go'); do
  80. if ! head -n 1 "$filename" | grep -q Copyright; then
  81. echo "File '$filename' may not have copyright notice"
  82. exit 1
  83. fi
  84. done
  85. set -e
  86. set -x
  87. if [ ! -z "$ROOT" ]; then
  88. echo "Running SUDO to get root priviledges for root tests"
  89. sudo echo "have root"
  90. fi
  91. if [ ! -z "$GEN" ]; then
  92. pushd macs
  93. go run gen.go | gofmt > valid_mac_prefixes.go
  94. popd
  95. pushd layers
  96. go run gen.go | gofmt > iana_ports.go
  97. go run gen2.go | gofmt > enums_generated.go
  98. popd
  99. fi
  100. # Make sure everything is formatted, compiles, and tests pass.
  101. go fmt ./...
  102. go test -i ./... 2>/dev/null >/dev/null || true
  103. go test
  104. go build
  105. pushd examples/bytediff
  106. go build
  107. popd
  108. if [ -f /usr/include/pcap.h ]; then
  109. pushd pcap
  110. go test ./...
  111. go build ./...
  112. go build pcap_tester.go
  113. Root pcap_tester --mode=basic
  114. Root pcap_tester --mode=filtered
  115. Root pcap_tester --mode=timestamp || echo "You might not support timestamp sources"
  116. popd
  117. pushd examples/afpacket
  118. go build
  119. popd
  120. pushd examples/pcapdump
  121. go build
  122. popd
  123. pushd examples/arpscan
  124. go build
  125. popd
  126. pushd examples/bidirectional
  127. go build
  128. popd
  129. pushd examples/synscan
  130. go build
  131. popd
  132. pushd examples/httpassembly
  133. go build
  134. popd
  135. pushd examples/statsassembly
  136. go build
  137. popd
  138. fi
  139. pushd macs
  140. go test ./...
  141. gofmt -w gen.go
  142. go build gen.go
  143. popd
  144. pushd tcpassembly
  145. go test ./...
  146. popd
  147. pushd reassembly
  148. go test ./...
  149. popd
  150. pushd layers
  151. gofmt -w gen.go
  152. go build gen.go
  153. go test ./...
  154. popd
  155. pushd pcapgo
  156. go test ./...
  157. go build ./...
  158. popd
  159. if [ -f /usr/include/linux/if_packet.h ]; then
  160. if grep -q TPACKET_V3 /usr/include/linux/if_packet.h; then
  161. pushd afpacket
  162. go build ./...
  163. go test ./...
  164. popd
  165. fi
  166. fi
  167. if [ -f /usr/include/pfring.h ]; then
  168. pushd pfring
  169. go test ./...
  170. go build ./...
  171. popd
  172. pushd examples/pfdump
  173. go build
  174. popd
  175. fi
  176. pushd ip4defrag
  177. go test ./...
  178. popd
  179. pushd defrag
  180. go test ./...
  181. popd
  182. for travis_script in `ls .travis.*.sh`; do
  183. ./$travis_script
  184. done
  185. # Run our initial commit
  186. git commit "[email protected]"
  187. if [ -z "$BENCH" ]; then
  188. set +x
  189. echo "We're not benchmarking and we've committed... we're done!"
  190. exit
  191. fi
  192. ### If we get here, we want to run benchmarks from current commit, and compare
  193. ### then to benchmarks from the last --benchmark commit.
  194. # Get our current branch.
  195. BRANCH="$(git branch | grep '^*' | awk '{print $2}')"
  196. # File we're going to build our commit description in.
  197. COMMIT_FILE="$(mktemp /tmp/tmp.XXXXXXXX)"
  198. # Add the word "BENCH" to the start of the git commit.
  199. echo -n "BENCH " > $COMMIT_FILE
  200. # Get the current description... there must be an easier way.
  201. git log -n 1 | grep '^ ' | sed 's/^ //' >> $COMMIT_FILE
  202. # Get the commit sha for the last benchmark commit
  203. PREV=$(git log -n 1 --grep='BENCHMARK_MARKER_DO_NOT_CHANGE' | head -n 1 | awk '{print $2}')
  204. ## Run current benchmarks
  205. cat >> $COMMIT_FILE <<EOF
  206. ----------------------------------------------------------
  207. BENCHMARK_MARKER_DO_NOT_CHANGE
  208. ----------------------------------------------------------
  209. Go version $(go version)
  210. TEST BENCHMARKS "$BENCH"
  211. EOF
  212. # go seems to have trouble with 'go test --bench=. ./...'
  213. go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
  214. pushd layers
  215. go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
  216. popd
  217. cat >> $COMMIT_FILE <<EOF
  218. PCAP BENCHMARK
  219. EOF
  220. if [ "$BENCH" -eq ".*" ]; then
  221. go run pcap/gopacket_benchmark/*.go 2>&1 | tee -a $COMMIT_FILE
  222. fi
  223. ## Reset to last benchmark commit, run benchmarks
  224. git checkout $PREV
  225. cat >> $COMMIT_FILE <<EOF
  226. ----------------------------------------------------------
  227. BENCHMARKING AGAINST COMMIT $PREV
  228. ----------------------------------------------------------
  229. OLD TEST BENCHMARKS
  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. OLD PCAP BENCHMARK
  238. EOF
  239. if [ "$BENCH" -eq ".*" ]; then
  240. go run pcap/gopacket_benchmark/*.go 2>&1 | tee -a $COMMIT_FILE
  241. fi
  242. ## Reset back to the most recent commit, edit the commit message by appending
  243. ## benchmark results.
  244. git checkout $BRANCH
  245. git commit --amend -F $COMMIT_FILE