-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.py
289 lines (232 loc) · 9.21 KB
/
main.py
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
import asyncio
import datetime
import hashlib
import random
import uuid
from collections import namedtuple
from typing import Callable, Coroutine, Any
import aiohttp
import psutil
import reactivex as rx
from reactivex import operators as ops
# Add >> to reactivex.Observable
rx.Observable.__rshift__ = lambda self, op: self.pipe(op)
# Note: Request creation code is intentionally not shared across scenarios
def scenario1(url: str) -> rx.Observable[str]:
async def _req():
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
def req(): return rx.from_future(asyncio.ensure_future(_req()))
return rx.merge(req(), req()) >> ops.first()
# Or:
# return rx.merge(req(), req()).pipe(ops.first())
def scenario2(url: str):
async def http_get():
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
def req(): return rx.from_future(asyncio.ensure_future(http_get())) >> ops.catch(rx.empty())
return rx.merge(req(), req()) >> ops.first()
# Or:
# def req(): return rx.from_future(asyncio.ensure_future(http_get())).pipe(ops.catch(rx.empty()))
#
# return rx.merge(req(), req()).pipe(ops.first())
def scenario3(url: str):
async def _req():
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
def req(): return rx.from_future(asyncio.ensure_future(_req()))
return rx.merge(*[req() for req in [req] * 10_000]) >> ops.first()
# Or:
# return rx.merge(*[req() for req in [req] * 10_000]).pipe(ops.first())
def scenario4(url: str):
async def _req():
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
def req(): return rx.from_future(asyncio.ensure_future(_req()))
return rx.merge(
req() >> ops.timeout(datetime.timedelta(seconds=1)) >> ops.catch(rx.empty()),
req()
) >> ops.first()
# Or:
# return rx.merge(
# req().pipe(
# ops.timeout(datetime.timedelta(seconds=1)),
# ops.catch(rx.empty())
# ),
# req()
# ).pipe(ops.first())
def scenario5(url: str):
async def _req():
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
response.raise_for_status()
return await response.text()
def req(): return rx.from_future(asyncio.ensure_future(_req())) >> ops.catch(rx.empty())
return rx.merge(req(), req()) >> ops.first()
# Or:
# def req(): return rx.from_future(asyncio.ensure_future(_req())).pipe(ops.catch(rx.empty()))
#
# return rx.merge(req(), req()).pipe(ops.first())
def scenario6(url: str):
async def _req():
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
response.raise_for_status()
return await response.text()
def req(): return rx.from_future(asyncio.ensure_future(_req())) >> ops.catch(rx.empty())
return rx.merge(req(), req(), req()) >> ops.first()
# Or:
# def req(): return rx.from_future(asyncio.ensure_future(_req())).pipe(ops.catch(rx.empty()))
#
# return rx.merge(req(), req(), req()).pipe(ops.first())
def scenario7(url: str) -> rx.Observable[str]:
async def _req():
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
def req(): return rx.from_future(asyncio.ensure_future(_req()))
hedge_req = rx.of(()) >> ops.delay(datetime.timedelta(seconds=3)) >> ops.flat_map(lambda _: req())
return rx.merge(req(), hedge_req) >> ops.first()
# Or:
# hedge_req = rx.of(()).pipe(
# ops.delay(datetime.timedelta(seconds=3)),
# ops.flat_map(lambda _: req())
# )
#
# return rx.merge(req(), hedge_req).pipe(ops.first())
def scenario8(base_url: str) -> rx.Observable[str]:
async def _req(url: str):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
response.raise_for_status()
return await response.text()
def open_req(): return rx.from_future(asyncio.ensure_future(_req(f"{base_url}?open")))
def use_req(res: str): return rx.from_future(asyncio.ensure_future(_req(f"{base_url}?use={res}")))
def close_req(res: str): return rx.from_future(asyncio.ensure_future(_req(f"{base_url}?close={res}")))
def res_req():
return open_req() >> ops.flat_map(
lambda res: use_req(res) >> ops.catch(rx.of(None)) >> ops.flat_map(
lambda result: close_req(res) >> ops.flat_map(
lambda _: rx.empty() if result is None else rx.of(result)
)
)
)
return rx.merge(res_req(), res_req()) >> ops.first()
# Or:
# def res_req():
# return open_req().pipe(
# ops.flat_map(
# lambda res: use_req(res).pipe(
# ops.catch(rx.of(None)),
# ops.flat_map(
# lambda result: close_req(res).pipe(
# ops.flat_map(
# lambda _: rx.empty() if result is None else rx.of(result)
# )
# )
# )
# )
# )
# )
#
# return rx.merge(res_req(), res_req()).pipe(ops.first())
def scenario9(url: str):
async def _req():
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
response.raise_for_status()
return await response.text()
def req(): return rx.from_future(asyncio.ensure_future(_req())) >> ops.catch(rx.empty())
return rx.merge(*[req() for req in [req] * 10]) >> ops.reduce(lambda x, y: x + y)
# Or:
# def req(): return rx.from_future(asyncio.ensure_future(_req())).pipe(ops.catch(rx.empty()))
#
# return rx.merge(*[req() for req in [req] * 10]).pipe(
# ops.reduce(lambda x, y: x + y)
# )
def scenario10(base_url: str) -> rx.Observable[str]:
req_id = uuid.uuid4()
p = psutil.Process()
Response = namedtuple("Response", ["status", "body_text"])
async def _req(url: str):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
response.raise_for_status()
return Response(response.status, await response.text())
def _busy(bs: bytes) -> bytes:
m = hashlib.sha512()
m.update(bs)
return m.digest()
def blocking(): return rx.repeat_value(()) \
>> ops.scan(lambda accum, _: _busy(accum), seed=random.randbytes(512)) \
>> ops.map(lambda _: None)
# Or:
# def blocking(): return rx.repeat_value(()).pipe(
# ops.scan(lambda accum, _: _busy(accum), seed=random.randbytes(512)),
# ops.map(lambda _: None)
# )
def blocker(): return rx.from_future(asyncio.ensure_future(_req(f"{base_url}?{req_id}")))
def reporter():
def handle_response(response: Response):
match response.status:
case 200:
return rx.of(response.body_text)
case 302:
return rx.of(()) >> ops.delay(datetime.timedelta(seconds=1)) >> ops.flat_map(reporter())
# Or:
# return rx.of(()).pipe(
# ops.delay(datetime.timedelta(seconds=1)),
# ops.flat_map(reporter())
# )
with p.oneshot():
load = p.cpu_percent()
return rx.from_future(
asyncio.ensure_future(_req(f"{base_url}?{req_id}={load}"))
) >> ops.flat_map(handle_response)
# Or:
# return rx.from_future(
# asyncio.ensure_future(_req(f"{base_url}?{req_id}={load}"))
# ).pipe(
# ops.flat_map(handle_response)
# )
return rx.merge(
rx.merge(
blocking(), blocker()
) >> ops.first(lambda value: value is not None) >> ops.map(lambda _: None),
reporter()
) >> ops.first(lambda value: value is not None)
# Or:
# return rx.merge(
# rx.merge(blocking(), blocker()).pipe(
# ops.first(lambda value: value is not None),
# ops.map(lambda _: None)
# ),
# reporter()
# ).pipe(
# ops.first(lambda value: value is not None),
# )
scenarios: list[Callable[[str], Coroutine[Any, Any, rx.Observable[str]]]] = [
scenario1,
scenario2,
scenario3,
scenario4,
scenario5,
scenario6,
scenario7,
scenario8,
scenario9,
scenario10,
]
async def main():
for scenario in scenarios:
num = scenario.__name__[8:]
url = f"http://localhost:8080/{num}"
await scenario(url) >> ops.do_action(
lambda value: print(f"{scenario.__name__}: {value}")
)
if __name__ == "__main__":
asyncio.run(main())