gc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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" "$@"
  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/pcapdump
  118. go build
  119. popd
  120. pushd examples/arpscan
  121. go build
  122. popd
  123. pushd examples/bidirectional
  124. go build
  125. popd
  126. pushd examples/synscan
  127. go build
  128. popd
  129. pushd examples/httpassembly
  130. go build
  131. popd
  132. pushd examples/statsassembly
  133. go build
  134. popd
  135. fi
  136. pushd macs
  137. go test ./...
  138. gofmt -w gen.go
  139. go build gen.go
  140. popd
  141. pushd tcpassembly
  142. go test ./...
  143. popd
  144. pushd reassembly
  145. go test ./...
  146. popd
  147. pushd layers
  148. gofmt -w gen.go
  149. go build gen.go
  150. go test ./...
  151. popd
  152. pushd pcapgo
  153. go test ./...
  154. go build ./...
  155. popd
  156. if [ -f /usr/include/linux/if_packet.h ]; then
  157. if grep -q TPACKET_V3 /usr/include/linux/if_packet.h; then
  158. pushd afpacket
  159. go build ./...
  160. go test ./...
  161. popd
  162. fi
  163. fi
  164. if [ -f /usr/include/pfring.h ]; then
  165. pushd pfring
  166. go test ./...
  167. go build ./...
  168. popd
  169. pushd examples/pfdump
  170. go build
  171. popd
  172. fi
  173. for travis_script in `ls .travis.*.sh`; do
  174. ./$travis_script
  175. done
  176. # Run our initial commit
  177. git commit "$@"
  178. if [ -z "$BENCH" ]; then
  179. set +x
  180. echo "We're not benchmarking and we've committed... we're done!"
  181. exit
  182. fi
  183. ### If we get here, we want to run benchmarks from current commit, and compare
  184. ### then to benchmarks from the last --benchmark commit.
  185. # Get our current branch.
  186. BRANCH="$(git branch | grep '^*' | awk '{print $2}')"
  187. # File we're going to build our commit description in.
  188. COMMIT_FILE="$(mktemp /tmp/tmp.XXXXXXXX)"
  189. # Add the word "BENCH" to the start of the git commit.
  190. echo -n "BENCH " > $COMMIT_FILE
  191. # Get the current description... there must be an easier way.
  192. git log -n 1 | grep '^ ' | sed 's/^ //' >> $COMMIT_FILE
  193. # Get the commit sha for the last benchmark commit
  194. PREV=$(git log -n 1 --grep='BENCHMARK_MARKER_DO_NOT_CHANGE' | head -n 1 | awk '{print $2}')
  195. ## Run current benchmarks
  196. cat >> $COMMIT_FILE <<EOF
  197. ----------------------------------------------------------
  198. BENCHMARK_MARKER_DO_NOT_CHANGE
  199. ----------------------------------------------------------
  200. Go version $(go version)
  201. TEST BENCHMARKS "$BENCH"
  202. EOF
  203. # go seems to have trouble with 'go test --bench=. ./...'
  204. go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
  205. pushd layers
  206. go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
  207. popd
  208. cat >> $COMMIT_FILE <<EOF
  209. PCAP BENCHMARK
  210. EOF
  211. if [ "$BENCH" -eq ".*" ]; then
  212. go run pcap/gopacket_benchmark/*.go 2>&1 | tee -a $COMMIT_FILE
  213. fi
  214. ## Reset to last benchmark commit, run benchmarks
  215. git checkout $PREV
  216. cat >> $COMMIT_FILE <<EOF
  217. ----------------------------------------------------------
  218. BENCHMARKING AGAINST COMMIT $PREV
  219. ----------------------------------------------------------
  220. OLD TEST BENCHMARKS
  221. EOF
  222. # go seems to have trouble with 'go test --bench=. ./...'
  223. go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
  224. pushd layers
  225. go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
  226. popd
  227. cat >> $COMMIT_FILE <<EOF
  228. OLD PCAP BENCHMARK
  229. EOF
  230. if [ "$BENCH" -eq ".*" ]; then
  231. go run pcap/gopacket_benchmark/*.go 2>&1 | tee -a $COMMIT_FILE
  232. fi
  233. ## Reset back to the most recent commit, edit the commit message by appending
  234. ## benchmark results.
  235. git checkout $BRANCH
  236. git commit --amend -F $COMMIT_FILE