build-perf-test-wrapper.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #!/bin/bash
  2. #
  3. # Build performance test script wrapper
  4. #
  5. # Copyright (c) 2016, Intel Corporation.
  6. #
  7. # This program is free software; you can redistribute it and/or modify it
  8. # under the terms and conditions of the GNU General Public License,
  9. # version 2, as published by the Free Software Foundation.
  10. #
  11. # This program is distributed in the hope it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. # more details.
  15. #
  16. #
  17. # This script is a simple wrapper around the actual build performance tester
  18. # script. This script initializes the build environment, runs
  19. # oe-build-perf-test and archives the results.
  20. script=`basename $0`
  21. script_dir=$(realpath $(dirname $0))
  22. archive_dir=~/perf-results/archives
  23. usage () {
  24. cat << EOF
  25. Usage: $script [-h] [-c COMMITISH] [-C GIT_REPO]
  26. Optional arguments:
  27. -h show this help and exit.
  28. -a ARCHIVE_DIR archive results tarball here, give an empty string to
  29. disable tarball archiving (default: $archive_dir)
  30. -c COMMITISH test (checkout) this commit, <branch>:<commit> can be
  31. specified to test specific commit of certain branch
  32. -C GIT_REPO commit results into Git
  33. -d DOWNLOAD_DIR directory to store downloaded sources in
  34. -E EMAIL_ADDR send email report
  35. -g GLOBALRES_DIR where to place the globalres file
  36. -P GIT_REMOTE push results to a remote Git repository
  37. -R DEST rsync reports to a remote destination
  38. -w WORK_DIR work dir for this script
  39. (default: GIT_TOP_DIR/build-perf-test)
  40. -x create xml report (instead of json)
  41. EOF
  42. }
  43. get_os_release_var () {
  44. ( source /etc/os-release; eval echo '$'$1 )
  45. }
  46. # Parse command line arguments
  47. commitish=""
  48. oe_build_perf_test_extra_opts=()
  49. oe_git_archive_extra_opts=()
  50. while getopts "ha:c:C:d:E:g:P:R:w:x" opt; do
  51. case $opt in
  52. h) usage
  53. exit 0
  54. ;;
  55. a) mkdir -p "$OPTARG"
  56. archive_dir=`realpath -s "$OPTARG"`
  57. ;;
  58. c) commitish=$OPTARG
  59. ;;
  60. C) mkdir -p "$OPTARG"
  61. results_repo=`realpath -s "$OPTARG"`
  62. ;;
  63. d) download_dir=`realpath -s "$OPTARG"`
  64. ;;
  65. E) email_to="$OPTARG"
  66. ;;
  67. g) mkdir -p "$OPTARG"
  68. globalres_dir=`realpath -s "$OPTARG"`
  69. ;;
  70. P) oe_git_archive_extra_opts+=("--push" "$OPTARG")
  71. ;;
  72. R) rsync_dst="$OPTARG"
  73. ;;
  74. w) base_dir=`realpath -s "$OPTARG"`
  75. ;;
  76. x) oe_build_perf_test_extra_opts+=("--xml")
  77. ;;
  78. *) usage
  79. exit 1
  80. ;;
  81. esac
  82. done
  83. # Check positional args
  84. shift "$((OPTIND - 1))"
  85. if [ $# -ne 0 ]; then
  86. echo "ERROR: No positional args are accepted."
  87. usage
  88. exit 1
  89. fi
  90. if [ -n "$email_to" ]; then
  91. if ! [ -x "$(command -v phantomjs)" ]; then
  92. echo "ERROR: Sending email needs phantomjs."
  93. exit 1
  94. fi
  95. if ! [ -x "$(command -v optipng)" ]; then
  96. echo "ERROR: Sending email needs optipng."
  97. exit 1
  98. fi
  99. fi
  100. # Open a file descriptor for flock and acquire lock
  101. LOCK_FILE="/tmp/oe-build-perf-test-wrapper.lock"
  102. if ! exec 3> "$LOCK_FILE"; then
  103. echo "ERROR: Unable to open lock file"
  104. exit 1
  105. fi
  106. if ! flock -n 3; then
  107. echo "ERROR: Another instance of this script is running"
  108. exit 1
  109. fi
  110. echo "Running on `uname -n`"
  111. if ! git_topdir=$(git rev-parse --show-toplevel); then
  112. echo "The current working dir doesn't seem to be a git clone. Please cd there before running `basename $0`"
  113. exit 1
  114. fi
  115. cd "$git_topdir"
  116. if [ -n "$commitish" ]; then
  117. echo "Running git fetch"
  118. git fetch &> /dev/null
  119. git checkout HEAD^0 &> /dev/null
  120. # Handle <branch>:<commit> format
  121. if echo "$commitish" | grep -q ":"; then
  122. commit=`echo "$commitish" | cut -d":" -f2`
  123. branch=`echo "$commitish" | cut -d":" -f1`
  124. else
  125. commit="$commitish"
  126. branch="$commitish"
  127. fi
  128. echo "Checking out $commitish"
  129. git branch -D $branch &> /dev/null
  130. if ! git checkout -f $branch &> /dev/null; then
  131. echo "ERROR: Git checkout failed"
  132. exit 1
  133. fi
  134. # Check that the specified branch really contains the commit
  135. commit_hash=`git rev-parse --revs-only $commit --`
  136. if [ -z "$commit_hash" -o "`git merge-base $branch $commit`" != "$commit_hash" ]; then
  137. echo "ERROR: branch $branch does not contain commit $commit"
  138. exit 1
  139. fi
  140. git reset --hard $commit > /dev/null
  141. fi
  142. # Determine name of the current branch
  143. branch=`git symbolic-ref HEAD 2> /dev/null`
  144. # Strip refs/heads/
  145. branch=${branch:11}
  146. # Setup build environment
  147. if [ -z "$base_dir" ]; then
  148. base_dir="$git_topdir/build-perf-test"
  149. fi
  150. echo "Using working dir $base_dir"
  151. if [ -z "$download_dir" ]; then
  152. download_dir="$base_dir/downloads"
  153. fi
  154. if [ -z "$globalres_dir" ]; then
  155. globalres_dir="$base_dir"
  156. fi
  157. timestamp=`date "+%Y%m%d%H%M%S"`
  158. git_rev=$(git rev-parse --short HEAD) || exit 1
  159. build_dir="$base_dir/build-$git_rev-$timestamp"
  160. results_dir="$base_dir/results-$git_rev-$timestamp"
  161. globalres_log="$globalres_dir/globalres.log"
  162. machine="qemux86"
  163. mkdir -p "$base_dir"
  164. source ./oe-init-build-env $build_dir >/dev/null || exit 1
  165. # Additional config
  166. auto_conf="$build_dir/conf/auto.conf"
  167. echo "MACHINE = \"$machine\"" > "$auto_conf"
  168. echo 'BB_NUMBER_THREADS = "8"' >> "$auto_conf"
  169. echo 'PARALLEL_MAKE = "-j 8"' >> "$auto_conf"
  170. echo "DL_DIR = \"$download_dir\"" >> "$auto_conf"
  171. # Disabling network sanity check slightly reduces the variance of timing results
  172. echo 'CONNECTIVITY_CHECK_URIS = ""' >> "$auto_conf"
  173. # Possibility to define extra settings
  174. if [ -f "$base_dir/auto.conf.extra" ]; then
  175. cat "$base_dir/auto.conf.extra" >> "$auto_conf"
  176. fi
  177. # Run actual test script
  178. oe-build-perf-test --out-dir "$results_dir" \
  179. --globalres-file "$globalres_log" \
  180. "${oe_build_perf_test_extra_opts[@]}" \
  181. --lock-file "$base_dir/oe-build-perf.lock"
  182. case $? in
  183. 1) echo "ERROR: oe-build-perf-test script failed!"
  184. exit 1
  185. ;;
  186. 2) echo "NOTE: some tests failed!"
  187. ;;
  188. esac
  189. # Commit results to git
  190. if [ -n "$results_repo" ]; then
  191. echo -e "\nArchiving results in $results_repo"
  192. oe-git-archive \
  193. --git-dir "$results_repo" \
  194. --branch-name "{hostname}/{branch}/{machine}" \
  195. --tag-name "{hostname}/{branch}/{machine}/{commit_count}-g{commit}/{tag_number}" \
  196. --exclude "buildstats.json" \
  197. --notes "buildstats/{branch_name}" "$results_dir/buildstats.json" \
  198. "${oe_git_archive_extra_opts[@]}" \
  199. "$results_dir"
  200. # Generate test reports
  201. sanitized_branch=`echo $branch | tr / _`
  202. report_txt=`hostname`_${sanitized_branch}_${machine}.txt
  203. report_html=`hostname`_${sanitized_branch}_${machine}.html
  204. echo -e "\nGenerating test report"
  205. oe-build-perf-report -r "$results_repo" > $report_txt
  206. oe-build-perf-report -r "$results_repo" --html > $report_html
  207. # Send email report
  208. if [ -n "$email_to" ]; then
  209. echo "Emailing test report"
  210. os_name=`get_os_release_var PRETTY_NAME`
  211. "$script_dir"/oe-build-perf-report-email.py --to "$email_to" --subject "Build Perf Test Report for $os_name" --text $report_txt --html $report_html "${OE_BUILD_PERF_REPORT_EMAIL_EXTRA_ARGS[@]}"
  212. fi
  213. # Upload report files, unless we're on detached head
  214. if [ -n "$rsync_dst" -a -n "$branch" ]; then
  215. echo "Uploading test report"
  216. rsync $report_txt $report_html $rsync_dst
  217. fi
  218. fi
  219. echo -ne "\n\n-----------------\n"
  220. echo "Global results file:"
  221. echo -ne "\n"
  222. cat "$globalres_log"
  223. if [ -n "$archive_dir" ]; then
  224. echo -ne "\n\n-----------------\n"
  225. echo "Archiving results in $archive_dir"
  226. mkdir -p "$archive_dir"
  227. results_basename=`basename "$results_dir"`
  228. results_dirname=`dirname "$results_dir"`
  229. tar -czf "$archive_dir/`uname -n`-${results_basename}.tar.gz" -C "$results_dirname" "$results_basename"
  230. fi
  231. rm -rf "$build_dir"
  232. rm -rf "$results_dir"
  233. echo "DONE"