You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

jc-bench.sh 6.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #!/bin/sh
  2. #
  3. # Benchmarking harness for json-c
  4. #
  5. # Use this to compare the behavior of two different versions of the library,
  6. # such as json-c-0.14 release vs master, master vs local changes, etc...
  7. #
  8. set -e
  9. trap 'echo "FAILED!"' EXIT
  10. RUNDIR=$(dirname "$0")
  11. RUNDIR=$(cd "$RUNDIR" && pwd)
  12. TOP=$(cd "$RUNDIR/.." && pwd)
  13. usage()
  14. {
  15. exitval=$1
  16. errmsg=$2
  17. if [ $exitval -ne 0 ] ; then
  18. exec 1>&2
  19. fi
  20. if [ ! -z "$errmsg" ] ; then
  21. echo "ERROR: $errmsg" 1>&2
  22. fi
  23. cat <<EOF
  24. Usage: $0 [-h] [-v] [--build] [--run] [--compare] ...XAX...
  25. EOF
  26. exit $extival
  27. }
  28. before_arg=
  29. after_arg=
  30. do_all=1
  31. do_build=0
  32. do_run=0
  33. do_compare=0
  34. while [ $# -gt 0 ] ; do
  35. case "$1" in
  36. --before)
  37. before_arg=$2
  38. shift
  39. ;;
  40. --after)
  41. after_arg=$2
  42. shift
  43. ;;
  44. --build)
  45. do_all=0
  46. do_build=1
  47. ;;
  48. --run)
  49. do_all=0
  50. do_run=1
  51. ;;
  52. --compare)
  53. do_all=0
  54. do_compare=1
  55. ;;
  56. -h)
  57. usage 0 ""
  58. ;;
  59. -v)
  60. set -x
  61. ;;
  62. *)
  63. usage 1 "Unknown args: $*"
  64. ;;
  65. esac
  66. shift
  67. done
  68. WORK="${RUNDIR}/work"
  69. mkdir -p "${WORK}"
  70. DATA="${RUNDIR}/data"
  71. mkdir -p "${DATA}"
  72. for file in citm_catalog.json twitter.json canada.json ; do
  73. if [ ! -r "${DATA}/${file}" ] ; then
  74. echo "Fetching ${file} from github.com/mloskot/json_benchmark"
  75. URL="https://github.com/mloskot/json_benchmark/raw/master/data/${file}"
  76. curl -s -L -o "${DATA}/${file}" "$URL"
  77. fi
  78. done
  79. echo
  80. # Identify "after" commit hash, in order of preference
  81. if [ ! -z "$after_arg" -a -d "$after_arg" ] ; then
  82. # Use provided directory
  83. after_src_dir="$after_arg"
  84. after_commit=
  85. echo "Using provided directory [$after_arg] as 'after'"
  86. else
  87. _commit=
  88. if [ ! -z "$after_arg" ] ; then
  89. # Use provided commit hash
  90. _commit=$(git rev-parse --verify "$after_arg")
  91. fi
  92. if [ ! -z "$_commit" ] ;then
  93. after_src_dir= # i.e. current tree
  94. after_commit="$_commit"
  95. echo "Using provided commit [$after_arg => $_commit] as 'after'"
  96. else
  97. # Local changes in current working directory
  98. # ${cur_branch}
  99. after_src_dir=$TOP
  100. after_commit=
  101. echo "Using local changes in $TOP as 'after'"
  102. fi
  103. fi
  104. # Identify "before" commit hash, in order of preference
  105. if [ ! -z "$before_arg" -a -d "$before_arg" ] ; then
  106. # Use provided directory
  107. before_src_dir="$before_arg"
  108. before_commit=
  109. echo "Using provided directory [$before_arg] as 'before'"
  110. else
  111. _commit=
  112. if [ ! -z "$before_arg" ] ; then
  113. # Use provided commit hash
  114. _commit=$(git rev-parse --verify "$before_arg")
  115. fi
  116. if [ ! -z "$_commit" ] ;then
  117. before_src_dir= # i.e. current tree
  118. before_commit="$_commit"
  119. echo "Using provided commit [$before_arg => $_commit] as 'before'"
  120. else
  121. # Use origin/${cur_branch}, if different from ${after_commit}
  122. _cur_branch=$(git rev-parse --abbrev-ref HEAD)
  123. _commit=
  124. if [ ! -z "${_cur_branch}" ] ; then
  125. _commit=$(git rev-parse --verify "origin/${_cur_branch}")
  126. echo "Using origin/${_cur_branch} [$_commit] as 'before'"
  127. fi
  128. if [ "$_commit" = "${after_commit}" ] ; then
  129. _commit=
  130. fi
  131. fi
  132. if [ ! -z "$_commit" ] ; then
  133. before_src_dir= # i.e. current tree
  134. before_commit="$_commit"
  135. else
  136. # Use previous release
  137. before_src_dir= # i.e. current tree
  138. before_commit="$(git tag | sort | tail -1)"
  139. echo "Using previous release [$before_commit] as 'before'"
  140. fi
  141. fi
  142. echo
  143. compile_benchmark()
  144. {
  145. local bname=$1
  146. local src_dir="$2"
  147. local src_commit="$3"
  148. local build_dir="${WORK}/$bname/build"
  149. local inst_dir="${WORK}/$bname/install"
  150. local bench_dir="${WORK}/$bname/bench"
  151. echo
  152. echo "=========== $bname ==========="
  153. echo
  154. mkdir -p "${build_dir}"
  155. mkdir -p "${inst_dir}"
  156. mkdir -p "${bench_dir}"
  157. if [ ! -z "$src_commit" ] ; then
  158. # Resolve the short hash, tag or branch name to full hash
  159. src_commit=$(git rev-parse $src_commit)
  160. fi
  161. # No src dir specified, clone and checkout $src_commit
  162. if [ -z "$src_dir" ] ; then
  163. src_dir="${WORK}/$bname/src"
  164. echo "=== Using sources in $src_dir"
  165. mkdir -p "$src_dir"
  166. at_commit=$(git --git-dir="$src_dir/.git" rev-parse HEAD 2> /dev/null || true)
  167. echo "at_commit: $at_commit"
  168. if [ -z "$at_commit" ] ; then
  169. # Assume it's an empty dir
  170. git clone -n "$TOP" "$src_dir"
  171. fi
  172. git -C "$src_dir" --git-dir="$src_dir/.git" checkout "$src_commit"
  173. fi
  174. # else, use the provided $src_dir
  175. if [ -e "${src_dir}/CMakeLists.txt" ] ; then
  176. cd "${build_dir}"
  177. cmake -DCMAKE_INSTALL_PREFIX="${inst_dir}" "${src_dir}"
  178. else
  179. # Old versions of json-c used automake/autoconf
  180. cd "${src_dir}"
  181. sh autogen.sh # always run it, configure doesn't always work
  182. cd "${build_dir}"
  183. "${src_dir}/configure" --prefix="${inst_dir}"
  184. fi
  185. make all install
  186. cd "${bench_dir}"
  187. cmake -DCMAKE_PREFIX_PATH="${inst_dir}" "${TOP}/apps"
  188. make all
  189. }
  190. # XXX TODO: name "after" and "before" uniquely using the dir & commit
  191. if [ $do_all -ne 0 -o $do_build -ne 0 ] ; then
  192. sleep 5 # Wait slightly, to allow the human to read the message
  193. # about what exactly we're doing to benchmark.
  194. compile_benchmark "after" "${after_src_dir}" "${after_commit}"
  195. compile_benchmark "before" "${before_src_dir}" "${before_commit}"
  196. fi
  197. run_benchmark()
  198. {
  199. local bname=$1
  200. local inst_dir="${WORK}/$bname/install"
  201. local bench_dir="${WORK}/$bname/bench"
  202. local INPUT=${DATA}/canada.json
  203. cd "${bench_dir}"
  204. mkdir -p results
  205. (time ./json_parse -n "${INPUT}") > results/basic_timing.out 2>&1
  206. valgrind --tool=massif --massif-out-file=massif.out ./json_parse -n "${INPUT}"
  207. ms_print massif.out > results/ms_print.out
  208. heaptrack -o heaptrack_out ./json_parse -n "${INPUT}"
  209. heaptrack --analyze heaptrack_out.gz -H histogram.out > results/heaptrack.out
  210. awk ' { s=$1; count=$2; ru=(int((s+ 15) / 16)) * 16; wasted = ((ru-s)*count); print s, count, ru-s, wasted; total=total+wasted} END { print "Total: ", total }' histogram.out > results/histogram2.out
  211. # XXX stamp some info about what was built & run into ./results/.
  212. echo "DONE with $bname"
  213. }
  214. if [ $do_all -ne 0 -o $do_run -ne 0 ] ; then
  215. run_benchmark "after"
  216. run_benchmark "before"
  217. fi
  218. if [ $do_compare -ne 0 ] ; then
  219. # XXX this needs better analysis
  220. cd "${WORK}"
  221. diff -udr before/bench/results after/bench/results || true
  222. else
  223. echo "To compare results, run:"
  224. echo "$0 --compare"
  225. fi
  226. trap '' EXIT
  227. :<<=cut
  228. Benchmarks to run:
  229. * Parse JSON strings, of various sizes and characteristics
  230. * Flags: STRICT vs. non-STRICT, validate UTF8
  231. * Serialization time
  232. * plain, spaces, pretty
  233. * json_c_visit tests
  234. * JSON pointer tests
  235. Things to record and compare:
  236. * Running time
  237. * Peak memory usage
  238. * Useful bytes vs. overhead for memory allocations
  239. * Total number of allocations
  240. * Average allocation size
  241. * Log of all allocation sizes
  242. =cut