File tree Expand file tree Collapse file tree 4 files changed +44
-2
lines changed Expand file tree Collapse file tree 4 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ cryptography = "*"
1414faker = " *"
1515fastapi = " *"
1616pytest = " *"
17+ pytest-asyncio = " *"
1718tortoise-orm = " *"
1819uvicorn = " *"
1920
Original file line number Diff line number Diff line change 1+ import asyncio
12from functools import wraps
23from logging import getLogger
34from time import perf_counter
67
78
89def timing (func ):
10+
911 @wraps (func )
10- async def wrapper (* args , ** kwargs ):
12+ async def async_wrapper (* args , ** kwargs ):
1113 st = perf_counter ()
1214 result = await func (* args , ** kwargs )
1315 et = perf_counter ()
1416 logger .info (f"{ func .__module__ } /{ func .__name__ } Cost { 1000 * (et - st ):.2f} ms" )
1517 return result
16- return wrapper
18+
19+ @wraps (func )
20+ def wrapper (* args , ** kwargs ):
21+ st = perf_counter ()
22+ result = func (* args , ** kwargs )
23+ et = perf_counter ()
24+ logger .info (f"{ func .__module__ } /{ func .__name__ } Cost { 1000 * (et - st ):.2f} ms" )
25+ return result
26+
27+ if asyncio .iscoroutinefunction (func ):
28+ return async_wrapper
29+ else :
30+ return wrapper
Original file line number Diff line number Diff line change @@ -5,5 +5,6 @@ cryptography
55faker
66fastapi
77pytest
8+ pytest-asyncio
89tortoise-orm
910uvicorn
Original file line number Diff line number Diff line change 1+ import pytest
2+
3+ from fastapi_esql import timing
4+
5+
6+ @timing
7+ async def afunc ():
8+ """async def afunc()"""
9+ return True
10+
11+
12+ @timing
13+ def func ():
14+ """def func()"""
15+ return False
16+
17+
18+ @pytest .mark .asyncio
19+ async def test_async_timing ():
20+ print (afunc , afunc .__doc__ , afunc .__name__ )
21+ assert await afunc () == True
22+
23+
24+ def test_sync_timing ():
25+ print (func , func .__doc__ , func .__name__ )
26+ assert func () == False
You can’t perform that action at this time.
0 commit comments