| 1 | #!/bin/bash |
| 2 | |
| 3 | # download the code and all its dependencies |
| 4 | _go_get() { |
| 5 | # Note: keep "github.com/ugorji/go/codec" in quotes, as script pushing to github will replace it appropriately |
| 6 | ${go[@]} get -u \ |
| 7 | "github.com/ugorji/go/codec" "github.com/ugorji/go/codec"/codecgen \ |
| 8 | github.com/tinylib/msgp/msgp github.com/tinylib/msgp \ |
| 9 | github.com/pquerna/ffjson/ffjson github.com/pquerna/ffjson \ |
| 10 | github.com/Sereal/Sereal/Go/sereal \ |
| 11 | bitbucket.org/bodhisnarkva/cbor/go \ |
| 12 | github.com/davecgh/go-xdr/xdr2 \ |
| 13 | gopkg.in/mgo.v2/bson \ |
| 14 | gopkg.in/vmihailenco/msgpack.v2 \ |
| 15 | github.com/json-iterator/go \ |
| 16 | go.mongodb.org/mongo-driver/bson \ |
| 17 | github.com/mailru/easyjson/... |
| 18 | } |
| 19 | |
| 20 | # add generated tag to the top of each file |
| 21 | _prependbt() { |
| 22 | local tag="${3:-generated}" |
| 23 | cat > ${2} <<EOF |
| 24 | // +build ${tag} |
| 25 | |
| 26 | EOF |
| 27 | cat ${1} >> ${2} |
| 28 | rm -f ${1} |
| 29 | } |
| 30 | |
| 31 | _sed_in_file() { |
| 32 | case "$(uname -s)" in |
| 33 | Darwin*) sed -i '' "$@";; |
| 34 | *) sed -i "$@";; |
| 35 | esac |
| 36 | } |
| 37 | |
| 38 | # To run the full suite of benchmarks, including executing against the external frameworks |
| 39 | # listed above, you MUST first run code generation for the frameworks that support it. |
| 40 | # |
| 41 | # If you want to run the benchmarks against code generated values. |
| 42 | # Then first generate the code generated values from values_test.go named typed. |
| 43 | # we cannot normally read a _test.go file, so temporarily copy it into a readable file. |
| 44 | _gen() { |
| 45 | local zsfx="_generated_test.go" |
| 46 | # local z=`pwd` |
| 47 | # z=${z%%/src/*} |
| 48 | # Note: ensure you run the codecgen for this codebase |
| 49 | # NOTE: MARKER: ffjson has been generating bad uncompilable code, so ignore it for now |
| 50 | cp values_test.go v.go && |
| 51 | echo "codecgen ..." && |
| 52 | codecgen -nx -ta=false -rt codecgen -t 'codecgen generated' -o values_codecgen${zsfx} -d 19780 v.go && |
| 53 | echo "msgp ... " && |
| 54 | msgp -unexported -tests=false -o=m9.go -file=v.go && |
| 55 | _prependbt m9.go values_msgp${zsfx} && |
| 56 | echo "easyjson ... " && |
| 57 | easyjson -all -no_std_marshalers -omit_empty -output_filename e9.go v.go && |
| 58 | _prependbt e9.go values_easyjson${zsfx} && |
| 59 | echo "ffjson ... " && |
| 60 | ffjson -force-regenerate -reset-fields -w f9.go v.go && |
| 61 | _prependbt f9.go values_ffjson${zsfx} ignore && |
| 62 | _sed_in_file -e 's+ MarshalJSON(+ _MarshalJSON(+g' values_ffjson${zsfx} && |
| 63 | _sed_in_file -e 's+ UnmarshalJSON(+ _UnmarshalJSON(+g' values_ffjson${zsfx} && |
| 64 | rm -f easyjson-bootstrap*.go ffjson-inception* && |
| 65 | rm -f v.go && |
| 66 | echo "... DONE" |
| 67 | } |
| 68 | |
| 69 | # run the full suite of tests |
| 70 | # |
| 71 | # Basically, its a sequence of |
| 72 | # ${go[@]} test -tags "alltests x codec.safe codecgen generated" -bench "CodecSuite or AllSuite or XSuite" -benchmem |
| 73 | # |
| 74 | |
| 75 | _suite_tests() { |
| 76 | if [[ "${do_x}" = "1" ]]; then |
| 77 | printf "\n==== X Baseline ====\n" |
| 78 | ${go[@]} test "${zargs[@]}" -tags x -v "$@" |
| 79 | else |
| 80 | printf "\n==== Baseline ====\n" |
| 81 | ${go[@]} test "${zargs[@]}" -v "$@" |
| 82 | fi |
| 83 | if [[ "${do_x}" = "1" ]]; then |
| 84 | printf "\n==== X Generated ====\n" |
| 85 | ${go[@]} test "${zargs[@]}" -tags "x generated" -v "$@" |
| 86 | else |
| 87 | printf "\n==== Generated ====\n" |
| 88 | ${go[@]} test "${zargs[@]}" -tags "generated" -v "$@" |
| 89 | fi |
| 90 | } |
| 91 | |
| 92 | _suite_tests_strip_file_line() { |
| 93 | # sed -e 's/^\([^a-zA-Z0-9]\+\)[a-zA-Z0-9_]\+\.go:[0-9]\+:/\1/' |
| 94 | sed -e 's/[a-zA-Z0-9_]*.go:[0-9]*://g' |
| 95 | } |
| 96 | |
| 97 | _suite_any() { |
| 98 | local x="$1" |
| 99 | local g="$2" |
| 100 | local b="$3" |
| 101 | shift 3 |
| 102 | local a=( "" "codec.safe" "codec.notfastpath" "codec.notfastpath codec.safe" "codecgen" "codecgen codec.safe") |
| 103 | if [[ "$g" = "g" ]]; then a=( "generated" "generated codec.safe"); fi |
| 104 | for i in "${a[@]}"; do |
| 105 | echo ">>>> bench TAGS: 'alltests $x $i' SUITE: $b" |
| 106 | ${go[@]} test "${zargs[@]}" -tags "alltests $x $i" -bench "$b" -benchmem "$@" |
| 107 | done |
| 108 | } |
| 109 | |
| 110 | # _suite() { |
| 111 | # local t="alltests x" |
| 112 | # local a=( "" "codec.safe" "codec.notfastpath" "codec.notfastpath codec.safe" "codecgen" "codecgen codec.safe") |
| 113 | # for i in "${a[@]}" |
| 114 | # do |
| 115 | # echo ">>>> bench TAGS: '$t $i' SUITE: BenchmarkCodecXSuite" |
| 116 | # ${go[@]} test "${zargs[@]}" -tags "$t $i" -bench BenchmarkCodecXSuite -benchmem "$@" |
| 117 | # done |
| 118 | # } |
| 119 | |
| 120 | # _suite_gen() { |
| 121 | # local t="alltests x" |
| 122 | # local b=( "generated" "generated codec.safe") |
| 123 | # for i in "${b[@]}" |
| 124 | # do |
| 125 | # echo ">>>> bench TAGS: '$t $i' SUITE: BenchmarkCodecXGenSuite" |
| 126 | # ${go[@]} test "${zargs[@]}" -tags "$t $i" -bench BenchmarkCodecXGenSuite -benchmem "$@" |
| 127 | # done |
| 128 | # } |
| 129 | |
| 130 | # _suite_json() { |
| 131 | # local t="alltests x" |
| 132 | # local a=( "" "codec.safe" "codec.notfastpath" "codec.notfastpath codec.safe" "codecgen" "codecgen codec.safe") |
| 133 | # for i in "${a[@]}" |
| 134 | # do |
| 135 | # echo ">>>> bench TAGS: '$t $i' SUITE: BenchmarkCodecQuickAllJsonSuite" |
| 136 | # ${go[@]} test "${zargs[@]}" -tags "$t $i" -bench BenchmarkCodecQuickAllJsonSuite -benchmem "$@" |
| 137 | # done |
| 138 | # } |
| 139 | |
| 140 | # _suite_very_quick_json() { |
| 141 | # # Quickly get numbers for json, stdjson, jsoniter and json (codecgen)" |
| 142 | # echo ">>>> very quick json bench" |
| 143 | # ${go[@]} test "${zargs[@]}" -tags "alltests x" -bench "__(Json|Std_Json|JsonIter)__" -benchmem "$@" |
| 144 | # echo |
| 145 | # ${go[@]} test "${zargs[@]}" -tags "alltests codecgen" -bench "__Json____" -benchmem "$@" |
| 146 | # } |
| 147 | |
| 148 | # _suite_very_quick_json_via_suite() { |
| 149 | # # Quickly get numbers for json, stdjson, jsoniter and json (codecgen)" |
| 150 | # echo ">>>> very quick json bench" |
| 151 | # local prefix="BenchmarkCodecVeryQuickAllJsonSuite/json-all-bd1......../" |
| 152 | # ${go[@]} test "${zargs[@]}" -tags "alltests x" -bench BenchmarkCodecVeryQuickAllJsonSuite -benchmem "$@" | |
| 153 | # sed -e "s+^$prefix++" |
| 154 | # echo "---- CODECGEN RESULTS ----" |
| 155 | # ${go[@]} test "${zargs[@]}" -tags "x generated" -bench "__(Json|Easyjson)__" -benchmem "$@" |
| 156 | # } |
| 157 | |
| 158 | _suite_very_quick_json_non_suite() { |
| 159 | # Quickly get numbers for json, stdjson, jsoniter and json (codecgen)" |
| 160 | local t="${1:-x}" |
| 161 | shift |
| 162 | echo ">>>> very quick json bench" |
| 163 | # local tags=( "x" "x generated" ) |
| 164 | local tags=("${t}" "${t} generated" "${t} codec.safe" "${t} generated codec.safe" "${t} codec.notfastpath") |
| 165 | local js=( En De ) |
| 166 | for t in "${tags[@]}"; do |
| 167 | echo "---- tags: ${t} ----" |
| 168 | local b="Json" |
| 169 | if [[ "${t}" =~ x && ! "${t}" =~ safe && ! "${t}" =~ notfastpath ]]; then |
| 170 | b="Json|Std_Json|JsonIter" |
| 171 | if [[ "${t}" =~ generated ]]; then b="Json|Easyjson"; fi |
| 172 | fi |
| 173 | for j in "${js[@]}"; do |
| 174 | ${go[@]} test "${zargs[@]}" -tags "${t}" -bench "__(${b})__.*${j}" -benchmem "$@" |
| 175 | [[ "${b}" != Json ]] && echo # echo if more than 1 line is printed |
| 176 | done |
| 177 | done |
| 178 | } |
| 179 | |
| 180 | _suite_very_quick_benchmark() { |
| 181 | local a="${1:-Json}" # Json Cbor Msgpack Simple |
| 182 | # case "$1" in |
| 183 | # Json|Cbor|Msgpack|Simple|Binc) a="${1}"; shift ;; |
| 184 | # esac |
| 185 | local b="${2}" |
| 186 | local c="${3}" |
| 187 | local t="${4:-4s}" # 4s 1x |
| 188 | shift 4 |
| 189 | ${go[@]} test "${zargs[@]}" -tags "alltests ${c}" -bench "__${a}__.*${b}" -benchmem -benchtime "${t}" "$@" |
| 190 | } |
| 191 | |
| 192 | _suite_trim_output() { |
| 193 | grep -v -E "^(goos:|goarch:|pkg:|cpu:|PASS|ok|=== RUN|--- PASS)" |
| 194 | } |
| 195 | |
| 196 | _bench_dot_out_dot_txt() { |
| 197 | printf "**** STATS ****\n\n" |
| 198 | ./bench.sh -tx # (stats) |
| 199 | printf "**** SUITE ****\n\n" |
| 200 | ./bench.sh -sx # (not codecgen) |
| 201 | printf "**** SUITE (WITH CODECGEN) ****\n\n" |
| 202 | ./bench.sh -sgx # (codecgen) |
| 203 | } |
| 204 | |
| 205 | _suite_debugging() { |
| 206 | _suite_very_quick_benchmark "$@" |
| 207 | |
| 208 | # local cg="n" # n = no codecgen, c = codecgen; options are c, n, cn |
| 209 | # local js=( De ) # or: ( _ ) ( En ) ( De ) ( En De ) |
| 210 | # for j in ${js[@]}; do |
| 211 | # if [[ ${cg} =~ "c" ]]; then |
| 212 | # echo "---- codecgen ----" |
| 213 | # ${go[@]} test "${zargs[@]}" -tags "generated" -bench "__(Json)__.*${j}" -benchtime 2s -benchmem "$@" |
| 214 | # fi |
| 215 | # if [[ ${cg} =~ "n" ]]; then |
| 216 | # echo "---- no codecgen ----" |
| 217 | # ${go[@]} test "${zargs[@]}" -tags "" -bench "__(Json)__.*${j}" -benchtime 2s -benchmem "$@" |
| 218 | # fi |
| 219 | # echo |
| 220 | # done |
| 221 | } |
| 222 | |
| 223 | _usage() { |
| 224 | printf "usage: bench.sh -[dcbsgjqpz] for \n" |
| 225 | printf "\t-d download\n" |
| 226 | printf "\t-c code-generate\n" |
| 227 | printf "\t-tx tests (show stats for each format and whether encoded == decoded); if x, do external also\n" |
| 228 | printf "\t-sgx run test suite for codec; if g, use generated files; if x, do external also\n" |
| 229 | printf "\t-jq run test suite for [json, json-quick]\n" |
| 230 | printf "\t-p run test suite with profiles: defaults to json: [format/prefix] [suffix] [tags] [benchtime]\n" |
| 231 | printf "\t-z run tests for bench.out.txt\n" |
| 232 | printf "\t-f [pprof file] run pprof\n" |
| 233 | printf "\t-y run debugging suite (during development only): [format/prefix] [suffix] [tags] [benchtime]\n" |
| 234 | } |
| 235 | |
| 236 | _main() { |
| 237 | if [[ "$1" == "" || "$1" == "-h" || "$1" == "-?" ]]; then |
| 238 | _usage |
| 239 | return 1 |
| 240 | fi |
| 241 | |
| 242 | # export GODEBUG=asyncpreemptoff=1 # TODO remove |
| 243 | |
| 244 | local go=( "${MYGOCMD:-go}" ) |
| 245 | local zargs=("-count" "1" "-tr") |
| 246 | local args=() |
| 247 | local do_x="0" |
| 248 | local do_g="0" |
| 249 | while getopts "dcbsjqptxklgzfy" flag |
| 250 | do |
| 251 | case "$flag" in |
| 252 | d|c|b|s|j|q|p|t|x|k|l|g|z|f|y) args+=( "$flag" ) ;; |
| 253 | *) _usage; return 1 ;; |
| 254 | esac |
| 255 | done |
| 256 | shift "$((OPTIND-1))" |
| 257 | |
| 258 | [[ " ${args[*]} " == *"x"* ]] && do_x="1" |
| 259 | [[ " ${args[*]} " == *"g"* ]] && do_g="1" |
| 260 | [[ " ${args[*]} " == *"k"* ]] && zargs+=("-gcflags" "all=-B") |
| 261 | [[ " ${args[*]} " == *"l"* ]] && zargs+=("-gcflags" "all=-l=4") |
| 262 | [[ " ${args[*]} " == *"d"* ]] && _go_get "$@" |
| 263 | [[ " ${args[*]} " == *"c"* ]] && _gen "$@" |
| 264 | |
| 265 | [[ " ${args[*]} " == *"s"* && "${do_x}" == 0 && "${do_g}" == 0 ]] && _suite_any - - BenchmarkCodecSuite "$@" | _suite_trim_output |
| 266 | [[ " ${args[*]} " == *"s"* && "${do_x}" == 0 && "${do_g}" == 1 ]] && _suite_any - g BenchmarkCodecSuite "$@" | _suite_trim_output |
| 267 | [[ " ${args[*]} " == *"s"* && "${do_x}" == 1 && "${do_g}" == 0 ]] && _suite_any x - BenchmarkCodecXSuite "$@" | _suite_trim_output |
| 268 | [[ " ${args[*]} " == *"s"* && "${do_x}" == 1 && "${do_g}" == 1 ]] && _suite_any x g BenchmarkCodecXGenSuite "$@" | _suite_trim_output |
| 269 | |
| 270 | [[ " ${args[*]} " == *"j"* ]] && _suite_any x - BenchmarkCodecQuickAllJsonSuite "$@" | _suite_trim_output |
| 271 | |
| 272 | # These are some very specific suites |
| 273 | # [[ " ${args[*]} " == *"q"* ]] && _suite_very_quick_json_via_suite "$@" | _suite_trim_output |
| 274 | [[ " ${args[*]} " == *"q"* ]] && _suite_very_quick_json_non_suite "$@" | _suite_trim_output |
| 275 | |
| 276 | # These are just helpers (not really running benchmark suites) |
| 277 | [[ " ${args[*]} " == *"t"* ]] && _suite_tests "$@" | _suite_trim_output | _suite_tests_strip_file_line |
| 278 | [[ " ${args[*]} " == *"p"* ]] && zargs+=("-cpuprofile" "cpu.out" "-memprofile" "mem.out" "-memprofilerate" "1") && _suite_very_quick_benchmark "$@" | _suite_trim_output |
| 279 | [[ " ${args[*]} " == *"f"* ]] && ${go[@]} tool pprof bench.test ${1:-mem.out} |
| 280 | [[ " ${args[*]} " == *"z"* ]] && _bench_dot_out_dot_txt |
| 281 | [[ " ${args[*]} " == *"y"* ]] && _suite_debugging "$@" | _suite_trim_output |
| 282 | |
| 283 | true |
| 284 | # shift $((OPTIND-1)) |
| 285 | } |
| 286 | |
| 287 | if [ "." = `dirname $0` ] |
| 288 | then |
| 289 | _main "$@" |
| 290 | else |
| 291 | echo "bench.sh must be run from the directory it resides in" |
| 292 | _usage |
| 293 | fi |