Spaces:
Sleeping
Sleeping
File size: 4,108 Bytes
02d9941 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | """
์ฑ๋ฅ ์ธก์ ๋ฐ ๋ชจ๋ํฐ๋ง ์ ํธ๋ฆฌํฐ ๋ชจ๋
"""
import time
import logging
import functools
import traceback
import psutil
import os
import platform
import gc
from typing import Callable, Any
# ๋ก๊น
์ค์
logger = logging.getLogger(__name__)
def performance_logger(func):
"""
ํจ์ ์คํ ์๊ฐ ๋ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ๋ก๊น
ํ๋ ๋ฐ์ฝ๋ ์ดํฐ
Args:
func (callable): ์ธก์ ํ ํจ์
Returns:
callable: ๋ํ๋ ํจ์
"""
@functools.wraps(func)
async def wrapper(*args, **kwargs):
# ์์ ์๊ฐ ๋ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ์ธก์
start_time = time.time()
start_memory = 0
try:
# ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ์ธก์ (๊ฐ๋ฅํ ๊ฒฝ์ฐ)
if psutil:
process = psutil.Process(os.getpid())
start_memory = process.memory_info().rss / 1024 / 1024 # MB ๋จ์
except Exception:
pass
# ํจ์ ํธ์ถ
try:
result = await func(*args, **kwargs)
except Exception as e:
logger.error(f"ํจ์ {func.__name__} ์คํ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
logger.error(traceback.format_exc())
raise
# ์ข
๋ฃ ์๊ฐ ๋ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ์ธก์
end_time = time.time()
end_memory = 0
try:
if psutil:
process = psutil.Process(os.getpid())
end_memory = process.memory_info().rss / 1024 / 1024 # MB ๋จ์
except Exception:
pass
# ์คํ ์ ๋ณด ๋ก๊น
execution_time = end_time - start_time
memory_usage = end_memory - start_memory if end_memory > 0 else 0
logger.info(f"ํจ์ {func.__name__} ์คํ ์๊ฐ: {execution_time:.4f}์ด")
if end_memory > 0:
logger.info(f"ํจ์ {func.__name__} ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ๋ณํ: {memory_usage:.2f} MB (์์: {start_memory:.2f} MB, ์ข
๋ฃ: {end_memory:.2f} MB)")
return result
return wrapper
def log_system_info():
"""์์คํ
์ ๋ณด ๋ก๊น
"""
try:
logger.info("===== ์์คํ
์ ๋ณด =====")
logger.info(f"OS: {platform.system()} {platform.release()}")
logger.info(f"Python ๋ฒ์ : {platform.python_version()}")
if psutil:
# CPU ์ ๋ณด
cpu_count = psutil.cpu_count(logical=False)
cpu_count_logical = psutil.cpu_count(logical=True)
logger.info(f"CPU: {cpu_count} ์ฝ์ด ({cpu_count_logical} ๋
ผ๋ฆฌ ํ๋ก์ธ์)")
# ๋ฉ๋ชจ๋ฆฌ ์ ๋ณด
mem = psutil.virtual_memory()
total_mem = mem.total / (1024 * 1024 * 1024) # GB ๋จ์
available_mem = mem.available / (1024 * 1024 * 1024) # GB ๋จ์
logger.info(f"๋ฉ๋ชจ๋ฆฌ: ์ด {total_mem:.2f} GB (์ฌ์ฉ ๊ฐ๋ฅ: {available_mem:.2f} GB)")
# ๋์คํฌ ์ ๋ณด
disk = psutil.disk_usage('/')
total_disk = disk.total / (1024 * 1024 * 1024) # GB ๋จ์
free_disk = disk.free / (1024 * 1024 * 1024) # GB ๋จ์
logger.info(f"๋์คํฌ: ์ด {total_disk:.2f} GB (์ฌ์ ๊ณต๊ฐ: {free_disk:.2f} GB)")
logger.info("=======================")
except Exception as e:
logger.error(f"์์คํ
์ ๋ณด ๋ก๊น
์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
def cleanup_memory():
"""
๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ ํจ์ - ๊ธด ์์
ํ ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ์ ์ฌ์ฉ
"""
try:
# ๊ฐ๋น์ง ์ปฌ๋ ์
๊ฐ์ ์คํ
collected = gc.collect()
logger.debug(f"๊ฐ๋น์ง ์ปฌ๋ ์
์คํ: {collected}๊ฐ ๊ฐ์ฒด ์์ง")
# ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ๋ก๊น
(๊ฐ๋ฅํ ๊ฒฝ์ฐ)
if psutil:
process = psutil.Process(os.getpid())
memory_usage = process.memory_info().rss / 1024 / 1024 # MB ๋จ์
logger.debug(f"ํ์ฌ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋: {memory_usage:.2f} MB")
except Exception as e:
logger.error(f"๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}") |