scripts / benchmark.py
"""
Manatrix Comprehensive Test Framework
用于评估 Manatrix 框架各项组件的性能
"""
import json
import time
import asyncio
from datetime import datetime
from dataclasses import dataclass, field
from typing import List, Dict, Any, Optional
from pathlib import Path
# 测试配置
@dataclass
class TestConfig:
"""测试配置"""
# LLM 配置
llm_provider: str = "deepseek"
llm_api_key: str = ""
llm_model: str = "deepseek-chat"
# RAG 配置
embedding_model: str = "all-MiniLM-L6-v2"
# 专家配置
enabled_domains: List[str] = field(default_factory=lambda: [
"network", "web", "ad", "cloud", "iot", "mobile"
])
# 测试目标
target_ip: str = "127.0.0.1"
target_port: int = 8000
@dataclass
class BenchmarkResult:
"""基准测试结果"""
name: str
metric: str
value: float
unit: str
timestamp: str = field(default_factory=lambda: datetime.now().isoformat())
@dataclass
class PenetrationTestResult:
"""渗透测试结果"""
target: str
duration_seconds: float
vulnerabilities_found: int
attack_paths_explored: int
successful_exploits: int
failed_exploits: int
coverage_percentage: float
notes: str = ""
class ComponentBenchmark:
"""组件基准测试"""
def __init__(self, config: TestConfig):
self.config = config
self.results: List[BenchmarkResult] = []
async def test_rag_retrieval(self) -> List[BenchmarkResult]:
"""测试 RAG 检索准确率"""
results = []
print("\n[1/6] 测试 RAG 检索系统...")
try:
from models.rag_retriever import RAGRetriever
retriever = RAGRetriever(
embedding_model=self.config.embedding_model
)
# 测试查询
test_queries = [
"SQL injection vulnerability",
" privilege escalation",
"CVE-2021-44228 log4j",
"Active Directory kerberoasting",
"AWS S3 bucket misconfiguration"
]
correct = 0
total = 0
latency_total = 0.0
for query in test_queries:
start = time.time()
hits = await retriever.retrieve(query, top_k=10)
latency = time.time() - start
latency_total += latency
# 检查是否返回了相关结果
if hits and len(hits) > 0:
# 简单检查:是否返回了相关文档
correct += 1
total += 1
accuracy = (correct / total * 100) if total > 0 else 0
avg_latency = (latency_total / total * 1000) # ms
results.append(BenchmarkResult(
name="RAG Retrieval",
metric="Accuracy",
value=accuracy,
unit="%"
))
results.append(BenchmarkResult(
name="RAG Retrieval",
metric="Avg Latency",
value=avg_latency,
unit="ms"
))
print(f" ✓ 检索准确率: {accuracy:.1f}%")
print(f" ✓ 平均延迟: {avg_latency:.1f}ms")
except Exception as e:
print(f" ✗ RAG测试失败: {e}")
results.append(BenchmarkResult(
name="RAG Retrieval",
metric="Error",
value=0,
unit=str(e)[:50]
))
self.results.extend(results)
return results
async def test_expert_router(self) -> List[BenchmarkResult]:
"""测试专家路由准确性"""
results = []
print("\n[2/6] 测试专家路由系统...")
try:
from models.expert_router import ExpertRouter
router = ExpertRouter(
enabled_domains=self.config.enabled_domains
)
# 测试用例:目标类型 -> 预期专家
test_cases = [
("192.168.1.100", "network", "NetworkReconExpert"),
("http://target.com", "web", "WebSecurityExpert"),
("192.168.1.100", "ad", "ADSecurityExpert"),
("aws://prod", "cloud", "CloudSecurityExpert"),
("iot-camera", "iot", "IoTSecurityExpert"),
]
correct = 0
total = 0
for target, target_type, expected_expert in test_cases:
selected = router.select_expert(target, target_type)
if selected == expected_expert:
correct += 1
total += 1
accuracy = (correct / total * 100) if total > 0 else 0
results.append(BenchmarkResult(
name="Expert Router",
metric="Selection Accuracy",
value=accuracy,
unit="%"
))
print(f" ✓ 专家选择准确率: {accuracy:.1f}%")
except Exception as e:
print(f" ✗ 专家路由测试失败: {e}")
self.results.extend(results)
return results
async def test_llm_response(self) -> List[BenchmarkResult]:
"""测试 LLM 响应时间"""
results = []
print("\n[3/6] 测试 LLM 响应时间...")
if not self.config.llm_api_key:
print(" ⚠ 跳过: 未配置 LLM API Key")
results.append(BenchmarkResult(
name="LLM Response",
metric="Skipped",
value=0,
unit="No API key"
))
self.results.extend(results)
return results
try:
from models.llm_provider import LLMProvider
llm = LLMProvider(
provider=self.config.llm_provider,
api_key=self.config.llm_api_key,
model=self.config.llm_model
)
# 测试提示
test_prompts = [
"分析: 目标 192.168.1.100 开放端口 80,443,22",
"生成针对 Windows Server 2019 的攻击计划",
"解释 nmap 扫描结果: 端口 445 开放",
]
times = []
for prompt in test_prompts:
start = time.time()
response = await llm.generate(prompt)
elapsed = time.time() - start
times.append(elapsed)
avg_time = sum(times) / len(times) * 1000 # ms
results.append(BenchmarkResult(
name="LLM Response",
metric="Avg Response Time",
value=avg_time,
unit="ms"
))
print(f" ✓ 平均响应时间: {avg_time:.0f}ms")
except Exception as e:
print(f" ✗ LLM测试失败: {e}")
self.results.extend(results)
return results
async def test_agent_planning(self) -> List[BenchmarkResult]:
"""测试 Agent 规划能力"""
results = []
print("\n[4/6] 测试 Agent 规划能力...")
if not self.config.llm_api_key:
print(" ⚠ 跳过: 未配置 LLM API Key")
results.append(BenchmarkResult(
name="Agent Planning",
metric="Skipped",
value=0,
unit="No API key"
))
self.results.extend(results)
return results
try:
from models.manatrix_agent import ManatrixAgent
from models.llm_provider import LLMConfig
llm_config = LLMConfig(
provider=self.config.llm_provider,
api_key=self.config.llm_api_key,
model=self.config.llm_model
)
agent = ManatrixAgent(llm_config=llm_config)
# 测试简报
test_briefs = [
"对 192.168.1.100 进行端口扫描",
"测试 http://example.com 的 SQL 注入",
"检查 Windows 域环境安全"
]
plan_times = []
for brief in test_briefs:
start = time.time()
plan = await agent.plan(brief)
elapsed = time.time() - start
plan_times.append(elapsed)
avg_time = sum(plan_times) / len(plan_times)
results.append(BenchmarkResult(
name="Agent Planning",
metric="Avg Planning Time",
value=avg_time,
unit="seconds"
))
print(f" ✓ 平均规划时间: {avg_time:.1f}s")
except Exception as e:
print(f" ✗ Agent规划测试失败: {e}")
self.results.extend(results)
return results
async def test_tool_orchestration(self) -> List[BenchmarkResult]:
"""测试工具编排"""
results = []
print("\n[5/6] 测试工具编排...")
try:
# 模拟工具执行测试
from utils.tool_orchestrator import ToolOrchestrator
orchestrator = ToolOrchestrator()
# 测试用例
test_tools = [
("nmap", ["-sV", "127.0.0.1"]),
("echo", ["test"]),
]
times = []
for tool, args in test_tools:
start = time.time()
try:
result = await orchestrator.execute(tool, args, timeout=5)
elapsed = time.time() - start
times.append(elapsed)
except Exception:
# 工具可能不存在
pass
if times:
avg_time = sum(times) / len(times) * 1000
results.append(BenchmarkResult(
name="Tool Orchestration",
metric="Avg Execution Time",
value=avg_time,
unit="ms"
))
print(f" ✓ 平均执行时间: {avg_time:.0f}ms")
else:
print(" ⚠ 没有可执行的工具")
except Exception as e:
print(f" ✗ 工具编排测试失败: {e}")
self.results.extend(results)
return results
async def test_knowledge_base(self) -> List[BenchmarkResult]:
"""测试知识库"""
results = []
print("\n[6/6] 测试知识库...")
try:
from knowledge_graph.vector_store import VectorStore
store = VectorStore()
# 获取知识库统计
stats = store.get_stats()
num_entries = stats.get("num_entries", 0)
num_cves = stats.get("num_cves", 0)
num_techniques = stats.get("num_techniques", 0)
results.append(BenchmarkResult(
name="Knowledge Base",
metric="Total Entries",
value=num_entries,
unit=""
))
results.append(BenchmarkResult(
name="Knowledge Base",
metric="CVE Entries",
value=num_cves,
unit=""
))
results.append(BenchmarkResult(
name="Knowledge Base",
metric="Technique Entries",
value=num_techniques,
unit=""
))
print(f" ✓ 知识库条目: {num_entries}")
print(f" ✓ CVE 条目: {num_cves}")
print(f" ✓ 技术条目: {num_techniques}")
except Exception as e:
print(f" ✗ 知识库测试失败: {e}")
self.results.extend(results)
return results
async def run_all(self) -> List[BenchmarkResult]:
"""运行所有组件测试"""
print("=" * 50)
print("Manatrix 组件基准测试")
print("=" * 50)
await self.test_rag_retrieval()
await self.test_expert_router()
await self.test_llm_response()
await self.test_agent_planning()
await self.test_tool_orchestration()
await self.test_knowledge_base()
return self.results
class PenetrationBenchmark:
"""渗透测试基准"""
def __init__(self, config: TestConfig):
self.config = config
self.results: List[PenetrationTestResult] = []
async def test_local_target(self) -> PenetrationTestResult:
"""测试本地靶标"""
print("\n[渗透测试] 测试本地靶标...")
start_time = time.time()
try:
from models.manatrix_agent import ManatrixAgent
from models.llm_provider import LLMConfig
if not self.config.llm_api_key:
print(" ⚠ 跳过: 未配置 LLM API Key")
return PenetrationTestResult(
target=self.config.target_ip,
duration_seconds=0,
vulnerabilities_found=0,
attack_paths_explored=0,
successful_exploits=0,
failed_exploits=0,
coverage_percentage=0,
notes="Skipped: No API key"
)
llm_config = LLMConfig(
provider=self.config.llm_provider,
api_key=self.config.llm_api_key,
model=self.config.llm_model
)
agent = ManatrixAgent(llm_config=llm_config)
# 运行简短的渗透测试
result = await agent.run(
brief=f"简要扫描 {self.config.target_ip} 的开放端口",
max_steps=10,
timeout=300
)
duration = time.time() - start_time
# 解析结果
vuln_count = len(result.get("vulnerabilities", []))
path_count = len(result.get("attack_paths", []))
result = PenetrationTestResult(
target=self.config.target_ip,
duration_seconds=duration,
vulnerabilities_found=vuln_count,
attack_paths_explored=path_count,
successful_exploits=0,
failed_exploits=0,
coverage_percentage=min(100, path_count / 20 * 100),
notes=f"Found {vuln_count} issues"
)
print(f" ✓ 完成: {vuln_count} 个漏洞, {duration:.1f}s")
except Exception as e:
print(f" ✗ 测试失败: {e}")
duration = time.time() - start_time
result = PenetrationTestResult(
target=self.config.target_ip,
duration_seconds=duration,
vulnerabilities_found=0,
attack_paths_explored=0,
successful_exploits=0,
failed_exploits=0,
coverage_percentage=0,
notes=str(e)[:100]
)
self.results.append(result)
return result
async def run_all(self) -> List[PenetrationTestResult]:
"""运行所有渗透测试"""
print("=" * 50)
print("Manatrix 渗透测试基准")
print("=" * 50)
await self.test_local_target()
return self.results
class ComparisonBenchmark:
"""对比基准测试"""
def __init__(self, config: TestConfig):
self.config = config
async def test_vs_nmap(self) -> Dict[str, Any]:
"""对比 nmap"""
print("\n[对比测试] Manatrix vs nmap...")
results = {}
# 测试 nmap
start = time.time()
nmap_time = 0
try:
import subprocess
start = time.time()
subprocess.run(
["nmap", "-sV", "-T4", "127.0.0.1"],
capture_output=True,
timeout=30
)
nmap_time = time.time() - start
except Exception:
nmap_time = -1
results["nmap_time"] = nmap_time if nmap_time > 0 else None
# Manatrix 时间(需要API)
if self.config.llm_api_key:
start = time.time()
# 简化的Manatrix扫描
results["manatrix_time"] = time.time() - start
else:
results["manatrix_time"] = None
print(f" nmap: {nmap_time:.2f}s" if nmap_time > 0 else " nmap: N/A")
return results
async def main():
"""主函数"""
# 加载配置
import os
config = TestConfig(
llm_api_key=os.environ.get("DEEPSEEK_API_KEY", ""),
target_ip="127.0.0.1"
)
# 运行组件测试
benchmark = ComponentBenchmark(config)
component_results = await benchmark.run_all()
# 保存结果
output_file = Path("benchmark_results.json")
output_data = {
"timestamp": datetime.now().isoformat(),
"config": {
"llm_provider": config.llm_provider,
"embedding_model": config.embedding_model,
"target_ip": config.target_ip
},
"component_results": [
{
"name": r.name,
"metric": r.metric,
"value": r.value,
"unit": r.unit
}
for r in component_results
]
}
with open(output_file, "w") as f:
json.dump(output_data, f, indent=2)
print(f"\n结果已保存到: {output_file}")
print("=" * 50)
return output_data
if __name__ == "__main__":
asyncio.run(main())