I'm trying to measure python async function execution time. But I faced with problem because actual point when the function is starting is unknown.
Measurement function code:
def timer(func):
async def process(func, *args, **params):
if asyncio.iscoroutinefunction(func):
print('this function is a coroutine: {}'.format(func.__name__))
return await func(*args, **params)
else:
print('this is not a coroutine')
return func(*args, **params)
async def helper(*args, **params):
print('{}.time'.format(func.__name__))
start = datetime.datetime.now()
print(start)
result = await process(func, *args, **params)
finish = datetime.datetime.now()
print('>>> calculated - ', finish - start, 'start-', start, 'finish-', finish)
return result
return helper
Rest code:
@timer
async def fetch(name, session):
url = 'http://localhost:8808/'
payload = {}
async with session.put(url, headers=HEADERS, json=payload) as response:
session.get_connection_count()
response_data = await response.read()
result = {'response_code': response.status,
'response_data': response_data}
return result
def on_data_received(future):
# pass
response_obj = json.loads(future.result()['response_data'])
response_code = future.result()['response_code']
device_id = response_obj.get('clientDeviceID')
async def run(r):
connector = DecoratedTCPConnector(limit_per_host=10000, limit=20000)
with TimedClientSession(connector=connector) as client:
for i in range(r):
id = ''.join([random.choice('01234') for x in range(16)])
task = asyncio.ensure_future(fetch(id, client))
task.add_done_callback(on_data_received)
tasks.append(task)
return await asyncio.gather(*tasks)
The start time actually is the time when the routine task is added to queue, but I need the time when PUT request is sent.
Any suggestions are welcome.