97 lines
3.1 KiB
Python
97 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
测试股票名称获取的缓存优化
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
import time
|
|
|
|
# 添加src目录到路径
|
|
current_dir = Path(__file__).parent
|
|
src_dir = current_dir / "src"
|
|
sys.path.insert(0, str(src_dir))
|
|
|
|
from loguru import logger
|
|
from src.data.data_fetcher import ADataFetcher
|
|
|
|
def test_stock_name_cache():
|
|
"""测试股票名称缓存机制"""
|
|
logger.info("🧪 开始测试股票名称缓存优化")
|
|
|
|
# 初始化数据获取器
|
|
data_fetcher = ADataFetcher()
|
|
|
|
# 测试股票列表
|
|
test_stocks = ['000001.SZ', '000002.SZ', '600000.SH', '600036.SH', '000858.SZ']
|
|
|
|
# 第一次获取股票名称(会触发缓存构建)
|
|
logger.info("📊 第一次获取股票名称(构建缓存)...")
|
|
start_time = time.time()
|
|
|
|
names_first = {}
|
|
for stock_code in test_stocks:
|
|
name = data_fetcher.get_stock_name(stock_code)
|
|
names_first[stock_code] = name
|
|
logger.info(f" {stock_code}: {name}")
|
|
|
|
first_duration = time.time() - start_time
|
|
logger.info(f"⏱️ 第一次获取耗时: {first_duration:.2f}秒")
|
|
|
|
# 等待一秒
|
|
time.sleep(1)
|
|
|
|
# 第二次获取股票名称(应该从缓存读取)
|
|
logger.info("📊 第二次获取股票名称(从缓存读取)...")
|
|
start_time = time.time()
|
|
|
|
names_second = {}
|
|
for stock_code in test_stocks:
|
|
name = data_fetcher.get_stock_name(stock_code)
|
|
names_second[stock_code] = name
|
|
logger.info(f" {stock_code}: {name}")
|
|
|
|
second_duration = time.time() - start_time
|
|
logger.info(f"⏱️ 第二次获取耗时: {second_duration:.2f}秒")
|
|
|
|
# 比较结果
|
|
logger.info("📈 性能对比:")
|
|
if second_duration < first_duration:
|
|
speedup = first_duration / second_duration
|
|
logger.info(f"✅ 缓存优化成功! 第二次比第一次快 {speedup:.1f}x")
|
|
else:
|
|
logger.warning("❌ 缓存优化效果不明显")
|
|
|
|
# 验证数据一致性
|
|
consistent = names_first == names_second
|
|
logger.info(f"🔍 数据一致性: {'✅ 一致' if consistent else '❌ 不一致'}")
|
|
|
|
# 显示缓存状态
|
|
logger.info(f"📦 当前缓存中的股票数量: {len(data_fetcher._stock_name_cache)}")
|
|
|
|
return first_duration, second_duration, consistent
|
|
|
|
if __name__ == "__main__":
|
|
# 设置日志
|
|
logger.remove()
|
|
logger.add(sys.stdout, level="INFO", format="{time:HH:mm:ss} | {level} | {message}")
|
|
|
|
print("=" * 60)
|
|
print("🧪 股票名称缓存优化测试")
|
|
print("=" * 60)
|
|
|
|
try:
|
|
first_time, second_time, is_consistent = test_stock_name_cache()
|
|
|
|
print("\n" + "=" * 60)
|
|
print("📊 测试结果总结:")
|
|
print(f" 第一次获取耗时: {first_time:.2f}秒")
|
|
print(f" 第二次获取耗时: {second_time:.2f}秒")
|
|
print(f" 性能提升倍数: {first_time/second_time:.1f}x")
|
|
print(f" 数据一致性: {'✅ 通过' if is_consistent else '❌ 失败'}")
|
|
print("=" * 60)
|
|
|
|
except Exception as e:
|
|
logger.error(f"测试过程中发生错误: {e}")
|
|
import traceback
|
|
traceback.print_exc() |