scripts: Convert build-perf-test-wrapper from shell to python

The shell script can't access the config.json data easily, convert
from shell to python so that we prepare the way for further enhancements.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Richard Purdie 2019-02-02 11:42:42 +00:00
parent efeb042a77
commit e0770f4b55
3 changed files with 225 additions and 239 deletions

View File

@ -99,7 +99,13 @@
"buildperf" : {
"MACHINE" : "qemux86",
"EXTRAPLAINCMDS" : [
"${SCRIPTSDIR}/build-perf-test-wrapper.sh -r ${BUILDPERF_RESULTSDIR} -E yocto-perf@yoctoproject.org -d ${BUILDPERF_STATEDIR}/downloads -w /home/pokybuild/build-perf-test -p ${HELPERRESULTSDIR}"
"${SCRIPTSDIR}/build-perf-test-wrapper -r ${BUILDPERF_RESULTSDIR} -E yocto-perf@yoctoproject.org -d ${BUILDPERF_STATEDIR}/downloads -w /home/pokybuild/build-perf-test -p ${HELPERRESULTSDIR}"
],
"extravars" : [
"BB_NUMBER_THREADS = '8'",
"PARALLEL_MAKE = '-j 8'",
"DL_DIR = '${BUILDPERF_STATEDIR}/downloads'",
"CONNECTIVITY_CHECK_URIS = ''"
]
},
"selftest" : {

218
scripts/build-perf-test-wrapper Executable file
View File

@ -0,0 +1,218 @@
#!/usr/bin/env python3
#
# Build performance test script wrapper
#
# Copyright (c) 2019, Linux Foundation
#
# This program is free software; you can redistribute it and/or modify it
# under the terms and conditions of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# This script is a simple wrapper around the build performance tests
import os
import fcntl
import errno
import utils
import subprocess
import sys
import datetime
parser = utils.ArgParser(description='Run a build performance test and process the results.')
parser.add_argument('-a', '--archive-dir',
action='store',
help="Where to archive a results tarball")
parser.add_argument('-C', '--git-repo',
action='store',
help="Commit results into this git repository")
parser.add_argument('-d', '--download-dir',
action='store',
help="Directory to download sources into")
parser.add_argument('-E', '--email-addr',
action='store',
help="Send an email report to this address")
parser.add_argument('-g', '--global-results',
action='store',
help="Save a global results file in this directory")
parser.add_argument('-P', '--push-remote',
action='store',
help="Push git changes to this remote repository")
parser.add_argument('-r', '--results-dir',
action='store',
help="Where to store results artefacts")
parser.add_argument('-w', '--work-dir',
action='store',
help="build directory to run the tests in")
parser.add_argument('-p', '--publish-dir',
action='store',
help="directory to publish into")
args = parser.parse_args()
scriptsdir = os.path.dirname(os.path.realpath(__file__))
ourconfig = utils.loadconfig()
if args.git_repo:
os.makedirs(args.git_repo, exist_ok=True)
if args.download_dir:
os.makedirs(args.download_dir, exist_ok=True)
if args.global_results:
os.makedirs(args.global_results, exist_ok=True)
if args.publish_dir:
os.makedirs(args.publish_dir, exist_ok=True)
archiveopts = ""
if args.push_remote:
archiveopts += "--push " + args.push_remote
if args.results_dir:
args.archive_dir = args.results_dir + "/archive"
args.git_repo = args.results_dir + "/archive-repo"
args.global_results = args.results_dir
os.makedirs(args.results_dir, exist_ok=True)
if args.email_addr:
try:
subprocess.check_output(["which", "phantomjs"])
except subprocess.CalledProcessError:
print("Please install phantomjs to email reports")
sys.exit(1)
try:
subprocess.check_output(["which", "optipng"])
except subprocess.CalledProcessError:
print("Please install optipng to email reports")
sys.exit(1)
op = fcntl.LOCK_EX
try:
lf = open("/tmp/oe-build-perf-test-wrapper.lock", 'a+')
fileno = lf.fileno()
fcntl.flock(fileno, op)
except OSError as e:
print("Another version of the script is running")
try:
lf.close()
except Exception:
pass
sys.exit(1)
print("Running on " + os.uname()[1])
try:
gitdir = subprocess.check_output("git rev-parse --show-toplevel", shell=True).decode("utf-8").strip()
except subprocess.CalledProcessError:
print("The current working dir doesn't seem to be a git clone. Please cd there before running " + sys.argv[0])
sys.exit(1)
os.chdir(gitdir)
# Determine name of the current branch
branch = subprocess.check_output("git symbolic-ref HEAD 2> /dev/null", shell=True).decode("utf-8").strip()
# Strip refs/heads/
branch = branch[11:]
if not args.work_dir:
args.work_dir = gitdir + "/build-perf-test"
if not args.archive_dir:
args.archive_dir = gitdir + "/archive-results"
os.makedirs(args.archive_dir, exist_ok=True)
# Ensure we start with a clean build buil directory
subprocess.check_call("rm -rf %s/*" % args.work_dir, shell=True)
os.makedirs(args.work_dir + "/conf", exist_ok=True)
# copy in auto.conf
subprocess.check_call("cp -r %s/build/conf/auto.conf %s/conf" % (gitdir, args.work_dir), shell=True)
print("Using working dir " + args.work_dir)
if not args.download_dir:
args.download_dir = args.work_dir + "\downloads"
if not args.global_results:
args.global_results = args.work_dir
timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
gitrev = subprocess.check_output("git rev-parse --short HEAD", shell=True).decode("utf-8").strip()
build_dir = args.work_dir + "build-" + gitrev + "-" + timestamp
results_tmpdir = args.work_dir + "results-" + gitrev + "-" + timestamp
globalres_log = args.global_results + "/globalres.log"
machine = "qemux86"
os.makedirs(args.work_dir, exist_ok=True)
# Run actual test script
ret = subprocess.call(". ./oe-init-build-env %s >/dev/null; oe-build-perf-test --out-dir %s --globalres-file %s --lock-file %s/oe-build-perf.lock"
% (build_dir, results_tmpdir, globalres_log, args.work_dir), shell=True)
if ret == 1:
print("ERROR: oe-build-perf-test script failed!")
sys.exit(1)
if ret == 2:
print("ERROR: some tests failed!")
if args.publish_dir:
subprocess.check_call("cp -r " + results_tmpdir + "/* " + args.publish_dir, shell=True)
# Commit results to git
if args.git_repo:
print("\nArchiving results in " + args.git_repo)
subprocess.check_call("oe-git-archive " \
"--git-dir " + args.git_repo + " " \
"--branch-name '{hostname}/{branch}/{machine}' " \
"--tag-name '{hostname}/{branch}/{machine}/{commit_count}-g{commit}/{tag_number}' " \
"--exclude 'buildstats.json' " \
"--notes 'buildstats/{branch_name}' " + results_tmpdir + "/buildstats.json " \
+ archiveopts + " " + results_tmpdir, shell=True)
# Generate test reports
sanitized_branch = branch.replace("/", "_")
report_txt = os.uname()[1] + "_" + sanitized_branch + "_" + machine + ".txt"
report_html = os.uname()[1] + "_" + sanitized_branch + "_" + machine + ".html"
print("\nGenerating test report")
subprocess.check_call("oe-build-perf-report -r " + args.git_repo + " > " + report_txt, shell=True)
subprocess.check_call("oe-build-perf-report -r " + args.git_repo + " --html > " + report_html, shell=True)
filename = os.uname()[1] + "_" + sanitized_branch + "_" + timestamp + "_" + gitrev
subprocess.check_call("cp " + report_txt + " " + args.global_results + "/" + filename + ".txt", shell=True)
subprocess.check_call("cp " + report_html + " " + args.global_results + "/" + filename + ".html", shell=True)
if args.publish_dir:
subprocess.check_call("cp " + report_txt + " " + args.publish_dir + "/" + filename + ".txt", shell=True)
subprocess.check_call("cp " + report_html + " " + args.publish_dir + "/" + filename + ".html", shell=True)
# Send email report
if args.email_addr:
print("Emailing test report")
os_name = subprocess.check_output(". /etc/os-release; eval echo '$'PRETTY_NAME", shell=True).decode("utf-8").strip()
cmd = scriptsdir + "/oe-build-perf-report-email.py --to '" + args.email_addr + \
"' --subject 'Build Perf Test Report for " + os_name + "' --text " + \
report_txt + " --html " + report_html
try:
subprocess.check_call(cmd, shell=True)
except subprocess.CalledProcessError:
print("ERROR: Send email command %s failed" % cmd)
if args.archive_dir:
print("\n\n-----------------\n")
print("Archiving results in " + args.archive_dir)
os.makedirs(args.archive_dir, exist_ok=True)
results_basename = os.path.basename(results_tmpdir)
results_dirname = os.path.dirname(results_tmpdir)
cmd = "tar -czf" + args.archive_dir + "/" + os.uname()[1] + "-" + results_basename + ".tar.gz -C " + results_dirname + " " + results_basename
subprocess.check_call(cmd, shell=True)
subprocess.check_call("rm -rf %s" % build_dir, shell=True)
subprocess.check_call("rm -rf %s" % results_tmpdir, shell=True)
print("DONE")

View File

@ -1,238 +0,0 @@
#!/bin/bash
#
# Build performance test script wrapper
#
# Copyright (c) 2016, Intel Corporation.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms and conditions of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
#
# This script is a simple wrapper around the actual build performance tester
# script. This script initializes the build environment, runs
# oe-build-perf-test and archives the results.
script=`basename $0`
script_dir=$(realpath $(dirname $0))
archive_dir=~/perf-results/archives
usage () {
cat << EOF
Usage: $script [-h] [-C GIT_REPO]
Optional arguments:
-h show this help and exit.
-a ARCHIVE_DIR archive results tarball here, give an empty string to
disable tarball archiving (default: $archive_dir)
-C GIT_REPO commit results into Git
-d DOWNLOAD_DIR directory to store downloaded sources in
-E EMAIL_ADDR send email report
-g GLOBALRES_DIR where to place the globalres file
-p PUBLISH_DIR directory to publish into
-P GIT_REMOTE push results to a remote Git repository
-r RESULTS_DIR directory to store results artefacts in
-w WORK_DIR work dir for this script
(default: GIT_TOP_DIR/build-perf-test)
EOF
}
get_os_release_var () {
( source /etc/os-release; eval echo '$'$1 )
}
# Parse command line arguments
oe_build_perf_test_extra_opts=()
oe_git_archive_extra_opts=()
while getopts "ha:c:C:d:E:g:p:P:r:R:w:x" opt; do
case $opt in
h) usage
exit 0
;;
a) mkdir -p "$OPTARG"
archive_dir=`realpath -s "$OPTARG"`
;;
C) mkdir -p "$OPTARG"
results_repo=`realpath -s "$OPTARG"`
;;
d) mkdir -p "$OPTARG"
download_dir=`realpath -s "$OPTARG"`
;;
E) email_to="$OPTARG"
;;
g) mkdir -p "$OPTARG"
globalres_dir=`realpath -s "$OPTARG"`
;;
p) mkdir -p "$OPTARG"
publish_dir=`realpath -s "$OPTARG"`
;;
P) oe_git_archive_extra_opts+=("--push" "$OPTARG")
;;
r) archive_dir=`realpath -s "$OPTARG"`/archive
results_repo=`realpath -s "$OPTARG"`/archive-repo
globalres_dir=`realpath -s "$OPTARG"`
mkdir -p $results_repo $archive_dir
;;
w) base_dir=`realpath -s "$OPTARG"`
if [ -n "$base_dir" ]; then
rm -rf $base_dir/*
fi
;;
*) usage
exit 1
;;
esac
done
# Check positional args
shift "$((OPTIND - 1))"
if [ $# -ne 0 ]; then
echo "ERROR: No positional args are accepted."
usage
exit 1
fi
if [ -n "$email_to" ]; then
if ! [ -x "$(command -v phantomjs)" ]; then
echo "ERROR: Sending email needs phantomjs."
exit 1
fi
if ! [ -x "$(command -v optipng)" ]; then
echo "ERROR: Sending email needs optipng."
exit 1
fi
fi
# Open a file descriptor for flock and acquire lock
LOCK_FILE="/tmp/oe-build-perf-test-wrapper.lock"
if ! exec 3> "$LOCK_FILE"; then
echo "ERROR: Unable to open lock file"
exit 1
fi
if ! flock -n 3; then
echo "ERROR: Another instance of this script is running"
exit 1
fi
echo "Running on `uname -n`"
if ! git_topdir=$(git rev-parse --show-toplevel); then
echo "The current working dir doesn't seem to be a git clone. Please cd there before running `basename $0`"
exit 1
fi
cd "$git_topdir"
# Determine name of the current branch
branch=`git symbolic-ref HEAD 2> /dev/null`
# Strip refs/heads/
branch=${branch:11}
# Setup build environment
if [ -z "$base_dir" ]; then
base_dir="$git_topdir/build-perf-test"
fi
echo "Using working dir $base_dir"
if [ -z "$download_dir" ]; then
download_dir="$base_dir/downloads"
fi
if [ -z "$globalres_dir" ]; then
globalres_dir="$base_dir"
fi
timestamp=`date "+%Y%m%d%H%M%S"`
git_rev=$(git rev-parse --short HEAD) || exit 1
build_dir="$base_dir/build-$git_rev-$timestamp"
results_dir="$base_dir/results-$git_rev-$timestamp"
globalres_log="$globalres_dir/globalres.log"
machine="qemux86"
mkdir -p "$base_dir"
source ./oe-init-build-env $build_dir >/dev/null || exit 1
# Additional config
auto_conf="$build_dir/conf/auto.conf"
echo "MACHINE = \"$machine\"" > "$auto_conf"
echo 'BB_NUMBER_THREADS = "8"' >> "$auto_conf"
echo 'PARALLEL_MAKE = "-j 8"' >> "$auto_conf"
echo "DL_DIR = \"$download_dir\"" >> "$auto_conf"
# Disabling network sanity check slightly reduces the variance of timing results
echo 'CONNECTIVITY_CHECK_URIS = ""' >> "$auto_conf"
# Possibility to define extra settings
if [ -f "$base_dir/auto.conf.extra" ]; then
cat "$base_dir/auto.conf.extra" >> "$auto_conf"
fi
# Run actual test script
oe-build-perf-test --out-dir "$results_dir" \
--globalres-file "$globalres_log" \
"${oe_build_perf_test_extra_opts[@]}" \
--lock-file "$base_dir/oe-build-perf.lock"
case $? in
1) echo "ERROR: oe-build-perf-test script failed!"
exit 1
;;
2) echo "NOTE: some tests failed!"
;;
esac
if [ -n "$publish_dir" ]; then
cp -r ${results_dir}/* $publish_dir
fi
# Commit results to git
if [ -n "$results_repo" ]; then
echo -e "\nArchiving results in $results_repo"
oe-git-archive \
--git-dir "$results_repo" \
--branch-name "{hostname}/{branch}/{machine}" \
--tag-name "{hostname}/{branch}/{machine}/{commit_count}-g{commit}/{tag_number}" \
--exclude "buildstats.json" \
--notes "buildstats/{branch_name}" "$results_dir/buildstats.json" \
"${oe_git_archive_extra_opts[@]}" \
"$results_dir"
# Generate test reports
sanitized_branch=`echo $branch | tr / _`
report_txt=`hostname`_${sanitized_branch}_${machine}.txt
report_html=`hostname`_${sanitized_branch}_${machine}.html
echo -e "\nGenerating test report"
oe-build-perf-report -r "$results_repo" > $report_txt
oe-build-perf-report -r "$results_repo" --html > $report_html
cp $report_txt $globalres_dir/`hostname`_${sanitized_branch}__$timestamp_$git_rev.txt
cp $report_html $globalres_dir/`hostname`_${sanitized_branch}_$timestamp_$git_rev.html
if [ -n "$publish_dir" ]; then
cp $report_txt $publish_dir/`hostname`_${sanitized_branch}_$timestamp_$git_rev.txt
cp $report_html $publish_dir/`hostname`_${sanitized_branch}_$timestamp_$git_rev.html
fi
# Send email report
if [ -n "$email_to" ]; then
echo "Emailing test report"
os_name=`get_os_release_var PRETTY_NAME`
"$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[@]}"
fi
fi
if [ -n "$archive_dir" ]; then
echo -ne "\n\n-----------------\n"
echo "Archiving results in $archive_dir"
mkdir -p "$archive_dir"
results_basename=`basename "$results_dir"`
results_dirname=`dirname "$results_dir"`
tar -czf "$archive_dir/`uname -n`-${results_basename}.tar.gz" -C "$results_dirname" "$results_basename"
fi
rm -rf "$build_dir"
rm -rf "$results_dir"
echo "DONE"