summaryrefslogtreecommitdiff
path: root/t/test_manual.py
blob: ee3c95fd6b35698488a49ffc6560f4c13c0f90a6 (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
import unittest
import yappi
import utils
import asyncio
import tempfile
try:
    import aiofiles
    HAS_AIOFILES = True
except ImportError:
    HAS_AIOFILES = False
try:
    from aiohttp import web
    import aiohttp
    HAS_AIOHTTP = True
except ImportError:
    HAS_AIOHTTP = False

# A test file containing advanced asyncio tests which have to be verified
# manually.


class Manual(utils.AergiaUnitTestCase):

    @unittest.skipIf(not HAS_AIOHTTP,
                     "Skipping 'test_fetch' because aiohttp "
                     "could not be imported.")
    def test_fetch(self):

        async def handle(request):
            return web.Response(text="Hello, world")

        def get_runner():
            app = web.Application()
            app.router.add_get('/', handle)
            return web.AppRunner(app)

        async def start_server():
            await runner.setup()
            site = web.TCPSite(runner, 'localhost', 8080)
            await site.start()

        async def stop_server():
            await runner.cleanup()

        async def fetch(url):
            async with aiohttp.ClientSession() as session:
                async with session.get(url) as response:
                    return await response.text()

        async def query(urls):
            tasks = [fetch(url) for url in urls]
            return await asyncio.gather(*tasks)

        async def main():
            await start_server()
            # wait for server to start
            await asyncio.sleep(1)

            urls = ['http://localhost:8080'] * 150

            await query(urls)
            await stop_server()

        print('')
        runner = get_runner()
        yappi.start()
        asyncio.run(main())
        yappi.stop()
        self.yappi_print_traceable_results(yappi.get_func_stats())

        runner = get_runner()
        self.Aergia.start()
        asyncio.run(main())
        self.Aergia.print_samples()
        self.Aergia.stop()
        print('')

    @unittest.skipIf(not HAS_AIOHTTP,
                     "Skipping 'test_fetch_with_burn' because aiohttp "
                     "could not be imported.")
    def test_fetch_with_burn(self):

        async def handle(request):
            utils.burn_cpu(0.05)
            return web.Response(text="Hello, world")

        def get_runner():
            app = web.Application()
            app.router.add_get('/', handle)
            return web.AppRunner(app)

        async def start_server():
            await runner.setup()
            site = web.TCPSite(runner, 'localhost', 8080)
            await site.start()

        async def stop_server():
            await runner.cleanup()

        async def fetch(url):
            async with aiohttp.ClientSession() as session:
                async with session.get(url) as response:
                    return await response.text()

        async def query(urls):
            tasks = [fetch(url) for url in urls]
            return await asyncio.gather(*tasks)

        async def main():
            await start_server()
            # wait for server to start
            await asyncio.sleep(1)

            urls = ['http://localhost:8080'] * 150

            await query(urls)
            await stop_server()

        print('')
        runner = get_runner()
        yappi.start()
        asyncio.run(main())
        yappi.stop()
        self.yappi_print_traceable_results(yappi.get_func_stats())

        runner = get_runner()
        self.Aergia.start()
        asyncio.run(main())
        self.Aergia.print_samples()
        self.Aergia.stop()
        print('')

    @unittest.skipIf(not HAS_AIOFILES,
                     "Skipping 'test_file' because aiofiles "
                     "could not be imported.")
    def test_file(self):

        async def write_file(filename, content):
            async with aiofiles.open(filename, mode='w') as f:
                await f.write(content)

        async def read_file(filename):
            async with aiofiles.open(filename, mode='r') as f:
                content = await f.read()
                return content

        async def read_lots(filename, iterations):
            tasks = [read_file(filename) for _ in range(iterations)]
            await asyncio.gather(*tasks)

        async def main():
            # Create a temp file (/tmp)
            with tempfile.NamedTemporaryFile(delete=True, suffix='.txt') as temp_file:
                filename = temp_file.name
                content = 'Hello, World!'

                # write the file
                await write_file(filename, content)

                # read the file 200 times
                await read_lots(filename, 200)

        print('')
        yappi.start()
        asyncio.run(main())
        yappi.stop()
        self.yappi_print_traceable_results(yappi.get_func_stats())

        self.Aergia.start()
        asyncio.run(main())
        self.Aergia.print_samples()
        self.Aergia.stop()
        print('')

    @unittest.skipIf(not HAS_AIOFILES,
                     "Skipping 'test_file_with_burn' because aiofiles "
                     "could not be imported.")
    def test_file_with_burn(self):

        async def write_file(filename, content):
            async with aiofiles.open(filename, mode='w') as f:
                await f.write(content)

        async def read_file(filename):
            utils.burn_cpu(0.05)
            async with aiofiles.open(filename, mode='r') as f:
                content = await f.read()
                return content

        async def read_lots(filename, iterations):
            tasks = [read_file(filename) for _ in range(iterations)]
            await asyncio.gather(*tasks)

        async def main():
            # Create a temp file (/tmp)
            with tempfile.NamedTemporaryFile(delete=False, suffix='.txt') as temp_file:
                filename = temp_file.name
                content = 'Hello, World!'

                # write the file
                await write_file(filename, content)

                # read the file 200 times
                await read_lots(filename, 200)

        print('')
        yappi.start()
        asyncio.run(main())
        yappi.stop()
        self.yappi_print_traceable_results(yappi.get_func_stats())

        self.Aergia.start()
        asyncio.run(main())
        self.Aergia.print_samples()
        self.Aergia.stop()
        print('')