mirror of
https://github.com/meeb/tubesync.git
synced 2025-06-25 14:36:34 +00:00
Merge pull request #827 from tcely/patch-17
Add timing and profiling decorators
This commit is contained in:
commit
fa1bcdbb3d
@ -1,8 +1,12 @@
|
||||
import cProfile
|
||||
import emoji
|
||||
import io
|
||||
import os
|
||||
import pstats
|
||||
import string
|
||||
import time
|
||||
from datetime import datetime
|
||||
from urllib.parse import urlunsplit, urlencode, urlparse
|
||||
import emoji
|
||||
from yt_dlp.utils import LazyList
|
||||
from .errors import DatabaseConnectionError
|
||||
|
||||
@ -172,3 +176,28 @@ def json_serial(obj):
|
||||
if isinstance(obj, LazyList):
|
||||
return list(obj)
|
||||
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()
|
||||
return (result, (end - start, 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, s),)
|
||||
return wrapper
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user