summaryrefslogtreecommitdiff
path: root/tests/benchmark/test_baseline_benchmarks.py
blob: 42521b5932a73dc777ff494efafe3908ce8ba0e4 (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
"""Profiles basic -jX functionality."""

# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

# pylint: disable=missing-function-docstring

import os
import pprint
import time
from unittest.mock import patch

import pytest
from astroid import nodes
from pytest_benchmark.fixture import BenchmarkFixture

from pylint.checkers import BaseRawFileChecker
from pylint.lint import PyLinter, check_parallel
from pylint.testutils import GenericTestReporter as Reporter
from pylint.testutils._run import _Run as Run
from pylint.typing import FileItem
from pylint.utils import register_plugins


def _empty_filepath() -> str:
    return os.path.abspath(
        os.path.join(
            os.path.dirname(__file__), "..", "input", "benchmark_minimal_file.py"
        )
    )


class SleepingChecker(BaseRawFileChecker):
    """A checker that sleeps, the wall-clock time should reduce as we add workers.

    As we apply a roughly constant amount of "work" in this checker any variance is
    likely to be caused by the pylint system.
    """

    name = "sleeper"
    msgs = {
        "R9999": (
            "Test",
            "test-check",
            "Some helpful text.",
        )
    }
    sleep_duration = 0.5  # the time to pretend we're doing work for

    def process_module(self, node: nodes.Module) -> None:
        """Sleeps for `sleep_duration` on each call.

        This effectively means each file costs ~`sleep_duration`+framework overhead
        """
        time.sleep(self.sleep_duration)


class SleepingCheckerLong(BaseRawFileChecker):
    """A checker that sleeps, the wall-clock time should reduce as we add workers.

    As we apply a roughly constant amount of "work" in this checker any variance is
    likely to be caused by the pylint system.
    """

    name = "long-sleeper"
    msgs = {
        "R9999": (
            "Test",
            "test-check",
            "Some helpful text.",
        )
    }
    sleep_duration = 0.5  # the time to pretend we're doing work for

    def process_module(self, node: nodes.Module) -> None:
        """Sleeps for `sleep_duration` on each call.

        This effectively means each file costs ~`sleep_duration`+framework overhead
        """
        time.sleep(self.sleep_duration)


class NoWorkChecker(BaseRawFileChecker):
    """A checker that sleeps, the wall-clock time should change as we add threads."""

    name = "sleeper"
    msgs = {
        "R9999": (
            "Test",
            "test-check",
            "Some helpful text.",
        )
    }

    def process_module(self, node: nodes.Module) -> None:
        pass


@pytest.mark.benchmark(
    group="baseline",
)
class TestEstablishBaselineBenchmarks:
    """Naive benchmarks for the high-level pylint framework.

    Because this benchmarks the fundamental and common parts and changes seen here will
    impact everything else
    """

    empty_filepath = _empty_filepath()
    empty_file_info = FileItem(
        "name-emptyfile-file",
        _empty_filepath(),
        "modname-emptyfile-mod",
    )
    lot_of_files = 500

    def test_baseline_benchmark_j1(self, benchmark: BenchmarkFixture) -> None:
        """Establish a baseline of pylint performance with no work.

        We will add extra Checkers in other benchmarks.

        Because this is so simple, if this regresses something very serious has happened
        """
        linter = PyLinter(reporter=Reporter())
        fileinfos = [self.empty_filepath]  # Single file to end-to-end the system
        assert linter.config.jobs == 1
        assert len(linter._checkers) == 1, "Should just have 'main'"
        benchmark(linter.check, fileinfos)
        assert (
            linter.msg_status == 0
        ), f"Expected no errors to be thrown: {pprint.pformat(linter.reporter.messages)}"

    @pytest.mark.needs_two_cores
    def test_baseline_benchmark_j2(self, benchmark: BenchmarkFixture) -> None:
        """Establish a baseline of pylint performance with no work across threads.

        Same as `test_baseline_benchmark_j1` but we use -j2 with 2 fake files to
        ensure end-to-end-system invoked.

        Because this is also so simple, if this regresses something very serious has
        happened.
        """
        linter = PyLinter(reporter=Reporter())
        linter.config.jobs = 2

        # Create file per worker, using all workers
        fileinfos = [self.empty_filepath for _ in range(linter.config.jobs)]

        assert linter.config.jobs == 2
        assert len(linter._checkers) == 1, "Should have 'main'"
        benchmark(linter.check, fileinfos)
        assert (
            linter.msg_status == 0
        ), f"Expected no errors to be thrown: {pprint.pformat(linter.reporter.messages)}"

    @pytest.mark.needs_two_cores
    def test_baseline_benchmark_check_parallel_j2(
        self, benchmark: BenchmarkFixture
    ) -> None:
        """Should demonstrate times very close to `test_baseline_benchmark_j2`."""
        linter = PyLinter(reporter=Reporter())

        # Create file per worker, using all workers
        fileinfos = [self.empty_file_info for _ in range(linter.config.jobs)]

        assert len(linter._checkers) == 1, "Should have 'main'"
        benchmark(check_parallel, linter, jobs=2, files=fileinfos)
        assert (
            linter.msg_status == 0
        ), f"Expected no errors to be thrown: {pprint.pformat(linter.reporter.messages)}"

    def test_baseline_lots_of_files_j1(self, benchmark: BenchmarkFixture) -> None:
        """Establish a baseline with only 'main' checker being run in -j1.

        We do not register any checkers except the default 'main', so the cost is just
        that of the system with a lot of files registered
        """
        if benchmark.disabled:
            benchmark(print, "skipping, only benchmark large file counts")
            return  # _only_ run this test is profiling
        linter = PyLinter(reporter=Reporter())
        linter.config.jobs = 1
        fileinfos = [self.empty_filepath for _ in range(self.lot_of_files)]
        assert linter.config.jobs == 1
        assert len(linter._checkers) == 1, "Should have 'main'"
        benchmark(linter.check, fileinfos)
        assert (
            linter.msg_status == 0
        ), f"Expected no errors to be thrown: {pprint.pformat(linter.reporter.messages)}"

    @pytest.mark.needs_two_cores
    def test_baseline_lots_of_files_j2(self, benchmark: BenchmarkFixture) -> None:
        """Establish a baseline with only 'main' checker being run in -j2.

        As with the -j1 variant above `test_baseline_lots_of_files_j1`, we do not
        register any checkers except the default 'main', so the cost is just that of
        the check_parallel system across 2 workers, plus the overhead of PyLinter
        """
        if benchmark.disabled:
            benchmark(print, "skipping, only benchmark large file counts")
            return  # _only_ run this test is profiling
        linter = PyLinter(reporter=Reporter())
        linter.config.jobs = 2
        fileinfos = [self.empty_filepath for _ in range(self.lot_of_files)]
        assert linter.config.jobs == 2
        assert len(linter._checkers) == 1, "Should have 'main'"
        benchmark(linter.check, fileinfos)
        assert (
            linter.msg_status == 0
        ), f"Expected no errors to be thrown: {pprint.pformat(linter.reporter.messages)}"

    def test_baseline_lots_of_files_j1_empty_checker(
        self, benchmark: BenchmarkFixture
    ) -> None:
        """Baselines pylint for a single extra checker being run in -j1, for N-files.

        We use a checker that does no work, so the cost is just that of the system at
        scale
        """
        if benchmark.disabled:
            benchmark(print, "skipping, only benchmark large file counts")
            return  # _only_ run this test is profiling
        linter = PyLinter(reporter=Reporter())
        linter.config.jobs = 1
        linter.register_checker(NoWorkChecker(linter))
        fileinfos = [self.empty_filepath for _ in range(self.lot_of_files)]
        assert linter.config.jobs == 1
        assert len(linter._checkers) == 2, "Should have 'main' and 'sleeper'"
        benchmark(linter.check, fileinfos)
        assert (
            linter.msg_status == 0
        ), f"Expected no errors to be thrown: {pprint.pformat(linter.reporter.messages)}"

    @pytest.mark.needs_two_cores
    def test_baseline_lots_of_files_j2_empty_checker(
        self, benchmark: BenchmarkFixture
    ) -> None:
        """Baselines pylint for a single extra checker being run in -j2, for N-files.

        We use a checker that does no work, so the cost is just that of the system at
        scale, across workers
        """
        if benchmark.disabled:
            benchmark(print, "skipping, only benchmark large file counts")
            return  # _only_ run this test is profiling
        linter = PyLinter(reporter=Reporter())
        linter.config.jobs = 2
        linter.register_checker(NoWorkChecker(linter))
        fileinfos = [self.empty_filepath for _ in range(self.lot_of_files)]
        assert linter.config.jobs == 2
        assert len(linter._checkers) == 2, "Should have 'main' and 'sleeper'"
        benchmark(linter.check, fileinfos)
        assert (
            linter.msg_status == 0
        ), f"Expected no errors to be thrown: {pprint.pformat(linter.reporter.messages)}"

    def test_baseline_benchmark_j1_single_working_checker(
        self, benchmark: BenchmarkFixture
    ) -> None:
        """Establish a baseline of single-worker performance for PyLinter.

        Here we mimic a single Checker that does some work so that we can see the
        impact of running a simple system with -j1 against the same system with -j2.

        We expect this benchmark to take very close to
        `numfiles*SleepingChecker.sleep_duration`
        """
        if benchmark.disabled:
            benchmark(print, "skipping, do not want to sleep in main tests")
            return  # _only_ run this test is profiling
        linter = PyLinter(reporter=Reporter())
        linter.register_checker(SleepingChecker(linter))

        # Check the same number of files as
        # `test_baseline_benchmark_j2_single_working_checker`
        fileinfos = [self.empty_filepath for _ in range(2)]

        assert linter.config.jobs == 1
        assert len(linter._checkers) == 2, "Should have 'main' and 'sleeper'"
        benchmark(linter.check, fileinfos)
        assert (
            linter.msg_status == 0
        ), f"Expected no errors to be thrown: {pprint.pformat(linter.reporter.messages)}"

    @pytest.mark.needs_two_cores
    def test_baseline_benchmark_j2_single_working_checker(
        self, benchmark: BenchmarkFixture
    ) -> None:
        """Establishes baseline of multi-worker performance for PyLinter/check_parallel.

        We expect this benchmark to take less time that test_baseline_benchmark_j1,
        `error_margin*(1/J)*(numfiles*SleepingChecker.sleep_duration)`

        Because of the cost of the framework and system the performance difference will
        *not* be 1/2 of -j1 versions.
        """
        if benchmark.disabled:
            benchmark(print, "skipping, do not want to sleep in main tests")
            return  # _only_ run this test is profiling
        linter = PyLinter(reporter=Reporter())
        linter.config.jobs = 2
        linter.register_checker(SleepingChecker(linter))

        # Check the same number of files as
        # `test_baseline_benchmark_j1_single_working_checker`
        fileinfos = [self.empty_filepath for _ in range(2)]

        assert linter.config.jobs == 2
        assert len(linter._checkers) == 2, "Should have 'main' and 'sleeper'"
        benchmark(linter.check, fileinfos)
        assert (
            linter.msg_status == 0
        ), f"Expected no errors to be thrown: {pprint.pformat(linter.reporter.messages)}"

    def test_baseline_benchmark_j1_all_checks_single_file(
        self, benchmark: BenchmarkFixture
    ) -> None:
        """Runs a single file, with -j1, against all checkers/Extensions."""
        args = [self.empty_filepath, "--enable=all", "--enable-all-extensions"]
        runner = benchmark(Run, args, reporter=Reporter(), exit=False)
        assert runner.linter.config.jobs == 1
        print("len(runner.linter._checkers)", len(runner.linter._checkers))
        assert len(runner.linter._checkers) > 1, "Should have more than 'main'"

        assert (
            runner.linter.msg_status == 0
        ), f"Expected no errors to be thrown: {pprint.pformat(runner.linter.reporter.messages)}"

    def test_baseline_benchmark_j1_all_checks_lots_of_files(
        self, benchmark: BenchmarkFixture
    ) -> None:
        """Runs lots of files, with -j1, against all plug-ins.

        ... that's the intent at least.
        """
        if benchmark.disabled:
            benchmark(print, "skipping, only benchmark large file counts")
            return  # _only_ run this test is profiling
        linter = PyLinter()

        # Register all checkers/extensions and enable them
        with patch("os.listdir", return_value=["pylint", "tests"]):
            register_plugins(
                linter,
                os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")),
            )
        linter.load_default_plugins()
        linter.enable("all")

        # Just 1 file, but all Checkers/Extensions
        fileinfos = [self.empty_filepath for _ in range(self.lot_of_files)]

        assert linter.config.jobs == 1
        print("len(linter._checkers)", len(linter._checkers))
        assert len(linter._checkers) > 1, "Should have more than 'main'"
        benchmark(linter.check, fileinfos)