Skip to content

Commit

Permalink
feat: yappi profiling
Browse files Browse the repository at this point in the history
Signed-off-by: Rongrong <[email protected]>
  • Loading branch information
Rongronggg9 committed Nov 10, 2023
1 parent 7bf9655 commit 7a7bc1d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ cachetools==5.3.1
CJKwrap==2.2
typing-extensions==4.8.0
uvloop==0.17.0; sys_platform!='win32' and sys_platform!='cygwin' and sys_platform!='cli'

# dev requirements
# TODO: drop requirements.txt and use pyproject.toml
yappi==1.4.0
27 changes: 25 additions & 2 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,22 @@
from . import log, db, command
from .i18n import i18n, ALL_LANGUAGES, get_commands_list
from .parsing import tgraph
from .compat import nullcontext

# log
logger = log.getLogger('RSStT')

if env.PROFILING:
try:
import yappi
yappi.set_clock_type(env.PROFILING if isinstance(env.PROFILING, str) else 'cpu')
logger.info(f'Profiling mode, clock type: {yappi.get_clock_type()}')
except ImportError:
logger.warning('yappi is not installed, profiling is disabled.')
yappi = None
else:
yappi = None

loop = env.loop
bot: Optional[TelegramClient] = None
pre_tasks = []
Expand Down Expand Up @@ -308,16 +320,27 @@ def main():
trigger=CronTrigger(minute='*', second=env.CRON_SECOND, timezone='UTC'),
max_instances=10,
misfire_grace_time=10)
scheduler.start()
with yappi.run(profile_threads=False) if yappi else nullcontext():
scheduler.start()
loop.run_until_complete(bot.disconnected)

loop.run_until_complete(bot.disconnected)
except (KeyboardInterrupt, SystemExit) as e:
logger.error(f'Received {type(e).__name__}, exiting...', exc_info=e)
exit_code = e.code if isinstance(e, SystemExit) and e.code is not None else 0
except Exception as e:
logger.critical('Uncaught error:', exc_info=e)
exit_code = 99
finally:
if yappi:
try:
prof = yappi.get_func_stats()
prof.print_all()
import time
curr_time = int(time.time())
for typ in ('pstat', 'ystat'):
prof.save(os.path.join(env.config_folder_path, f'prof_{curr_time}.{typ}'), typ)
except Exception as e:
logger.error('Error when exporting profiling stats:', exc_info=e)
try:
if getattr(signal, 'SIGALRM', None):
signal.alarm(15)
Expand Down
11 changes: 11 additions & 0 deletions src/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ def __list_parser(var: Optional[str]) -> list[str]:
LAZY_MEDIA_VALIDATION: Final = __bool_parser(os.environ.get('LAZY_MEDIA_VALIDATION'))
NO_UVLOOP: Final = __bool_parser(os.environ.get('NO_UVLOOP'))
MULTIPROCESSING: Final = __bool_parser(os.environ.get('MULTIPROCESSING'))

DEBUG: Final = __bool_parser(os.environ.get('DEBUG'))
__configure_logging( # config twice to make .env file work
level=colorlog.DEBUG if DEBUG else colorlog.INFO,
Expand All @@ -258,6 +259,16 @@ def __list_parser(var: Optional[str]) -> list[str]:
if DEBUG:
logger.debug('DEBUG mode enabled')

_profiling = os.environ.get('PROFILING')
if _profiling is None:
PROFILING: Final = False
else:
_profiling = _profiling.lower()
if _profiling in ('cpu', 'wall'):
PROFILING: Final = _profiling
else:
PROFILING: Final = __bool_parser(_profiling)

# ----- environment config -----
RAILWAY_STATIC_URL: Final = os.environ.get('RAILWAY_STATIC_URL')
PORT: Final = int(os.environ.get('PORT', 0)) or (8080 if RAILWAY_STATIC_URL else None)
Expand Down

0 comments on commit 7a7bc1d

Please sign in to comment.