一个性能测试工具aiohttp实现
#! /usr/bin/python3 # -*- coding:utf-8 -*- # file: test.py # author: wangchenxi # mail: wongchenxi@icloud.com # brief: # version: 0.1.00 # Create Time:2021-01-15 21:01:12 # Last Update: 2021-01-16 22时06分19秒 import time import asyncio from threading import Thread import functools import os import signal import aiohttp headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' } stop_sign = False def exit_loop(sign_name): global stop_sign if not stop_sign: stop_sign = True async def task_stop(): loop = asyncio.get_event_loop() loop.stop() async def task(e, i, m, u, h, data, loopargs): url = u try: async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=0.3)) as s: if hasattr(s, m): m = getattr(s, m) loopargs['output'][e][i]['start_time'] = time.time() async with await m(url) as response: ret = await response.text() loopargs['output'][e][i]['end_time'] = time.time() loopargs['output'][e][i]['status'] = response.status # 细节:在每一个with前面加上async,在每一步的阻塞操作前加上await except asyncio.TimeoutError: print('time_out') loopargs['output'][e][i]['time_out'] = 'time_out' finally: loopargs['input']['finished'] += 1 def sleep_to(t): s = 0 while s < t: s = time.time() time.sleep(0.01) def control_func(loop, loopargs): # 添加任务 global stop_sign while True: e = time.time() i = 0 ee = int(e) loopargs['output'][ee] = {} while i < loopargs['input']['times']: loopargs['output'][ee][i] = {} url = loopargs['input']['url'] asyncio.run_coroutine_threadsafe( task(ee, i, 'get', url, headers, None, loopargs), loop) loopargs['input']['started'] += 1 i += 1 print(ee, loopargs['input']['started']) sleep_to(e+1) if stop_sign: while loopargs['input']['started'] != loopargs['input']['finished']: time.sleep(1) print( loopargs['input']['started'], loopargs['input']['finished'] ) asyncio.run_coroutine_threadsafe(task_stop(), loop) break print('ending...') if __name__ == '__main__': new_loop = asyncio.new_event_loop() loopargs = { 'input':{ 'times':500, 'url':'http://127.0.0.1:8080', 'started':0, 'finished':0, }, 'output':{ } } for sign_name in ('SIGINT', 'SIGTERM'): new_loop.add_signal_handler(getattr(signal, sign_name), functools.partial(exit_loop, sign_name)) t = Thread(target=control_func, args=(new_loop, loopargs) ) t.start() asyncio.set_event_loop(new_loop) new_loop.run_forever() new_loop.close() t.join() # 归纳整理 for _, item in loopargs['output'].items(): ok = len(list(filter(lambda x: 'status' in x and x['status'] == 200 , item.values()))) all_res = len(list(filter(lambda x: 'status' in x , item.values()))) tt = len(list(filter(lambda x: 'time_out' in x, item.values()))) s = len(list(filter(lambda x: 'start_time' in x, item.values()))) print(f'{_}, 发送总数:{s}, 超时数量:{tt}, 返回数量:{all_res}, 成功数量:{ok}')
版权声明:除特别注明外,本站所有文章均为王晨曦个人站点原创
转载请注明:出处来自王晨曦个人站点 » 服务器性能测试客户端,aiohttp实现
ps -lef | grep http_client | grep -v grep|awk -F' ' '{print $4}' | xargs kill -2
Discount Shipped Ups Macrobid Visa Accepted
http://buystromectolon.com/ - Stromectol
Cialis Daily Fachinformation
Kamagra Oral Jelly
Ireland Voltaren
Nylon Gland United Structure PG Thread Insulating Glass Molecular Sieve ルイヴィトンコピー財布
Positive Displacement Flowmeters Trendy 316L Stainless Steel Chain Necklace Silver 14k Gold Filled Color Top Quality Clavicle Chain Minimalist Jewelry ルイヴィトン財布コピー
best selling national soccer jersey tennessee titans 2020 jersey cheap timberland boots china bills cole beasley jersey white and red dunks on feet mr price plus size dresses ilovejay http://www.ilovejay.net/
ルイヴィトン財布コピー Sheet Metal Box Fabrication French Luxury All Solid Wood Royal Blue Corner Table
2-Arm Reciprocating Spray Machine ヴィトン財布コピー Stainless Steel Pig Feeder
Waterproof Led Strip Light Bluetooth 24 Keys Remote RGB Led Strip Lights Music Control ルイヴィトン財布コピー Electrical Mcb Box
Bulk Disposable Vapes Agricultural Cultivator Rotary Tiller Blade イヴィトン財布コピー激安代引き
ルイヴィトンコピー財布 Scv For Navara D40 5412000822 Mercedes Benz Cooling Fan Clutch
chicago white sox hat 1986 kit nike free run 3 femmes fonctionnement chaussures hot punch dior red carpet dresses chicago white sox starter hat amazon red white nike flyknit racer the north face summit l3 down hoody rudere http://www.rudere.com/
Custom Cleaning Cloth Round Vanity Mirror イヴィトン財布コピー激安代引き
1000w Led Grow Light 702-21-07311 Excavator main pump solenoid valve ルイヴィトン財布コピー
eagles fitted hats new era 045 football lineman clothing karrimor gilet ladies red yellow dress dallas mavericks new era cap for uk jordan 8 n7 lesmatons http://www.lesmatons.com/
Thermal Insulating Fabric Network Communication Shell Aluminum Box ヴィトン財布コピー
retro jordan 1 sand dune bottom of feet red and tingling mufc away shirt for cheap deannas bridal boutique jordan 11 retro space jam elite customized youth jersey baltimore ravens alternate black nfl masalokumak http://www.masalokumak.com/
Ascorbyl Palmitate ヴィトン財布コピー Plastic Pipe Shredder
ルイヴィトン財布スーパーコピー Pop Up Sprinkler Spray Patterns Stainless Steel Feeder Use For Nursery Crate
ルイヴィトン財布スーパーコピー Food Plastic Bottles Flat Ceramic ring With Small Round of Three Colors
ルイヴィトン財布コピー激安代引き Diamond Decor Coffee Table Bitumen Joint Recycler
southern gentleman straw hat club fleece crew neck sweater sambavelo adidas birthday dress in winter fc bayern away jersey moncler brazeau down jacket nescim http://www.nescim.com/