summaryrefslogtreecommitdiffstats
path: root/scripts/corebenchrunner/qt.py
blob: 56af71ecf4a9bb36fe326212f202a57e976bf617 (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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# Copyright (C) 2023 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
import asyncio
import decimal
import logging
import os
import shlex
import subprocess
from typing import List, Optional, Union

import common

CONFIGURE_TIMEOUT = 10 * 60
BUILD_TIMEOUT = 30 * 60
TEST_TIMEOUT = 15 * 60


class Module:
    def __init__(self, test_files: List["TestFile"]) -> None:
        self.test_files = test_files

    @staticmethod
    async def configure(
        build_directory: str, repository_directory: str, log_directory: str
    ) -> Optional[common.Error]:
        output_file = os.path.join(log_directory, "configure.log")

        error = await common.Command.run(
            arguments=[
                # Absolute path needed because we are changing the working directory.
                os.path.abspath(os.path.join(repository_directory, "configure")),
                "-release",
                "--",
                "-DQT_BUILD_BENCHMARKS=ON",
            ],
            output_file=output_file,
            timeout=CONFIGURE_TIMEOUT,
            cwd=build_directory,
        )
        if error:
            return error
        else:
            return None

    @staticmethod
    async def build(
        build_directory: str, log_directory: str, test_file: Optional[str], logger: logging.Logger
    ) -> Union["Module", common.Error]:
        target = test_file if test_file is not None else "tests/benchmarks/install/local"

        error = await common.Command.run(
            arguments=["cmake", "--build", ".", "--target", target],
            output_file=os.path.join(log_directory, "build.log"),
            timeout=BUILD_TIMEOUT,
            cwd=build_directory,
        )
        if error:
            return error

        logger.debug("Searching for test files")
        directory = os.path.join(build_directory, "tests", "benchmarks")
        test_files = Module.find_test_files(directory=directory, logger=logger)
        if not test_files:
            return common.Error(f"Found no test files in {directory}")
        else:
            return Module(test_files)

    @staticmethod
    def find_test_files(directory: str, logger: logging.Logger) -> List["TestFile"]:
        paths = []

        def report_error(error: OSError) -> None:
            logger.error(f"Error for {error.filename} while finding test cases")

        for parent, _, names in os.walk(top=directory, onerror=report_error):
            for name in names:
                path = os.path.join(parent, name)
                if name.startswith("tst_bench_") and os.access(path, os.X_OK):
                    paths.append(path)

        return [
            TestFile(directory=directory, relative_path=os.path.relpath(path, directory))
            for path in sorted(paths)
        ]


class TestFile:
    """
    A test file that runs benchmarks. It stores the benchmark results to a file.
    """

    def __init__(self, directory: str, relative_path: str) -> None:
        self.directory = directory
        self.relative_path = relative_path

    @property
    def name(self) -> str:
        return os.path.basename(self.relative_path)

    @property
    def absolute_path(self) -> str:
        return os.path.join(self.directory, self.relative_path)

    async def run(
        self,
        command_prefix: str,
        result_file: str,
        output_file: str,
        test_function: Optional[str],
        data_tag: Optional[str],
        logger: logging.Logger,
    ) -> Union["ResultFile", "TestFileIssue"]:
        # Build the command-line.
        arguments = [self.absolute_path, "-o", f"{result_file},xml"]
        if test_function:
            if data_tag:
                arguments.append(f"{test_function}:{data_tag}")
            else:
                arguments.append(f"{test_function}")
        command = command_prefix + " " + " ".join(map(shlex.quote, arguments))

        logger.debug(f'Running command "{command}"')
        with open(output_file, "w") as f:
            process = await asyncio.create_subprocess_shell(
                cmd=command,
                stdout=f,
                stderr=subprocess.STDOUT,
            )
        try:
            await asyncio.wait_for(process.wait(), timeout=TEST_TIMEOUT)
        except asyncio.TimeoutError:
            return TestFileIssue(
                test_file=self, description=f"Test timed out after {TEST_TIMEOUT} seconds"
            )

        if not os.path.exists(result_file):
            return TestFileIssue(
                test_file=self,
                description=f"Test exited with code {process.returncode} and no result file",
            )
        else:
            return ResultFile(test_file=self, path=result_file)


class TestFileIssue:
    """
    A problem that prevented us from obtaining benchmark results.

    These should be fixed.
    """

    def __init__(self, test_file: TestFile, description: str) -> None:
        self.test_file = test_file
        self.description = description


class ResultFile:
    """
    A file that contains benchmark results.
    """

    def __init__(self, test_file: TestFile, path: str) -> None:
        self.test_file = test_file
        self.path = path


class TestFileResult:
    """
    Benchmark results obtained from a result file.
    """

    def __init__(self, test_file: TestFile, test_case_result: "TestCaseResult") -> None:
        self.test_file = test_file
        self.test_case_result = test_case_result


class TestCaseResult:
    def __init__(
        self,
        name: str,
        duration: decimal.Decimal,
        test_function_results: List["TestFunctionResult"],
    ) -> None:
        self.name = name
        self.duration = duration
        self.test_function_results = test_function_results


class TestFunctionResult:
    def __init__(
        self,
        name: str,
        benchmark_results: List["BenchmarkResult"],
        incidents: List["Incident"],
        messages: List["Message"],
    ) -> None:
        self.name = name
        self.benchmark_results = benchmark_results
        self.incidents = incidents
        self.messages = messages


class BenchmarkResult:
    def __init__(
        self, data_tag: Optional[str], metric: str, iterations: int, value: decimal.Decimal
    ) -> None:
        self.data_tag = data_tag
        self.metric = metric
        self.iterations = iterations
        self.value = value


class Incident:
    def __init__(self, incident_type: str, data_tag: Optional[str]) -> None:
        self.incident_type = incident_type
        self.data_tag = data_tag


class Message:
    def __init__(self, message_type: str, data_tag: Optional[str], description: str) -> None:
        self.message_type = message_type
        self.data_tag = data_tag
        self.description = description


class ResultFileParser:
    @staticmethod
    def parse(result_file: ResultFile) -> Union[TestFileResult, TestFileIssue]:
        test_file = result_file.test_file
        test_case_result = ResultFileParser.parse_file(result_file.path)
        match test_case_result:
            case common.Error() as error:
                return TestFileIssue(
                    test_file=test_file,
                    description=f"Test result file is invalid: {error.message}",
                )

        return TestFileResult(test_file=test_file, test_case_result=test_case_result)

    @staticmethod
    def parse_file(file: str) -> Union[TestCaseResult, common.Error]:
        element = common.XmlParser.load(file=file, tag="TestCase")
        match element:
            case common.Error() as error:
                return error

        return ResultFileParser.parse_test_case_result(element)

    @staticmethod
    def parse_test_case_result(
        element: common.XmlParser,
    ) -> Union[TestCaseResult, common.Error]:
        name = element.string_attribute("name")
        match name:
            case common.Error() as error:
                return error

        child = element.child("Duration")
        match child:
            case common.Error() as error:
                return error

        duration = child.decimal_attribute("msecs")
        match duration:
            case common.Error() as error:
                return error

        test_function_results = []
        for child in element.children("TestFunction"):
            result = ResultFileParser.parse_test_function_result(child)
            match result:
                case common.Error() as error:
                    return error

            test_function_results.append(result)

        return TestCaseResult(
            name=name, duration=duration, test_function_results=test_function_results
        )

    @staticmethod
    def parse_test_function_result(
        element: common.XmlParser,
    ) -> Union[TestFunctionResult, common.Error]:
        name = element.string_attribute("name")
        match name:
            case common.Error() as error:
                return error

        messages = []
        for child in element.children("Message"):
            message = ResultFileParser.parse_message(child)
            match message:
                case common.Error() as error:
                    return error

            messages.append(message)

        incidents = []
        for child in element.children("Incident"):
            incident = ResultFileParser.parse_incident(child)
            match incident:
                case common.Error() as error:
                    return error

            incidents.append(incident)

        benchmark_results = []
        for child in element.children("BenchmarkResult"):
            benchmark_result = ResultFileParser.parse_benchmark_result(child)
            match benchmark_result:
                case common.Error() as error:
                    return error

            benchmark_results.append(benchmark_result)

        return TestFunctionResult(
            name=name, messages=messages, incidents=incidents, benchmark_results=benchmark_results
        )

    @staticmethod
    def parse_benchmark_result(
        element: common.XmlParser,
    ) -> Union[BenchmarkResult, common.Error]:
        tag = element.string_attribute("tag")
        match tag:
            case common.Error() as error:
                return error

        data_tag = tag if tag != "" else None

        metric = element.string_attribute("metric")
        match metric:
            case common.Error() as error:
                return error

        iterations = element.integer_attribute("iterations")
        match iterations:
            case common.Error() as error:
                return error

        value = element.decimal_attribute("value")
        match value:
            case common.Error() as error:
                return error

        return BenchmarkResult(data_tag=data_tag, metric=metric, iterations=iterations, value=value)

    @staticmethod
    def parse_incident(element: common.XmlParser) -> Union[Incident, common.Error]:
        incident_type = element.string_attribute("type")
        match incident_type:
            case common.Error() as error:
                return error

        children = element.children("DataTag")
        if len(children) == 0:
            data_tag = None
        elif len(children) == 1:
            data_tag = children[0].element.text
        else:
            return common.Error("Incident has multiple DataTag children")

        return Incident(incident_type=incident_type, data_tag=data_tag)

    @staticmethod
    def parse_message(element: common.XmlParser) -> Union[Message, common.Error]:
        message_type = element.string_attribute("type")
        match message_type:
            case common.Error() as error:
                return error

        children = element.children("DataTag")
        if len(children) == 0:
            data_tag = None
        elif len(children) == 1:
            data_tag = children[0].element.text
        else:
            return common.Error("Message has multiple DataTag children")

        child = element.child("Description")
        match child:
            case common.Error() as error:
                return error

        description = child.element.text
        if description is None:
            return common.Error("Message has no text")

        return Message(message_type=message_type, data_tag=data_tag, description=description)