feat(性能分析): 实现性能分析工具模块并添加相关测试
添加性能分析工具模块,包括时间测量、内存分析和性能统计功能 添加测试文件和示例配置,完善性能分析工具的使用场景 在工具模块中实现单例装饰器并导出到__init__.py
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
通用单例模式基类
|
||||
"""
|
||||
from typing import Any, Optional, Type, TypeVar
|
||||
import functools
|
||||
|
||||
T = TypeVar('T')
|
||||
|
||||
@@ -38,3 +39,29 @@ class Singleton:
|
||||
if self._initialized:
|
||||
return
|
||||
self._initialized = True
|
||||
|
||||
|
||||
def singleton(cls: Type[T]) -> Type[T]:
|
||||
"""
|
||||
单例装饰器
|
||||
|
||||
将普通类转换为单例类,确保整个应用程序中只有一个实例。
|
||||
|
||||
Args:
|
||||
cls: 要转换为单例的类
|
||||
|
||||
Returns:
|
||||
Type[T]: 单例类
|
||||
"""
|
||||
_instance: Optional[T] = None
|
||||
_initialized: bool = False
|
||||
|
||||
@functools.wraps(cls)
|
||||
def wrapper(*args: Any, **kwargs: Any) -> T:
|
||||
nonlocal _instance, _initialized
|
||||
|
||||
if _instance is None:
|
||||
_instance = cls(*args, **kwargs)
|
||||
return _instance
|
||||
|
||||
return wrapper
|
||||
|
||||
Reference in New Issue
Block a user