fix(core): 修复调试模式下程序运行速度过慢的问题

This commit is contained in:
XcantloadX 2025-02-05 13:03:43 +08:00
parent 78142018da
commit dd3d3499ca
2 changed files with 40 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import psutil
import hashlib import hashlib
import traceback import traceback
from pathlib import Path from pathlib import Path
from functools import cache
from datetime import datetime from datetime import datetime
from dataclasses import dataclass from dataclasses import dataclass
from typing import NamedTuple, TextIO, Literal from typing import NamedTuple, TextIO, Literal
@ -133,6 +134,7 @@ def to_html(text: str) -> str:
IDEType = Literal['vscode', 'cursor', 'windsurf'] IDEType = Literal['vscode', 'cursor', 'windsurf']
@cache
def get_current_ide() -> IDEType | None: def get_current_ide() -> IDEType | None:
"""获取当前IDE类型""" """获取当前IDE类型"""
me = psutil.Process() me = psutil.Process()

View File

@ -1,7 +1,9 @@
import os import os
import time import time
import pstats
import typing import typing
import logging import logging
import cProfile
from time import sleep from time import sleep
from importlib import resources from importlib import resources
from functools import lru_cache from functools import lru_cache
@ -208,3 +210,39 @@ def res_path(path: str) -> str:
logger.debug(f'res_path: {ret}') logger.debug(f'res_path: {ret}')
return ret return ret
class Profiler:
"""
性能分析器 `cProfile` 的简单封装
使用方法
```python
with Profiler('profile.prof'):
# ...
# 或者
profiler = Profiler('profile.prof')
profiler.begin()
# ...
profiler.end()
```
"""
def __init__(self, file_path: str):
self.profiler = cProfile.Profile()
self.stats = None
self.file_path = file_path
def __enter__(self):
self.profiler.enable()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.profiler.disable()
self.stats = pstats.Stats(self.profiler)
self.stats.dump_stats(self.file_path)
def begin(self):
self.__enter__()
def end(self):
self.__exit__(None, None, None)