summaryrefslogtreecommitdiff
path: root/post-build-report.py
blob: 12594afe30f5838b5515695f84a2bd381df62f2b (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/python
import os
import sys
import json
import hashlib
import httplib
import logging
import mimetypes
import xml.etree.ElementTree as ET

from urlparse import urljoin, urlsplit
from StringIO import StringIO


logging.basicConfig(format='%(levelname)s:  %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
httplib.HTTPConnection.debuglevel = 1


RESULT_ENDPOINT = "/api/result/"
BENCHMARK_MANIFEST_PROJECT_LIST = [
    'linaro-art/platform/bionic',
    'linaro-art/platform/build',
    'linaro-art/platform/external/vixl',
    'linaro-art/platform/art'
]


def transcode_results_dict(results_dict, parsed_dict, prefix_key=None):
    for key, value in parsed_dict.items():
        if prefix_key is not None:
            key = "%s/%s" % (prefix_key, key)
        if isinstance(value, dict):
            transcode_results_dict(results_dict, value, key)
        else:
            results_dict.update({key: value})


def encode_multipart_formdata(fields, files):
    LIMIT = '----------lImIt_of_THE_fIle_eW_$'
    CRLF = '\r\n'
    L = []
    for key, value in fields.iteritems():
        L.append('--' + LIMIT)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append('')
        L.append(value)
    for key, values in files.iteritems():
        print key
        if type(values) is not list:
            values = [values]
        for value in values:
            # value should be a 2-tuple with first item as filename and second item file descriptor
            L.append('--' + LIMIT)
            L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, value[0]))
            L.append('Content-Type: %s' % get_content_type(key))
            L.append('')
            value[1].seek(0) # in case the pointer wasn't placed in the beginning of the file
            L.append(value[1].read())
    L.append('--' + LIMIT + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % LIMIT
    return content_type, body

def get_content_type(filename):
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'


def _push_object(auth_pw, backend_url, endpoint, params, files):
    usplit = urlsplit(backend_url)
    url = urljoin(backend_url, endpoint)

    logger.info("Submitting to URL: %s" % url)
    conn = None
    if usplit.scheme.lower() == "http":
        conn = httplib.HTTP(usplit.netloc)
    if usplit.scheme.lower() == "https":
        conn = httplib.HTTPS(usplit.netloc)

    if conn is None:
        print "Unknown scheme: %s" % usplit.scheme
        sys.exit(1)

    content_type, body = encode_multipart_formdata(params, files)
    conn.putrequest('POST', endpoint)
    conn.putheader("Authorization", "Token %s" % auth_pw)
    conn.putheader("Auth-Token", auth_pw)
    conn.putheader('Content-Type', content_type)
    conn.putheader('Content-Length', str(len(body)))
    conn.putheader('Host', usplit.netloc)
    conn.endheaders()
    conn.set_debuglevel(-1)
    conn.send(body)
    errcode, errmsg, headers = conn.getreply()
    logger.info("return code: %s" % errcode)
    logger.info(errmsg)
    logger.info(headers)


    if errcode < 300:
        return conn.file.read()
    else:
        logger.warn(errcode)
        logger.warn(errmsg)
    return []


def _get_manifest(workspace_path):
    manifest_path = os.path.join(workspace_path, "pinned-manifest.xml")
    print "Searching for: %s" % manifest_path
    if os.path.exists(manifest_path):
        with open(manifest_path, "r") as manifest_file:
            return manifest_file.read()
    print "Manifest not found"
    return None


def _get_files(workspace_path):
    files_dict = {}
    for dirpath, dirnames, filenames in os.walk(workspace_path):
        for result_file_name in filenames:
            if result_file_name.endswith(".json"):
                statinfo = os.stat(os.path.join(dirpath, result_file_name))
                print("adding %s [%s]" % (result_file_name, statinfo.st_size))
                files_dict.update({result_file_name: (result_file_name, open(os.path.join(dirpath, result_file_name), 'rb'))})
    return files_dict


def _results(workspace_path):
    benchmarks = {
        "Boot.oat size": ['boot_oat_size_ARM_32_Quick.txt',
                          'boot_oat_size_ARM_64_Quick.txt',
                          'boot_oat_size_x86_32_Quick.txt',
                          'boot_oat_size_x86_64_Quick.txt',
                          'boot_oat_size_x86_64_Optimizing.txt',
                          'boot_oat_size_x86_32_Optimizing.txt',
                          'boot_oat_size_ARM_64_Optimizing.txt',
                          'boot_oat_size_ARM_32_Optimizing.txt',
                          'boot_oat_size_mips_64_Optimizing.txt',
                          'boot_oat_size_mips_32_Quick.txt'],

        "Oat Execution Time": ['avg_oat_time_ARM_32_Quick.txt',
                               'avg_oat_time_ARM_64_Quick.txt',
                               'avg_oat_time_x86_64_Quick.txt',
                               'avg_oat_time_x86_32_Quick.txt',
                               'avg_oat_time_x86_64_Optimizing.txt',
                               'avg_oat_time_x86_32_Optimizing.txt',
                               'avg_oat_time_ARM_32_Optimizing.txt',
                               'avg_oat_time_ARM_64_Optimizing.txt',
                               'avg_oat_time_mips_64_Optimizing.txt',
                               'avg_oat_time_mips_32_Quick.txt']
        }

    val = []

    for benchmark, subscores in benchmarks.items():
        for subscore in subscores:
            path = os.path.join(workspace_path, subscore)
            print path, os.path.exists(path)
            if os.path.exists(path):
                raw = open(path, 'r').read().strip()
                measurement = float(raw.replace("YVALUE=", ""))

                name = (subscore
                        .replace("avg_oat_time_", "")
                        .replace("boot_oat_size_", "")
                        .replace(".txt", "")
                        .replace("_", " "))

                val.append({
                    "benchmark": benchmark,
                    "name": name,
                    "measurement": measurement
                })

    return val


if __name__ == '__main__':
    jenkins_project_name = os.environ.get("SOURCE_PROJECT_NAME")

    jenkins_build_number = os.environ.get("SOURCE_BUILD_NUMBER")
    jenkins_build_id = os.environ.get("SOURCE_BUILD_ID", jenkins_build_number)
    jenkins_build_url = os.environ.get("SOURCE_BUILD_URL")

    branch_name = os.environ.get("SOURCE_BRANCH_NAME", "")

    # QA reports submission
    qa_reports_url = os.environ.get("QA_REPORTS_URL")
    qa_reports_token = os.environ.get("QA_REPORTS_TOKEN")

    art_url = os.environ.get("ART_URL", "http://localhost:8000/")
    art_token = None
    if urlsplit(art_url).netloc.startswith('art-reports'):
        art_token = os.environ.get("ART_TOKEN_ART_REPORTS", None)
    elif urlsplit(art_url).netloc.startswith('android-qa-reports'):
        art_token = os.environ.get("ART_TOKEN_ANDROID_REPORTS", None)

    # we use ART_TOKEN keyword in LAVA so we check for that as well
    if art_token is None:
        art_token = os.environ.get("ART_TOKEN")

    manifest = _get_manifest("./artifacts")
    test_jobs = os.environ.get("LAVA_JOB_IDS", None)
    results = _results("./artifacts")

    if jenkins_build_number is None:
        print "Build number not set. Exiting!"
        sys.exit(1)
    if jenkins_project_name is None:
        print "Project name not set. Exiting!"
        sys.exit(1)
    if jenkins_build_url is None:
        print "Build URL not set. Exiting!"
        sys.exit(1)
    if art_token is None:
        print "ART token not set. Exiting!"
        sys.exit(1)
    if not manifest:
        print "Manifest missing. Exiting!"
        sys.exit(1)

    print "Registered test jobs: %s" % test_jobs

    params = {
        'name': jenkins_project_name,

        'build_id': jenkins_build_id,
        'build_url': jenkins_build_url,
        'build_number': jenkins_build_number,

        'test_jobs': test_jobs,
        'manifest': manifest,
        'branch_name': branch_name,

        "gerrit_change_number": os.environ.get("SOURCE_GERRIT_CHANGE_NUMBER", ""),
        "gerrit_patchset_number":os.environ.get("SOURCE_GERRIT_PATCHSET_NUMBER", ""),
        "gerrit_change_url": os.environ.get("SOURCE_GERRIT_CHANGE_URL", ""),
        "gerrit_change_id": os.environ.get("SOURCE_GERRIT_CHANGE_ID", ""),

        "results": results
    }

    if not results:
        params.pop("results")

    files = {}
    if test_jobs is None:
        params.pop('test_jobs')
        files = _get_files("./artifacts")
    _push_object(art_token, art_url, RESULT_ENDPOINT, params, files)
    params.pop('manifest')
    print params

    # submit to QA reports
    if qa_reports_url is None:
        sys.exit(0)

    if qa_reports_token is None:
        print("Token for QA reports missing. Exiting")
        sys.exit(1)

    # produce reduced manifest hash
    doc = ET.fromstring(manifest)
    commit_id_hash = hashlib.sha1()
    for project in BENCHMARK_MANIFEST_PROJECT_LIST:
        project_element_list = doc.findall('.//project[@name="%s"]' % project)
        if project_element_list:
            for project_element in project_element_list:
                if project_element.tag == "project":
                    commit_id = project_element.get('revision')
                    commit_id_hash.update(commit_id)

    reduced_manifest = commit_id_hash.hexdigest()[:12]

    # endpoint comes in format team/project/build/environment
    # there should 'baseline' and 'patch' projects for each branch
    project_name = "%s" % (branch_name)
    if os.environ.get("SOURCE_GERRIT_CHANGE_NUMBER", "") is not None:
        project_name = project_name + "-patch"
    else:
        project_name = project_name + "-baseline"

    for file_path, file_tuple in files.items():
        file_name, file_contents = file_tuple
        if file_path.endswith(".json"):
            job_id = file_path.replace(".json","")
            qa_reports_endpoint = "/api/submit/art/%s/%s/%s" % (project_name, reduced_manifest, job_id)
            squad_params = StringIO()
            params.update({"job_id": job_id})
            json.dump(params, squad_params)
            # repack the result file to SQUAD friendly format
            metrics = StringIO()
            file_contents.seek(0)
            metrics_data_raw = json.load(file_contents)
            metrics_data = metrics_data_raw['benchmarks']
            transcode_results_dict(metrics_data, metrics_data_raw['compilation statistics'])
            json.dump(metrics_data, metrics)
            result_files = {
                "metadata": ("metadata.json", squad_params),
                "attachment": [("pinned-manifest.xml", StringIO(manifest)), (file_path, file_contents)],
                "metrics": ("results.json", metrics)
            }
            _push_object(qa_reports_token, qa_reports_url, qa_reports_endpoint, {}, result_files)