File: test_fork.py

package info (click to toggle)
austin 3.7.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,444 kB
  • sloc: ansic: 8,622; python: 2,669; sh: 106; makefile: 54
file content (249 lines) | stat: -rw-r--r-- 7,728 bytes parent folder | download | duplicates (2)
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
# This file is part of "austin" which is released under GPL.
#
# See file LICENCE or go to http://www.gnu.org/licenses/ for full license
# details.
#
# Austin is a Python frame stack sampler for CPython.
#
# Copyright (c) 2022 Gabriele N. Tornetta <phoenix1987@gmail.com>.
# All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that 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.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import platform
from pathlib import Path
from test.utils import allpythons
from test.utils import austin
from test.utils import compress
from test.utils import demojo
from test.utils import has_pattern
from test.utils import maps
from test.utils import metadata
from test.utils import mojo
from test.utils import processes
from test.utils import python
from test.utils import samples
from test.utils import sum_metric
from test.utils import sum_metrics
from test.utils import target
from test.utils import threads
from test.utils import variants

import pytest


@pytest.mark.parametrize("heap", [tuple(), ("-h", "0"), ("-h", "64")])
@allpythons()
@variants
@mojo
def test_fork_wall_time(austin, py, heap, mojo):
    result = austin("-i", "2ms", *heap, *python(py), target("target34.py"), mojo=mojo)
    assert py in (result.stderr or result.stdout), result.stderr or result.stdout

    assert len(processes(result.stdout)) == 1, compress(result.stdout)
    ts = threads(result.stdout)
    assert len(ts) == 2, compress(result.stdout)
    assert all(len(t[1].split(":")) == 2 for t in ts), "threads have interpreter ID"

    assert has_pattern(result.stdout, "target34.py:keep_cpu_busy:3"), compress(
        result.stdout
    )
    assert not has_pattern(result.stdout, "Unwanted")

    meta = metadata(result.stdout)

    assert meta["mode"] == "wall"

    a = sum_metric(result.stdout)
    d = int(meta["duration"])

    assert 0 < a < 2.1 * d

    if austin == "austinp":
        ms = maps(result.stdout)
        assert len(ms) >= 2, ms
        assert [_ for _ in ms if "python" in _], ms


@pytest.mark.parametrize("heap", [tuple(), ("-h", "0"), ("-h", "64")])
@allpythons()
@variants
@mojo
def test_fork_cpu_time_cpu_bound(py, heap, austin, mojo):
    result = austin("-si", "1ms", *heap, *python(py), target("target34.py"), mojo=mojo)
    assert result.returncode == 0, result.stderr or result.stdout

    assert has_pattern(result.stdout, "target34.py:keep_cpu_busy:3"), compress(
        result.stdout
    )
    assert not has_pattern(result.stdout, "Unwanted")

    meta = metadata(result.stdout)

    assert meta["mode"] == "cpu"

    a = sum_metric(result.stdout)
    d = int(meta["duration"])

    assert 0 < a < 2.1 * d


@allpythons()
@variants
def test_fork_cpu_time_idle(py, austin):
    result = austin("-si", "1ms", *python(py), target("sleepy.py"))
    assert result.returncode == 0, result.stderr or result.stdout

    assert has_pattern(result.stdout, "sleepy.py:<module>:"), compress(result.stdout)

    meta = metadata(result.stdout)

    a = sum_metric(result.stdout)
    d = int(meta["duration"])

    assert a < 1.1 * d


@allpythons()
@mojo
def test_fork_memory(py, mojo):
    result = austin("-mi", "1ms", *python(py), target("target34.py"), mojo=mojo)
    assert result.returncode == 0, result.stderr or result.stdout

    assert has_pattern(result.stdout, "target34.py:keep_cpu_busy:32")

    meta = metadata(result.stdout)

    assert meta["mode"] == "memory"

    d = int(meta["duration"])
    assert d > 100000

    ms = [int(_.rpartition(" ")[-1]) for _ in samples(result.stdout)]
    alloc = sum(_ for _ in ms if _ > 0)
    dealloc = sum(-_ for _ in ms if _ < 0)

    assert alloc * dealloc


@allpythons()
@mojo
def test_fork_output(py, tmp_path: Path, mojo):
    datafile = tmp_path / "test_fork_output.austin"

    result = austin(
        "-i", "1ms", "-o", str(datafile), *python(py), target("target34.py"), mojo=mojo
    )
    assert result.returncode == 0, result.stderr or result.stdout

    assert "Unwanted" in result.stdout

    data = demojo(datafile.read_bytes()) if mojo else datafile.read_text()

    assert has_pattern(data, "target34.py:keep_cpu_busy:32")

    meta = metadata(data)

    assert meta["mode"] == "wall"

    a = sum(int(_.rpartition(" ")[-1]) for _ in samples(data))
    d = int(meta["duration"])

    assert 0 < 0.9 * d < a < 2.1 * d


# Support for multiprocess is attach-like and seems to suffer from the same
# issues as attach tests on Windows.
@pytest.mark.xfail(platform.system() == "Windows", reason="Does not pass in Windows CI")
@allpythons()
@mojo
def test_fork_multiprocess(py, mojo):
    result = austin("-Ci", "1ms", *python(py), target("target_mp.py"), mojo=mojo)
    assert result.returncode == 0, result.stderr or result.stdout

    ps = processes(result.stdout)
    assert len(ps) >= 3, ps

    meta = metadata(result.stdout)
    assert meta["multiprocess"] == "on", meta
    assert meta["mode"] == "wall", meta

    assert has_pattern(result.stdout, "target_mp.py:do:"), compress(result.stdout)
    assert has_pattern(result.stdout, "target_mp.py:fact:31 "), compress(result.stdout)


@allpythons()
@mojo
def test_fork_full_metrics(py, mojo):
    result = austin("-i", "10ms", "-f", *python(py), target("target34.py"), mojo=mojo)
    assert py in (result.stderr or result.stdout), result.stderr or result.stdout

    assert len(processes(result.stdout)) == 1
    ts = threads(result.stdout)
    assert len(ts) == 2, ts

    assert has_pattern(result.stdout, "target34.py:keep_cpu_busy:32")
    assert not has_pattern(result.stdout, "Unwanted")

    meta = metadata(result.stdout)

    assert meta["mode"] == "full"

    wall, cpu, alloc, dealloc = sum_metrics(result.stdout)
    d = int(meta["duration"])

    assert 0 < 0.9 * d < wall < 2.1 * d
    assert 0 < cpu <= wall
    assert alloc * dealloc


@pytest.mark.parametrize("exposure", [1, 2])
@allpythons()
def test_fork_exposure(py, exposure):
    result = austin(
        "-i", "1ms", "-x", str(exposure), *python(py), target("sleepy.py"), "1"
    )
    assert result.returncode == 0, result.stderr or result.stdout

    assert has_pattern(result.stdout, "sleepy.py:<module>:"), compress(result.stdout)

    meta = metadata(result.stdout)

    assert meta["mode"] == "wall"

    d = int(meta["duration"])
    assert 900000 * exposure < d < 1100000 * exposure


@variants
@allpythons(min=(3, 11))
def test_qualnames(py, austin):
    result = austin("-i", "1ms", *python(py), target("qualnames.py"))
    assert py in (result.stderr or result.stdout), result.stderr or result.stdout

    assert len(processes(result.stdout)) == 1, compress(result.stdout)
    ts = threads(result.stdout)
    assert len(ts) == 1, compress(result.stdout)

    assert has_pattern(result.stdout, "qualnames.py:Foo.run"), compress(result.stdout)
    assert has_pattern(result.stdout, "qualnames.py:Bar.run"), compress(result.stdout)


@allpythons()
def test_no_logging(py, monkeypatch):
    monkeypatch.setenv("AUSTIN_NO_LOGGING", "1")
    result = austin("-i", "1ms", *python(py), target("target34.py"))
    assert has_pattern(result.stdout, "target34.py:keep_cpu_busy:3"), compress(
        result.stdout
    )
    assert result.returncode == 0, result.stderr or result.stdout