aboutsummaryrefslogtreecommitdiffstats
path: root/etc/ci/performance/submit_to_perfherder.py
blob: e8f0f267c15f1b60760a7e992acf8de83474c70a (plain) (blame)
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python3

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import argparse
from functools import partial, reduce
import json
import operator
import os
import random
import string
from thclient import (TreeherderClient, TreeherderResultSetCollection,
                      TreeherderJobCollection)
import time

from runner import format_result_summary


def geometric_mean(iterable):
        filtered = list(filter(lambda x: x > 0, iterable))
        return (reduce(operator.mul, filtered)) ** (1.0 / len(filtered))


def format_testcase_name(name):
    temp = name.replace('http://localhost:8000/page_load_test/', '')
    temp = temp.replace('http://localhost:8000/tp6/', '')
    temp = temp.split('/')[0]
    temp = temp[0:80]
    return temp


def format_perf_data(perf_json, engine='servo'):
    suites = []
    measurement = "domComplete"  # Change this to an array when we have more

    def get_time_from_nav_start(timings, measurement):
        return timings[measurement] - timings['navigationStart']

    measurementFromNavStart = partial(get_time_from_nav_start,
                                      measurement=measurement)

    if (engine == 'gecko'):
        name = 'gecko.{}'.format(measurement)
    else:
        name = measurement

    suite = {
        "name": name,
        "value": geometric_mean(map(measurementFromNavStart, perf_json)),
        "subtests": []
    }
    for testcase in perf_json:
        if measurementFromNavStart(testcase) < 0:
            value = -1
            # print('Error: test case has negative timing. Test timeout?')
        else:
            value = measurementFromNavStart(testcase)

        suite["subtests"].append({
            "name": format_testcase_name(testcase["testcase"]),
            "value": value
        })

    suites.append(suite)

    return {
        "performance_data": {
            # https://bugzilla.mozilla.org/show_bug.cgi?id=1271472
            "framework": {"name": "servo-perf"},
            "suites": suites
        }
    }


def create_resultset_collection(dataset):
    print("[DEBUG] ResultSet Collection:")
    print(dataset)
    trsc = TreeherderResultSetCollection()

    for data in dataset:
        trs = trsc.get_resultset()

        trs.add_push_timestamp(data['push_timestamp'])
        trs.add_revision(data['revision'])
        trs.add_author(data['author'])
        # TODO: figure out where type is used
        # trs.add_type(data['type'])

        revisions = []
        for rev in data['revisions']:
            tr = trs.get_revision()

            tr.add_revision(rev['revision'])
            tr.add_author(rev['author'])
            tr.add_comment(rev['comment'])
            tr.add_repository(rev['repository'])

            revisions.append(tr)
        trs.add_revisions(revisions)

        trsc.add(trs)

    return trsc


def create_job_collection(dataset):
    print("[DEBUG] Job Collection:")
    print(dataset)

    tjc = TreeherderJobCollection()

    for data in dataset:
        tj = tjc.get_job()

        tj.add_revision(data['revision'])
        tj.add_project(data['project'])
        tj.add_coalesced_guid(data['job']['coalesced'])
        tj.add_job_guid(data['job']['job_guid'])
        tj.add_job_name(data['job']['name'])
        tj.add_job_symbol(data['job']['job_symbol'])
        tj.add_group_name(data['job']['group_name'])
        tj.add_group_symbol(data['job']['group_symbol'])
        tj.add_description(data['job']['desc'])
        tj.add_product_name(data['job']['product_name'])
        tj.add_state(data['job']['state'])
        tj.add_result(data['job']['result'])
        tj.add_reason(data['job']['reason'])
        tj.add_who(data['job']['who'])
        tj.add_tier(data['job']['tier'])
        tj.add_submit_timestamp(data['job']['submit_timestamp'])
        tj.add_start_timestamp(data['job']['start_timestamp'])
        tj.add_end_timestamp(data['job']['end_timestamp'])
        tj.add_machine(data['job']['machine'])

        tj.add_build_info(
            data['job']['build_platform']['os_name'],
            data['job']['build_platform']['platform'],
            data['job']['build_platform']['architecture']
        )

        tj.add_machine_info(
            data['job']['machine_platform']['os_name'],
            data['job']['machine_platform']['platform'],
            data['job']['machine_platform']['architecture']
        )

        tj.add_option_collection(data['job']['option_collection'])

        for artifact_data in data['job']['artifacts']:
            tj.add_artifact(
                artifact_data['name'],
                artifact_data['type'],
                artifact_data['blob']
            )
        tjc.add(tj)

        return tjc


# TODO: refactor this big function to smaller chunks
def submit(perf_data, failures, revision, summary, engine):

    print("[DEBUG] failures:")
    print(list(map(lambda x: x['testcase'], failures)))

    author = "{} <{}>".format(revision['author']['name'],
                              revision['author']['email'])

    dataset = [
        {
            # The top-most revision in the list of commits for a push.
            'revision': revision['commit'],
            'author': author,
            'push_timestamp': int(revision['author']['timestamp']),
            'type': 'push',
            # a list of revisions associated with the resultset. There should
            # be at least one.
            'revisions': [
                {
                    'comment': revision['subject'],
                    'revision': revision['commit'],
                    'repository': 'servo',
                    'author': author
                }
            ]
        }
    ]

    trsc = create_resultset_collection(dataset)

    result = "success"
    # TODO: verify a failed test won't affect Perfherder visualization
    # if len(failures) > 0:
    #     result = "testfailed"

    hashlen = len(revision['commit'])
    job_guid = ''.join(
        random.choice(string.ascii_letters + string.digits) for i in range(hashlen)
    )

    if (engine == "gecko"):
        project = "servo"
        job_symbol = 'PLG'
        group_symbol = 'SPG'
        group_name = 'Servo Perf on Gecko'
    else:
        project = "servo"
        job_symbol = 'PL'
        group_symbol = 'SP'
        group_name = 'Servo Perf'

    dataset = [
        {
            'project': project,
            'revision': revision['commit'],
            'job': {
                'job_guid': job_guid,
                'product_name': project,
                'reason': 'scheduler',
                # TODO: What is `who` for?
                'who': 'Servo',
                'desc': 'Servo Page Load Time Tests',
                'name': 'Servo Page Load Time',
                # The symbol representing the job displayed in
                # treeherder.allizom.org
                'job_symbol': job_symbol,

                # The symbol representing the job group in
                # treeherder.allizom.org
                'group_symbol': group_symbol,
                'group_name': group_name,

                # TODO: get the real timing from the test runner
                'submit_timestamp': str(int(time.time())),
                'start_timestamp': str(int(time.time())),
                'end_timestamp': str(int(time.time())),

                'state': 'completed',
                'result': result,  # "success" or "testfailed"

                'machine': 'local-machine',
                # TODO: read platform from test result
                'build_platform': {
                    'platform': 'linux64',
                    'os_name': 'linux',
                    'architecture': 'x86_64'
                },
                'machine_platform': {
                    'platform': 'linux64',
                    'os_name': 'linux',
                    'architecture': 'x86_64'
                },

                'option_collection': {'opt': True},

                # jobs can belong to different tiers
                # setting the tier here will determine which tier the job
                # belongs to.  However, if a job is set as Tier of 1, but
                # belongs to the Tier 2 profile on the server, it will still
                # be saved as Tier 2.
                'tier': 1,

                # the ``name`` of the log can be the default of "buildbot_text"
                # however, you can use a custom name.  See below.
                # TODO: point this to the log when we have them uploaded to S3
                'log_references': [
                    {
                        'url': 'TBD',
                        'name': 'test log'
                    }
                ],
                # The artifact can contain any kind of structured data
                # associated with a test.
                'artifacts': [
                    {
                        'type': 'json',
                        'name': 'performance_data',
                        # TODO: include the job_guid when the runner actually
                        # generates one
                        # 'job_guid': job_guid,
                        'blob': perf_data
                    },
                    {
                        'type': 'json',
                        'name': 'Job Info',
                        # 'job_guid': job_guid,
                        "blob": {
                            "job_details": [
                                {
                                    "content_type": "link",
                                    "url": "https://www.github.com/servo/servo",
                                    "value": "GitHub",
                                    "title": "Source code"
                                },
                                {
                                    "content_type": "raw_html",
                                    "title": "Result Summary",
                                    "value": summary
                                }
                            ]
                        }
                    }
                ],
                # List of job guids that were coalesced to this job
                'coalesced': []
            }
        }
    ]

    tjc = create_job_collection(dataset)

    # TODO: extract this read credential code out of this function.
    cred = {
        'client_id': os.environ['TREEHERDER_CLIENT_ID'],
        'secret': os.environ['TREEHERDER_CLIENT_SECRET']
    }

    client = TreeherderClient(protocol='https',
                              host='treeherder.allizom.org',
                              client_id=cred['client_id'],
                              secret=cred['secret'])

    # data structure validation is automatically performed here, if validation
    # fails a TreeherderClientError is raised
    client.post_collection('servo', trsc)
    client.post_collection('servo', tjc)


def main():
    parser = argparse.ArgumentParser(
        description=("Submit Servo performance data to Perfherder. "
                     "Remember to set your Treeherder credentail as environment"
                     " variable \'TREEHERDER_CLIENT_ID\' and "
                     "\'TREEHERDER_CLIENT_SECRET\'"))
    parser.add_argument("perf_json",
                        help="the output json from runner")
    parser.add_argument("revision_json",
                        help="the json containing the servo revision data")
    parser.add_argument("--engine",
                        type=str,
                        default='servo',
                        help=("The engine to run the tests on. Currently only"
                              " servo and gecko are supported."))
    args = parser.parse_args()

    with open(args.perf_json, 'r') as f:
        result_json = json.load(f)

    with open(args.revision_json, 'r') as f:
        revision = json.load(f)

    perf_data = format_perf_data(result_json, args.engine)
    failures = list(filter(lambda x: x['domComplete'] == -1, result_json))
    summary = format_result_summary(result_json).replace('\n', '<br/>')

    submit(perf_data, failures, revision, summary, args.engine)
    print("Done!")


if __name__ == "__main__":
    main()