summaryrefslogtreecommitdiff
path: root/t/test_functionality.py
blob: 75f0137e0b430ae4b60ed7715ba1b69ad8c2573f (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
import utils
import asyncio
import threading


class TerminateTaskGroup(Exception):
    '''Exception raised to terminate a task group.'''


class BasicUsage(utils.AergiaUnitTestCase):

    def test_asyncless(self):
        def a():
            x = 100
            while x > 0:
                x -= 1

        self.Aergia.start()
        a()
        self.Aergia.stop()

        samples = self.Aergia.get_samples()
        self.assertFalse(samples)

    def test_sequential_tasks(self):
        delay = 0.2
        num_times = 5

        async def b(tot, num):
            await asyncio.sleep(delay)
            return tot + num

        async def a():
            tot = 0
            for i in range(num_times):
                tot = await b(tot, i)
            assert tot == 10

        self.Aergia.start()
        asyncio.run(a())
        self.Aergia.stop()

        samples = self.Aergia.get_samples()

        self.assertFuncContains('b',
                                [self.expected_samples(delay * num_times)],
                                samples)

    def test_simultaneous_tasks(self):
        delay = 0.2
        async def b(): await asyncio.sleep(delay)
        async def a(): await asyncio.gather(b(), b(), b())

        self.Aergia.start()
        asyncio.run(a())
        self.Aergia.stop()

        samples = self.Aergia.get_samples()

        self.assertFuncContains('b', [self.expected_samples(delay * 3)],
                                samples)
        # the gather function is technically waiting for all tasks to finish.
        # This might be seen as unintuitive though I don't want to bias the
        # results by adding logic to add artificial consistency.
        # profiling does not mean obscuring implementation details.
        self.assertFuncContains('a', [self.expected_samples(delay)], samples)

    def test_subthread_task(self):
        delay = 0.2

        async def b(): await asyncio.sleep(delay)
        async def a(): await asyncio.gather(b(), b(), b())
        def c(): asyncio.run(a())

        self.Aergia.start()
        x = threading.Thread(target=c)
        x.start()
        x.join()
        self.Aergia.stop()

        samples = self.Aergia.get_samples()
        self.assertFuncContains('c', [], samples)
        self.assertFuncContains('b', [self.expected_samples(delay * 3)],
                                samples)
        # see comment on `test_simultaneous_tasks'.
        self.assertFuncContains('a', [self.expected_samples(delay)], samples)

    def test_eager_task(self):
        delay = 0.2

        async def a():
            proc = await asyncio.create_subprocess_shell(f'sleep {delay}')
            await proc.communicate()

        self.Aergia.start()
        asyncio.run(a())
        self.Aergia.stop()

        samples = self.Aergia.get_samples()
        self.assertFuncContains('a', [self.expected_samples(delay)], samples)

    def test_async_eager_scheduled_woven(self):
        d1 = 0.2
        d2 = 0.3
        d3 = 0.2
        d4 = 0.1

        async def a():
            await asyncio.sleep(d1)
            proc = await asyncio.create_subprocess_shell(f'sleep {d2}')
            await proc.communicate()
            await asyncio.sleep(d3)
            proc = await asyncio.create_subprocess_shell(f'sleep {d4}')
            await proc.communicate()

        self.Aergia.start()
        asyncio.run(a())
        self.Aergia.stop()

        samples = self.Aergia.get_samples()
        self.assertFuncContains('a', [self.expected_samples(d1),
                                      self.expected_samples(d2),
                                      self.expected_samples(d3),
                                      self.expected_samples(d4),
                                      ], samples)

    def test_async_generator(self):
        delay = 0.2
        num_times = 10

        async def b():
            for i in range(num_times):
                await asyncio.sleep(delay)
                yield i

        async def a():
            lst = []
            async for item in b():
                lst.append(item)

        self.Aergia.start()
        asyncio.run(a())
        self.Aergia.stop()

        samples = self.Aergia.get_samples()
        # TODO I do not think async generators report correctly.
        # also does not work at all with yappi.
        # doing so would be unique to this profiler.

        # self.assertFuncContains('b',
        #                         [self.expected_samples(delay * num_times)],
        #                         samples)
        # self.assertFuncContains('a', [], samples)

        self.assertFuncContains('a',
                                [self.expected_samples(delay * num_times)],
                                samples)
        self.assertFuncContains('b', [], samples)

    def test_async_gen_and_comp(self):
        delay = 0.2
        num_times = 10

        async def b():
            for i in range(num_times):
                await asyncio.sleep(delay)
                yield i

        async def a():
            return [r async for r in b()]

        self.Aergia.start()
        asyncio.run(a())
        self.Aergia.stop()

        samples = self.Aergia.get_samples()
        # TODO I do not think async generators report correctly.
        # also does not work at all with yappi.
        # doing so would be unique to this profiler.

        self.assertFuncContains('b', [], samples)
        self.assertFuncContains('a', [], samples)
        self.assertFuncContains('<listcomp>',
                                [self.expected_samples(delay * num_times)],
                                samples)

    def test_deep_await(self):
        delay = 0.2

        async def c(): await asyncio.sleep(delay)
        async def b(): await c()
        async def a(): await b()

        self.Aergia.start()
        asyncio.run(a())
        self.Aergia.stop()

        samples = self.Aergia.get_samples()

        self.assertFuncContains('c', [self.expected_samples(delay)], samples)
        self.assertFuncContains('b', [], samples)
        self.assertFuncContains('a', [], samples)

    def test_task_groups(self):
        d1 = 0.2
        d2 = 0.3
        d3 = 0.1
        async def d(): await asyncio.sleep(d1)
        async def c(): await asyncio.sleep(d2)
        async def b(): await asyncio.sleep(d3)

        async def a():
            async with asyncio.TaskGroup() as tg:
                tg.create_task(d())
                tg.create_task(c())
                tg.create_task(b())

        self.Aergia.start()
        asyncio.run(a())
        self.Aergia.stop()

        samples = self.Aergia.get_samples()

        self.assertFuncContains('d', [self.expected_samples(d1)], samples)
        self.assertFuncContains('c', [self.expected_samples(d2)], samples)
        self.assertFuncContains('b', [self.expected_samples(d3)], samples)
        # the task group is technically waiting for all tasks to finish.
        # same as task.gather
        # This might be seen as unintuitive, (especially considering how
        # the next test works), though I don't want to bias the results
        # by adding logic to add artificial consistency.
        # Both are reporting correctly.
        self.assertFuncContains('a', [self.expected_samples(d2)], samples)

    def test_task_groups_cancel(self):
        d1 = 0.1
        d2 = 0.3
        d3 = 0.2
        async def d(): raise TerminateTaskGroup

        async def c(): await asyncio.sleep(d1)

        async def b(): await asyncio.sleep(d2)

        async def a():
            try:
                async with asyncio.TaskGroup() as tg:
                    tg.create_task(c())
                    tg.create_task(b())
                    await asyncio.sleep(d3)
                    tg.create_task(d())
            except* TerminateTaskGroup:
                pass

        self.Aergia.start()
        asyncio.run(a())
        self.Aergia.stop()

        samples = self.Aergia.get_samples()

        self.assertFuncContains('d', [], samples)
        self.assertFuncContains('c', [self.expected_samples(d1)], samples)
        self.assertFuncContains('b', [self.expected_samples(d3)], samples)
        # this time is attached to the sleep call itself. Aergia's print
        # function would confirm this!
        self.assertFuncContains('a', [self.expected_samples(d3)], samples)