mirror of
https://github.com/meeb/tubesync.git
synced 2025-06-24 14:06:36 +00:00
Add timing and profiling decorators
This commit is contained in:
parent
fecfc689fa
commit
3f10e45e6a
@ -1,7 +1,11 @@
|
|||||||
|
import cProfile
|
||||||
|
import emoji
|
||||||
|
import io
|
||||||
|
import pstats
|
||||||
import string
|
import string
|
||||||
|
import time
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from urllib.parse import urlunsplit, urlencode, urlparse
|
from urllib.parse import urlunsplit, urlencode, urlparse
|
||||||
import emoji
|
|
||||||
from yt_dlp.utils import LazyList
|
from yt_dlp.utils import LazyList
|
||||||
from .errors import DatabaseConnectionError
|
from .errors import DatabaseConnectionError
|
||||||
|
|
||||||
@ -136,3 +140,29 @@ def json_serial(obj):
|
|||||||
if isinstance(obj, LazyList):
|
if isinstance(obj, LazyList):
|
||||||
return list(obj)
|
return list(obj)
|
||||||
raise TypeError(f'Type {type(obj)} is not json_serial()-able')
|
raise TypeError(f'Type {type(obj)} is not json_serial()-able')
|
||||||
|
|
||||||
|
|
||||||
|
def time_func(func):
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
start = time.perf_counter()
|
||||||
|
result = func(*args, **kwargs)
|
||||||
|
end = time.perf_counter()
|
||||||
|
elapsed = end - start
|
||||||
|
return (result, (start - end, start, end,),)
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def profile_func(func):
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
s = io.StringIO()
|
||||||
|
with cProfile.Profile() as pr:
|
||||||
|
pr.enable()
|
||||||
|
result = func(*args, **kwargs)
|
||||||
|
pr.disable()
|
||||||
|
ps = pstats.Stats(pr, stream=s)
|
||||||
|
ps.sort_stats(
|
||||||
|
pstats.SortKey.CUMULATIVE
|
||||||
|
).print_stats()
|
||||||
|
return (result, (s.getvalue(), ps),)
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user