239 lines
7.5 KiB
Python
239 lines
7.5 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
快速配置和启动后端接口性能测试
|
||
|
||
这个脚本提供交互式配置界面,帮助快速设置测试参数
|
||
"""
|
||
import sys
|
||
import os
|
||
|
||
def print_header():
|
||
"""打印头部"""
|
||
print("\n" + "="*80)
|
||
print("后端接口性能测试 - 快速配置向导")
|
||
print("="*80 + "\n")
|
||
|
||
def get_api_url():
|
||
"""获取API地址"""
|
||
print("1. 配置主服务地址")
|
||
print("-" * 80)
|
||
default_url = "http://127.0.0.1:8216"
|
||
url = input(f"请输入主服务地址 (默认: {default_url}): ").strip()
|
||
return url if url else default_url
|
||
|
||
def get_search_url():
|
||
"""获取搜索服务地址"""
|
||
print("\n2. 配置搜索服务地址")
|
||
print("-" * 80)
|
||
default_url = "http://127.0.0.1:8321"
|
||
url = input(f"请输入搜索服务地址 (默认: {default_url}): ").strip()
|
||
return url if url else default_url
|
||
|
||
def get_credentials():
|
||
"""获取登录凭证"""
|
||
print("\n3. 配置登录信息")
|
||
print("-" * 80)
|
||
default_username = "13621242430"
|
||
username = input(f"请输入用户名 (默认: {default_username}): ").strip()
|
||
username = username if username else default_username
|
||
|
||
default_password = "admin123"
|
||
password = input(f"请输入密码 (默认: {default_password}): ").strip()
|
||
password = password if password else default_password
|
||
|
||
return username, password
|
||
|
||
def select_modules():
|
||
"""选择测试模块"""
|
||
print("\n4. 选择测试模块")
|
||
print("-" * 80)
|
||
|
||
modules = {
|
||
'basic': {'name': '基础接口', 'default': True},
|
||
'auth': {'name': '认证接口', 'default': True},
|
||
'dashboard': {'name': '工作台', 'default': True},
|
||
'enterprise': {'name': '企业管理', 'default': True},
|
||
'image': {'name': '图片管理', 'default': True},
|
||
'log': {'name': '日志管理', 'default': True},
|
||
'article': {'name': '文章管理', 'default': True},
|
||
'employee': {'name': '员工管理', 'default': True},
|
||
'author': {'name': '作者管理', 'default': True},
|
||
'search': {'name': '搜索服务', 'default': False},
|
||
}
|
||
|
||
print("可用的测试模块:")
|
||
for i, (key, info) in enumerate(modules.items(), 1):
|
||
status = "✓" if info['default'] else " "
|
||
print(f" [{status}] {i:2}. {info['name']:12} ({key})")
|
||
|
||
print("\n选择操作:")
|
||
print(" a - 全部启用")
|
||
print(" n - 全部禁用")
|
||
print(" d - 使用默认配置(推荐)")
|
||
print(" c - 自定义选择")
|
||
|
||
choice = input("\n请选择 (默认: d): ").strip().lower()
|
||
|
||
if choice == 'a':
|
||
# 全部启用
|
||
return {key: True for key in modules.keys()}
|
||
elif choice == 'n':
|
||
# 全部禁用
|
||
return {key: False for key in modules.keys()}
|
||
elif choice == 'c':
|
||
# 自定义选择
|
||
print("\n请输入要启用的模块编号,用逗号分隔(如: 1,2,3)")
|
||
print("或输入要禁用的模块编号前加'-'(如: 1,2,3,-10)")
|
||
selected = input("模块编号: ").strip()
|
||
|
||
result = {key: info['default'] for key, info in modules.items()}
|
||
|
||
if selected:
|
||
module_list = list(modules.keys())
|
||
for s in selected.split(','):
|
||
s = s.strip()
|
||
if s.startswith('-'):
|
||
# 禁用
|
||
try:
|
||
idx = int(s[1:]) - 1
|
||
if 0 <= idx < len(module_list):
|
||
result[module_list[idx]] = False
|
||
except:
|
||
pass
|
||
else:
|
||
# 启用
|
||
try:
|
||
idx = int(s) - 1
|
||
if 0 <= idx < len(module_list):
|
||
result[module_list[idx]] = True
|
||
except:
|
||
pass
|
||
|
||
return result
|
||
else:
|
||
# 默认配置
|
||
return {key: info['default'] for key, info in modules.items()}
|
||
|
||
def generate_config(api_url, search_url, username, password, modules):
|
||
"""生成配置代码"""
|
||
config_lines = [
|
||
"# 自动生成的配置",
|
||
f"API_BASE_URL = \"{api_url}\"",
|
||
f"SEARCH_API_BASE_URL = \"{search_url}\"",
|
||
f"USERNAME = \"{username}\"",
|
||
f"PASSWORD = \"{password}\"",
|
||
"",
|
||
"TEST_MODULES = {",
|
||
]
|
||
|
||
for key, enabled in modules.items():
|
||
config_lines.append(f" '{key}': {enabled},")
|
||
|
||
config_lines.append("}")
|
||
|
||
return "\n".join(config_lines)
|
||
|
||
def update_script(config):
|
||
"""更新脚本配置"""
|
||
script_file = "测试整体后端接口性能.py"
|
||
|
||
if not os.path.exists(script_file):
|
||
print(f"\n✗ 错误:找不到文件 {script_file}")
|
||
return False
|
||
|
||
try:
|
||
with open(script_file, 'r', encoding='utf-8') as f:
|
||
content = f.read()
|
||
|
||
# 查找配置区域
|
||
start_marker = "# API配置"
|
||
end_marker = "# 测试结果存储"
|
||
|
||
start_idx = content.find(start_marker)
|
||
end_idx = content.find(end_marker)
|
||
|
||
if start_idx == -1 or end_idx == -1:
|
||
print("\n✗ 错误:无法找到配置区域")
|
||
return False
|
||
|
||
# 替换配置
|
||
new_content = (
|
||
content[:start_idx] +
|
||
config + "\n\n" +
|
||
content[end_idx:]
|
||
)
|
||
|
||
# 保存
|
||
with open(script_file, 'w', encoding='utf-8') as f:
|
||
f.write(new_content)
|
||
|
||
print(f"\n✓ 配置已更新到 {script_file}")
|
||
return True
|
||
|
||
except Exception as e:
|
||
print(f"\n✗ 更新配置失败: {e}")
|
||
return False
|
||
|
||
def main():
|
||
"""主函数"""
|
||
print_header()
|
||
|
||
# 获取配置
|
||
api_url = get_api_url()
|
||
search_url = get_search_url()
|
||
username, password = get_credentials()
|
||
modules = select_modules()
|
||
|
||
# 显示配置摘要
|
||
print("\n" + "="*80)
|
||
print("配置摘要")
|
||
print("="*80)
|
||
print(f"主服务地址: {api_url}")
|
||
print(f"搜索服务地址: {search_url}")
|
||
print(f"登录用户: {username}")
|
||
print(f"登录密码: {'*' * len(password)}")
|
||
|
||
enabled_modules = [k for k, v in modules.items() if v]
|
||
disabled_modules = [k for k, v in modules.items() if not v]
|
||
|
||
print(f"\n启用的模块 ({len(enabled_modules)}个): {', '.join(enabled_modules)}")
|
||
if disabled_modules:
|
||
print(f"禁用的模块 ({len(disabled_modules)}个): {', '.join(disabled_modules)}")
|
||
|
||
# 确认
|
||
print("\n" + "="*80)
|
||
confirm = input("确认配置并更新脚本?(Y/n): ").strip().lower()
|
||
|
||
if confirm in ['', 'y', 'yes']:
|
||
# 生成配置
|
||
config = generate_config(api_url, search_url, username, password, modules)
|
||
|
||
# 更新脚本
|
||
if update_script(config):
|
||
print("\n" + "="*80)
|
||
print("✓ 配置完成!")
|
||
print("="*80)
|
||
print("\n现在可以运行测试脚本:")
|
||
print(" python 测试整体后端接口性能.py")
|
||
print("\n或者使用命令:")
|
||
print(" python -m 测试整体后端接口性能")
|
||
return 0
|
||
else:
|
||
return 1
|
||
else:
|
||
print("\n✗ 已取消配置")
|
||
return 0
|
||
|
||
if __name__ == "__main__":
|
||
try:
|
||
sys.exit(main())
|
||
except KeyboardInterrupt:
|
||
print("\n\n✗ 配置被用户中断")
|
||
sys.exit(1)
|
||
except Exception as e:
|
||
print(f"\n✗ 配置过程出现异常: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
sys.exit(1)
|