Skip to content

Commit 4559cbe

Browse files
authored
Create stress_testing.py
1 parent 949057e commit 4559cbe

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

extra/stress_testing.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from ThreadPoolExecutorPlus import ThreadPoolExecutor
2+
from collections import deque
3+
import time
4+
import random
5+
import datetime
6+
import asyncio
7+
import os
8+
import platform
9+
pltfm = platform.system()
10+
11+
'''
12+
Significantly performace drop down found on linux , reason not clear.
13+
'''
14+
15+
def func(arg):
16+
time.sleep(arg)
17+
return str(arg)
18+
19+
async def one_task(loop , executor , arg , statistics , dataflow):
20+
r = await loop.run_in_executor(executor , func , arg)
21+
if r == str(arg):
22+
statistics[0] += 1
23+
statistics[2] += arg
24+
else:
25+
statistics[1] += 1
26+
dataflow[0] = dataflow[0] - arg
27+
28+
async def report_thread(executor , statistics , dataflow , scale_factor):
29+
st = time.time()
30+
line = deque()
31+
while True:
32+
await asyncio.sleep(2)
33+
print(f"Report every 2 secs : [success] {statistics[0]} \t[fail] {statistics[1]} \t[dataflow] {'%.2f' % round(dataflow[0],2)} \t[currentthread] {len(executor._threads)} \t[qps] {round((statistics[0] / (time.time() - st)) , 2)}")
34+
35+
async def main():
36+
loop = asyncio.get_running_loop()
37+
statistics = [0 , 0 , 0]
38+
dataflow = [0]
39+
max_capability = 1024 * 10 * 0.8
40+
scale_factor = 0.2
41+
if pltfm == 'Windows':
42+
max_workers = min((os.cpu_count() or 1) << 7 , 1024)
43+
elif pltfm == 'Linux':
44+
max_workers = min((os.cpu_count() or 1) << 8 , 4096)
45+
else:
46+
raise RuntimeError('could only run on x86 platform')
47+
48+
with ThreadPoolExecutor(max_workers = max_workers) as executor:
49+
loop.create_task(report_thread(executor , statistics , dataflow , scale_factor))
50+
while True:
51+
rand_time = random.random() * scale_factor
52+
if dataflow[0] >= max_capability:
53+
await asyncio.sleep(0.1)
54+
continue
55+
# else:
56+
dataflow[0] = dataflow[0] + rand_time
57+
loop.create_task(one_task(loop , executor , rand_time , statistics , dataflow))
58+
await asyncio.sleep(0.001)
59+
60+
asyncio.run(main())

0 commit comments

Comments
 (0)