build-perf-test-wrapper.sh 7.6 KB

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