github.com/gopacket/gopacket@v1.1.0/gc (about)

     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    go run gen2.go | gofmt > enums_generated.go
   110    popd
   111  fi
   112  
   113  # Make sure everything is formatted, compiles, and tests pass.
   114  go fmt ./...
   115  go test -i ./... 2>/dev/null >/dev/null || true
   116  go test
   117  go build
   118  pushd examples/bytediff
   119  go build
   120  popd
   121  if [ -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
   151  fi
   152  pushd macs
   153  go test ./...
   154  gofmt -w gen.go
   155  go build gen.go
   156  popd
   157  pushd tcpassembly
   158  go test ./...
   159  popd
   160  pushd reassembly
   161  go test ./...
   162  popd
   163  pushd layers
   164  gofmt -w gen.go
   165  go build gen.go
   166  go test ./...
   167  popd
   168  pushd pcapgo
   169  go test ./...
   170  go build ./...
   171  popd
   172  if [ -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
   179  fi
   180  if [ -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
   188  fi
   189  pushd ip4defrag
   190  go test ./...
   191  popd
   192  pushd defrag
   193  go test ./...
   194  popd
   195  
   196  for travis_script in `ls .travis.*.sh`; do
   197    ./$travis_script
   198  done
   199  
   200  # Run our initial commit
   201  git commit "$@"
   202  
   203  if [ -z "$BENCH" ]; then
   204    set +x
   205    echo "We're not benchmarking and we've committed... we're done!"
   206    exit
   207  fi
   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.
   213  BRANCH="$(git branch | grep '^*' | awk '{print $2}')"
   214  
   215  # File we're going to build our commit description in.
   216  COMMIT_FILE="$(mktemp /tmp/tmp.XXXXXXXX)"
   217  
   218  # Add the word "BENCH" to the start of the git commit.
   219  echo -n "BENCH " > $COMMIT_FILE
   220  
   221  # Get the current description... there must be an easier way.
   222  git log -n 1 | grep '^ ' | sed 's/^    //' >> $COMMIT_FILE
   223  
   224  # Get the commit sha for the last benchmark commit
   225  PREV=$(git log -n 1 --grep='BENCHMARK_MARKER_DO_NOT_CHANGE' | head -n 1 | awk '{print $2}')
   226  
   227  ## Run current benchmarks
   228  
   229  cat >> $COMMIT_FILE <<EOF
   230  
   231  
   232  ----------------------------------------------------------
   233  BENCHMARK_MARKER_DO_NOT_CHANGE
   234  ----------------------------------------------------------
   235  
   236  Go version $(go version)
   237  
   238  
   239  TEST BENCHMARKS "$BENCH"
   240  EOF
   241  # go seems to have trouble with 'go test --bench=. ./...'
   242  go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
   243  pushd layers
   244  go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
   245  popd
   246  cat >> $COMMIT_FILE <<EOF
   247  
   248  
   249  PCAP BENCHMARK
   250  EOF
   251  if [ "$BENCH" -eq ".*" ]; then
   252    go run pcap/gopacket_benchmark/*.go 2>&1 | tee -a $COMMIT_FILE
   253  fi
   254  
   255  
   256  
   257  ## Reset to last benchmark commit, run benchmarks
   258  
   259  git checkout $PREV
   260  
   261  cat >> $COMMIT_FILE <<EOF
   262  ----------------------------------------------------------
   263  BENCHMARKING AGAINST COMMIT $PREV
   264  ----------------------------------------------------------
   265  
   266  
   267  OLD TEST BENCHMARKS
   268  EOF
   269  # go seems to have trouble with 'go test --bench=. ./...'
   270  go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
   271  pushd layers
   272  go test --test.bench="$BENCH" 2>&1 | tee -a $COMMIT_FILE
   273  popd
   274  cat >> $COMMIT_FILE <<EOF
   275  
   276  
   277  OLD PCAP BENCHMARK
   278  EOF
   279  if [ "$BENCH" -eq ".*" ]; then
   280    go run pcap/gopacket_benchmark/*.go 2>&1 | tee -a $COMMIT_FILE
   281  fi
   282  
   283  
   284  
   285  ## Reset back to the most recent commit, edit the commit message by appending
   286  ## benchmark results.
   287  git checkout $BRANCH
   288  git commit --amend -F $COMMIT_FILE