aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/build-perf-test-wrapper
blob: 660ef827f8213d0475a09333d412a299ab330811 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/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

import utils

parser = utils.ArgParser(description='Run a build performance test and process the results.')

parser.add_argument('-b', '--branch',
                    action='store',
                    help="Branch being tested")
parser.add_argument('-R', '--repo',
                    action='store',
                    help="Repository name being tested")
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('-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()

git_repo = None
archive_dir = None
global_results = None
archiveopts = ""
if args.push_remote:
    archiveopts += "--push " + args.push_remote
if args.results_dir:
    archive_dir = args.results_dir + "/archive"
    git_repo = args.results_dir + "/archive-repo"
    global_results = args.results_dir

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)

hostname = os.uname()[1]

print("Running on " + hostname)

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 = args.branch
if not args.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 archive_dir:
    archive_dir = gitdir + "/archive-results"
os.makedirs(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)

print("Using working dir " + args.work_dir)

if not args.download_dir:
    args.download_dir = args.work_dir + "/downloads"
os.makedirs(args.download_dir, exist_ok=True)

if not global_results:
    global_results = args.work_dir
os.makedirs(global_results, exist_ok=True)


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()
gitrevfull = subprocess.check_output("git rev-parse 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 = global_results + "/globalres.log"
machine = "qemux86"

os.makedirs(args.work_dir, exist_ok=True)
os.makedirs(build_dir + "/conf", exist_ok=True)
# copy in auto.conf
subprocess.check_call("cp -r %s/build/conf/auto.conf %s/conf" % (gitdir, build_dir), shell=True)

# Run actual test script
ret = subprocess.call("BDIR=%s . ./oe-init-build-env >/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:
    os.makedirs(args.publish_dir, exist_ok=True)
    subprocess.check_call("cp -r %s/* %s" % (results_tmpdir, args.publish_dir), shell=True)

# Commit results to git
if git_repo:
    print("\nArchiving results in " + git_repo)

    os.makedirs(git_repo, exist_ok=True)
    subprocess.check_call("BDIR=%s . ./oe-init-build-env >/dev/null; oe-git-archive " % build_dir + \
        "--git-dir " + 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 = build_dir + "/" + hostname + "_" + sanitized_branch + "_" + machine + ".txt"
    report_html = build_dir + "/" + hostname + "_" + sanitized_branch + "_" + machine + ".html"
    filename = hostname + "_" + sanitized_branch + "_" + timestamp + "_" + gitrev

    extraopts = " --branch %s --commit %s" % (branch, gitrevfull)
    if args.repo and args.branch:
        basebranch, comparebranch = utils.getcomparisonbranch(ourconfig, args.repo, args.branch)
        if comparebranch:
            extraopts = extraopts + " --branch2 %s" % (comparebranch)
        else:
            print("No comparision branch found, comparing to master")
            extraopts = extraopts + " --branch2 master"

    print("\nGenerating test report")
    open(report_txt, "w").close()
    if args.publish_dir:
        web_root = utils.getconfig('WEBPUBLISH_DIR', ourconfig)
        web_url = utils.getconfig('WEBPUBLISH_URL', ourconfig)
        url = args.publish_dir.replace(web_root, web_url) + "/" + filename + ".html"

        print("Report URL is %s" % url)

        with open(report_txt, "w") as f:
            f.write("HTML Report/Graphs are available at:\n    %s\n\n" % url)

    subprocess.check_call("BDIR=%s . ./oe-init-build-env >/dev/null; oe-build-perf-report -r %s %s >> %s" % (build_dir, git_repo, extraopts, report_txt), shell=True)
    subprocess.check_call("BDIR=%s . ./oe-init-build-env >/dev/null; oe-build-perf-report -r %s %s --html > %s " % (build_dir, git_repo, extraopts, report_html), shell=True)

    subprocess.check_call("cp %s %s/%s.txt" % (report_txt, global_results, filename), shell=True)
    subprocess.check_call("cp %s %s/%s.html" % (report_html, global_results, filename), shell=True)

    if args.publish_dir:
        subprocess.check_call("cp %s %s/%s.txt" % (report_txt, args.publish_dir, filename), shell=True)
        subprocess.check_call("cp %s %s/%s.html" % (report_html, args.publish_dir, filename), 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 archive_dir:
    print("\n\n-----------------\n")
    print("Archiving results in " + archive_dir)
    os.makedirs(archive_dir, exist_ok=True)
    results_basename = os.path.basename(results_tmpdir)
    results_dirname = os.path.dirname(results_tmpdir)
    cmd = "tar -czf %s/%s-%s.tar.gz -C %s %s" % (archive_dir, hostname, results_basename, 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)
subprocess.check_call("rm -rf *", shell=True)

print("DONE")