62 lines
2.3 KiB
Python
62 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""测试完整的截图流程"""
|
|
|
|
import time
|
|
from operations.screenshot_service import ScreenshotService
|
|
from operations.screenshot_operations import ScreenshotOperations
|
|
|
|
def test_screenshot_flow(device_id=None):
|
|
"""测试截图流程"""
|
|
print("=== 截图流程测试 ===")
|
|
|
|
if not device_id:
|
|
# 尝试从环境变量或其他地方获取设备ID
|
|
import os
|
|
device_id = os.environ.get("ANDROID_DEVICE_ID")
|
|
if not device_id:
|
|
print("[ERROR] 未找到设备ID,请设置环境变量 ANDROID_DEVICE_ID")
|
|
return
|
|
|
|
print(f"设备ID: {device_id}")
|
|
|
|
# 1. 测试直接截图
|
|
print("\n1. 测试直接截图...")
|
|
ops = ScreenshotOperations()
|
|
start_time = time.time()
|
|
result1 = ops.get_screenshot(device_id, use_service=False)
|
|
time1 = (time.time() - start_time) * 1000
|
|
print(f"直接截图: 结果={result1}, 时间={time1:.0f}ms")
|
|
print("\n2. 启动后台截图服务...")
|
|
service = ScreenshotService.get_instance()
|
|
service.start(device_id, interval=1.5)
|
|
print("服务启动完成,等待第一次截图...")
|
|
|
|
# 等待服务完成第一次截图
|
|
time.sleep(3)
|
|
|
|
# 3. 测试服务模式(应该使用缓存)
|
|
print("\n3. 测试服务模式(应该使用缓存)...")
|
|
start_time = time.time()
|
|
result2 = ops.get_screenshot(device_id, use_service=True)
|
|
time2 = (time.time() - start_time) * 1000
|
|
print(f"服务模式截图: 结果={result2}, 时间={time2:.0f}ms")
|
|
# 4. 再次测试缓存命中
|
|
print("\n4. 再次测试缓存命中...")
|
|
start_time = time.time()
|
|
result3 = ops.get_screenshot(device_id, use_service=True)
|
|
time3 = (time.time() - start_time) * 1000
|
|
print(f"缓存命中截图: 结果={result3}, 时间={time3:.0f}ms")
|
|
# 5. 显示服务状态
|
|
print("\n5. 服务状态信息:")
|
|
cache_info = service.get_cache_info()
|
|
print(f" 运行状态: {cache_info['running']}")
|
|
if cache_info['last_screenshot_time'] > 0:
|
|
time_since = time.time() - cache_info['last_screenshot_time']
|
|
print(f" 上次截图时间: {time_since:.1f}秒前")
|
|
print(f" 缓存有效: {time_since < cache_info['cache_valid_duration']}")
|
|
|
|
print("\n=== 测试完成 ===")
|
|
|
|
if __name__ == "__main__":
|
|
test_screenshot_flow()
|