scripts: Import build-perf scripts from oe-core

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Richard Purdie 2019-01-22 22:36:35 +00:00
parent 337cd8ee97
commit fbe172bbdf
3 changed files with 604 additions and 0 deletions

View File

@ -0,0 +1,266 @@
#!/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 COMMITISH] [-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 COMMITISH test (checkout) this commit, <branch>:<commit> can be
specified to test specific commit of certain branch
-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 GIT_REMOTE push results to a remote Git repository
-R DEST rsync reports to a remote destination
-w WORK_DIR work dir for this script
(default: GIT_TOP_DIR/build-perf-test)
-x create xml report (instead of json)
EOF
}
get_os_release_var () {
( source /etc/os-release; eval echo '$'$1 )
}
# Parse command line arguments
commitish=""
oe_build_perf_test_extra_opts=()
oe_git_archive_extra_opts=()
while getopts "ha:c:C:d:E:g:P:R:w:x" opt; do
case $opt in
h) usage
exit 0
;;
a) mkdir -p "$OPTARG"
archive_dir=`realpath -s "$OPTARG"`
;;
c) commitish=$OPTARG
;;
C) mkdir -p "$OPTARG"
results_repo=`realpath -s "$OPTARG"`
;;
d) download_dir=`realpath -s "$OPTARG"`
;;
E) email_to="$OPTARG"
;;
g) mkdir -p "$OPTARG"
globalres_dir=`realpath -s "$OPTARG"`
;;
P) oe_git_archive_extra_opts+=("--push" "$OPTARG")
;;
R) rsync_dst="$OPTARG"
;;
w) base_dir=`realpath -s "$OPTARG"`
;;
x) oe_build_perf_test_extra_opts+=("--xml")
;;
*) 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"
if [ -n "$commitish" ]; then
echo "Running git fetch"
git fetch &> /dev/null
git checkout HEAD^0 &> /dev/null
# Handle <branch>:<commit> format
if echo "$commitish" | grep -q ":"; then
commit=`echo "$commitish" | cut -d":" -f2`
branch=`echo "$commitish" | cut -d":" -f1`
else
commit="$commitish"
branch="$commitish"
fi
echo "Checking out $commitish"
git branch -D $branch &> /dev/null
if ! git checkout -f $branch &> /dev/null; then
echo "ERROR: Git checkout failed"
exit 1
fi
# Check that the specified branch really contains the commit
commit_hash=`git rev-parse --revs-only $commit --`
if [ -z "$commit_hash" -o "`git merge-base $branch $commit`" != "$commit_hash" ]; then
echo "ERROR: branch $branch does not contain commit $commit"
exit 1
fi
git reset --hard $commit > /dev/null
fi
# 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
# 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
# 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
# Upload report files, unless we're on detached head
if [ -n "$rsync_dst" -a -n "$branch" ]; then
echo "Uploading test report"
rsync $report_txt $report_html $rsync_dst
fi
fi
echo -ne "\n\n-----------------\n"
echo "Global results file:"
echo -ne "\n"
cat "$globalres_log"
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"

View File

@ -0,0 +1,282 @@
#!/usr/bin/python3
#
# Send build performance test report emails
#
# Copyright (c) 2017, 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.
#
import argparse
import base64
import logging
import os
import pwd
import re
import shutil
import smtplib
import socket
import subprocess
import sys
import tempfile
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Setup logging
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
log = logging.getLogger('oe-build-perf-report')
# Find js scaper script
SCRAPE_JS = os.path.join(os.path.dirname(__file__), '..', 'lib', 'build_perf',
'scrape-html-report.js')
if not os.path.isfile(SCRAPE_JS):
log.error("Unableto find oe-build-perf-report-scrape.js")
sys.exit(1)
class ReportError(Exception):
"""Local errors"""
pass
def check_utils():
"""Check that all needed utils are installed in the system"""
missing = []
for cmd in ('phantomjs', 'optipng'):
if not shutil.which(cmd):
missing.append(cmd)
if missing:
log.error("The following tools are missing: %s", ' '.join(missing))
sys.exit(1)
def parse_args(argv):
"""Parse command line arguments"""
description = """Email build perf test report"""
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description=description)
parser.add_argument('--debug', '-d', action='store_true',
help="Verbose logging")
parser.add_argument('--quiet', '-q', action='store_true',
help="Only print errors")
parser.add_argument('--to', action='append',
help="Recipients of the email")
parser.add_argument('--cc', action='append',
help="Carbon copy recipients of the email")
parser.add_argument('--bcc', action='append',
help="Blind carbon copy recipients of the email")
parser.add_argument('--subject', default="Yocto build perf test report",
help="Email subject")
parser.add_argument('--outdir', '-o',
help="Store files in OUTDIR. Can be used to preserve "
"the email parts")
parser.add_argument('--text',
help="Plain text message")
parser.add_argument('--html',
help="HTML peport generated by oe-build-perf-report")
parser.add_argument('--phantomjs-args', action='append',
help="Extra command line arguments passed to PhantomJS")
args = parser.parse_args(argv)
if not args.html and not args.text:
parser.error("Please specify --html and/or --text")
return args
def decode_png(infile, outfile):
"""Parse/decode/optimize png data from a html element"""
with open(infile) as f:
raw_data = f.read()
# Grab raw base64 data
b64_data = re.sub('^.*href="data:image/png;base64,', '', raw_data, 1)
b64_data = re.sub('">.+$', '', b64_data, 1)
# Replace file with proper decoded png
with open(outfile, 'wb') as f:
f.write(base64.b64decode(b64_data))
subprocess.check_output(['optipng', outfile], stderr=subprocess.STDOUT)
def mangle_html_report(infile, outfile, pngs):
"""Mangle html file into a email compatible format"""
paste = True
png_dir = os.path.dirname(outfile)
with open(infile) as f_in:
with open(outfile, 'w') as f_out:
for line in f_in.readlines():
stripped = line.strip()
# Strip out scripts
if stripped == '<!--START-OF-SCRIPTS-->':
paste = False
elif stripped == '<!--END-OF-SCRIPTS-->':
paste = True
elif paste:
if re.match('^.+href="data:image/png;base64', stripped):
# Strip out encoded pngs (as they're huge in size)
continue
elif 'www.gstatic.com' in stripped:
# HACK: drop references to external static pages
continue
# Replace charts with <img> elements
match = re.match('<div id="(?P<id>\w+)"', stripped)
if match and match.group('id') in pngs:
f_out.write('<img src="cid:{}"\n'.format(match.group('id')))
else:
f_out.write(line)
def scrape_html_report(report, outdir, phantomjs_extra_args=None):
"""Scrape html report into a format sendable by email"""
tmpdir = tempfile.mkdtemp(dir='.')
log.debug("Using tmpdir %s for phantomjs output", tmpdir)
if not os.path.isdir(outdir):
os.mkdir(outdir)
if os.path.splitext(report)[1] not in ('.html', '.htm'):
raise ReportError("Invalid file extension for report, needs to be "
"'.html' or '.htm'")
try:
log.info("Scraping HTML report with PhangomJS")
extra_args = phantomjs_extra_args if phantomjs_extra_args else []
subprocess.check_output(['phantomjs', '--debug=true'] + extra_args +
[SCRAPE_JS, report, tmpdir],
stderr=subprocess.STDOUT)
pngs = []
images = []
for fname in os.listdir(tmpdir):
base, ext = os.path.splitext(fname)
if ext == '.png':
log.debug("Decoding %s", fname)
decode_png(os.path.join(tmpdir, fname),
os.path.join(outdir, fname))
pngs.append(base)
images.append(fname)
elif ext in ('.html', '.htm'):
report_file = fname
else:
log.warning("Unknown file extension: '%s'", ext)
#shutil.move(os.path.join(tmpdir, fname), outdir)
log.debug("Mangling html report file %s", report_file)
mangle_html_report(os.path.join(tmpdir, report_file),
os.path.join(outdir, report_file), pngs)
return (os.path.join(outdir, report_file),
[os.path.join(outdir, i) for i in images])
finally:
shutil.rmtree(tmpdir)
def send_email(text_fn, html_fn, image_fns, subject, recipients, copy=[],
blind_copy=[]):
"""Send email"""
# Generate email message
text_msg = html_msg = None
if text_fn:
with open(text_fn) as f:
text_msg = MIMEText("Yocto build performance test report.\n" +
f.read(), 'plain')
if html_fn:
html_msg = msg = MIMEMultipart('related')
with open(html_fn) as f:
html_msg.attach(MIMEText(f.read(), 'html'))
for img_fn in image_fns:
# Expect that content id is same as the filename
cid = os.path.splitext(os.path.basename(img_fn))[0]
with open(img_fn, 'rb') as f:
image_msg = MIMEImage(f.read())
image_msg['Content-ID'] = '<{}>'.format(cid)
html_msg.attach(image_msg)
if text_msg and html_msg:
msg = MIMEMultipart('alternative')
msg.attach(text_msg)
msg.attach(html_msg)
elif text_msg:
msg = text_msg
elif html_msg:
msg = html_msg
else:
raise ReportError("Neither plain text nor html body specified")
pw_data = pwd.getpwuid(os.getuid())
full_name = pw_data.pw_gecos.split(',')[0]
email = os.environ.get('EMAIL',
'{}@{}'.format(pw_data.pw_name, socket.getfqdn()))
msg['From'] = "{} <{}>".format(full_name, email)
msg['To'] = ', '.join(recipients)
if copy:
msg['Cc'] = ', '.join(copy)
if blind_copy:
msg['Bcc'] = ', '.join(blind_copy)
msg['Subject'] = subject
# Send email
with smtplib.SMTP('localhost') as smtp:
smtp.send_message(msg)
def main(argv=None):
"""Script entry point"""
args = parse_args(argv)
if args.quiet:
log.setLevel(logging.ERROR)
if args.debug:
log.setLevel(logging.DEBUG)
check_utils()
if args.outdir:
outdir = args.outdir
if not os.path.exists(outdir):
os.mkdir(outdir)
else:
outdir = tempfile.mkdtemp(dir='.')
try:
log.debug("Storing email parts in %s", outdir)
html_report = images = None
if args.html:
html_report, images = scrape_html_report(args.html, outdir,
args.phantomjs_args)
if args.to:
log.info("Sending email to %s", ', '.join(args.to))
if args.cc:
log.info("Copying to %s", ', '.join(args.cc))
if args.bcc:
log.info("Blind copying to %s", ', '.join(args.bcc))
send_email(args.text, html_report, images, args.subject,
args.to, args.cc, args.bcc)
except subprocess.CalledProcessError as err:
log.error("%s, with output:\n%s", str(err), err.output.decode())
return 1
except ReportError as err:
log.error(err)
return 1
finally:
if not args.outdir:
log.debug("Wiping %s", outdir)
shutil.rmtree(outdir)
return 0
if __name__ == "__main__":
sys.exit(main())

View File

@ -0,0 +1,56 @@
var fs = require('fs');
var system = require('system');
var page = require('webpage').create();
// Examine console log for message from chart drawing
page.onConsoleMessage = function(msg) {
console.log(msg);
if (msg === "ALL CHARTS READY") {
window.charts_ready = true;
}
else if (msg.slice(0, 11) === "CHART READY") {
var chart_id = msg.split(" ")[2];
console.log('grabbing ' + chart_id);
var png_data = page.evaluate(function (chart_id) {
var chart_div = document.getElementById(chart_id + '_png');
return chart_div.outerHTML;
}, chart_id);
fs.write(args[2] + '/' + chart_id + '.png', png_data, 'w');
}
};
// Check command line arguments
var args = system.args;
if (args.length != 3) {
console.log("USAGE: " + args[0] + " REPORT_HTML OUT_DIR\n");
phantom.exit(1);
}
// Open the web page
page.open(args[1], function(status) {
if (status == 'fail') {
console.log("Failed to open file '" + args[1] + "'");
phantom.exit(1);
}
});
// Check status every 100 ms
interval = window.setInterval(function () {
//console.log('waiting');
if (window.charts_ready) {
clearTimeout(timer);
clearInterval(interval);
var fname = args[1].replace(/\/+$/, "").split("/").pop()
console.log("saving " + fname);
fs.write(args[2] + '/' + fname, page.content, 'w');
phantom.exit(0);
}
}, 100);
// Time-out after 10 seconds
timer = window.setTimeout(function () {
clearInterval(interval);
console.log("ERROR: timeout");
phantom.exit(1);
}, 10000);