ai角色批量测试

This commit is contained in:
2025-09-21 10:12:51 +08:00
parent 0e7a009383
commit 696cbc9f66
4 changed files with 739 additions and 313 deletions
@@ -1,287 +1,307 @@
import { _decorator, Component, Node, log, Button, Label } from 'cc';
import { ChatAIService } from '../core/ChatAIService';
import { _decorator, Component, Node, log, Button, Label } from "cc";
import { ChatAIService } from "../core/ChatAIService";
const { ccclass, property } = _decorator;
interface TestResult {
roleId: number;
success: boolean;
response?: string;
error?: string;
duration: number;
roleId: number;
success: boolean;
response?: string;
error?: string;
duration: number;
}
interface TestReport {
totalTests: number;
successCount: number;
failureCount: number;
successIds: number[];
failureIds: number[];
results: TestResult[];
totalDuration: number;
totalTests: number;
successCount: number;
failureCount: number;
successIds: number[];
failureIds: number[];
results: TestResult[];
totalDuration: number;
}
@ccclass('ChatAIServiceBatchTest')
@ccclass("ChatAIServiceBatchTest")
export class ChatAIServiceBatchTest extends Component {
@property(Button)
startTestButton: Button = null;
@property(Button)
startTestButton: Button = null;
@property(Label)
statusLabel: Label = null;
@property(Label)
statusLabel: Label = null;
@property(Label)
resultLabel: Label = null;
@property(Label)
resultLabel: Label = null;
private isTestRunning: boolean = false;
private testReport: TestReport = null;
private isTestRunning: boolean = false;
private testReport: TestReport = null;
onLoad() {
if (this.startTestButton) {
this.startTestButton.node.on(Button.EventType.CLICK, this.startBatchTest, this);
}
this.updateStatus("点击开始按钮进行批量测试");
onLoad() {
if (this.startTestButton) {
this.startTestButton.node.on(
Button.EventType.CLICK,
this.startBatchTest,
this
);
}
/**
* 延时工具函数
* @param seconds 延时秒数
*/
private async delay(seconds: number): Promise<void> {
return new Promise(resolve => {
setTimeout(resolve, seconds * 1000);
});
this.updateStatus("点击开始按钮进行批量测试");
}
/**
* 延时工具函数
* @param seconds 延时秒数
*/
private async delay(seconds: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, seconds * 1000);
});
}
/**
* 更新状态显示
*/
private updateStatus(message: string): void {
if (this.statusLabel) {
this.statusLabel.string = message;
}
log(`[ChatAIBatchTest] ${message}`);
}
/**
* 更新状态显示
*/
private updateStatus(message: string): void {
if (this.statusLabel) {
this.statusLabel.string = message;
}
log(`[ChatAIBatchTest] ${message}`);
}
/**
* 更新结果显示
*/
private updateResult(report: TestReport): void {
if (!this.resultLabel) return;
/**
* 更新结果显示
*/
private updateResult(report: TestReport): void {
if (!this.resultLabel) return;
const resultText = `测试完成!
const resultText = `测试完成!
总测试数: ${report.totalTests}
成功数: ${report.successCount}
失败数: ${report.failureCount}
总耗时: ${(report.totalDuration / 1000).toFixed(2)}s
成功的ID: ${report.successIds.join(', ')}
成功的ID: ${report.successIds.join(", ")}
失败的ID: ${report.failureIds.join(', ')}`;
失败的ID: ${report.failureIds.join(", ")}`;
this.resultLabel.string = resultText;
}
this.resultLabel.string = resultText;
}
/**
* 测试单个角色ID
*/
private async testSingleRole(roleId: number): Promise<TestResult> {
const startTime = Date.now();
/**
* 测试单个角色ID
*/
private async testSingleRole(roleId: number): Promise<TestResult> {
const startTime = Date.now();
try {
this.updateStatus(`正在测试角色ID: ${roleId}`);
try {
this.updateStatus(`正在测试角色ID: ${roleId}`);
const response = await ChatAIService.Instance.sendMessage(roleId, "hello");
const duration = Date.now() - startTime;
const response = await ChatAIService.Instance.sendMessage(
roleId,
"hello"
);
const duration = Date.now() - startTime;
if (response && response.trim() !== "") {
log(`[ChatAIBatchTest] 角色 ${roleId} 测试成功: ${response.substring(0, 50)}...`);
return {
roleId,
success: true,
response: response.substring(0, 100), // 只记录前100字符
duration
};
} else {
log(`[ChatAIBatchTest] 角色 ${roleId} 返回空响应`);
return {
roleId,
success: false,
error: "返回空响应",
duration
};
}
} catch (error) {
const duration = Date.now() - startTime;
const errorMessage = error instanceof Error ? error.message : String(error);
log(`[ChatAIBatchTest] 角色 ${roleId} 测试失败: ${errorMessage}`);
return {
roleId,
success: false,
error: errorMessage,
duration
};
}
}
/**
* 开始批量测试
*/
public async startBatchTest(): Promise<void> {
if (this.isTestRunning) {
this.updateStatus("测试正在进行中,请等待...");
return;
}
this.isTestRunning = true;
if (this.startTestButton) {
this.startTestButton.interactable = false;
}
if (this.resultLabel) {
this.resultLabel.string = "";
}
const startTime = Date.now();
const results: TestResult[] = [];
const successIds: number[] = [];
const failureIds: number[] = [];
log("[ChatAIBatchTest] 开始批量测试角色ID 10001-10030");
this.updateStatus("开始批量测试...");
// 测试角色ID 10001-10030
for (let roleId = 10001; roleId <= 10030; roleId++) {
try {
// 测试单个角色
const result = await this.testSingleRole(roleId);
results.push(result);
if (result.success) {
successIds.push(roleId);
} else {
failureIds.push(roleId);
}
// 等待10秒(最后一个不需要等待)
if (roleId < 10030) {
this.updateStatus(`角色 ${roleId} 测试完成,等待10秒...`);
await this.delay(10);
}
} catch (error) {
log(`[ChatAIBatchTest] 测试角色 ${roleId} 时发生意外错误: ${error}`);
results.push({
roleId,
success: false,
error: `意外错误: ${error}`,
duration: 0
});
failureIds.push(roleId);
}
}
const totalDuration = Date.now() - startTime;
// 生成测试报告
this.testReport = {
totalTests: 30,
successCount: successIds.length,
failureCount: failureIds.length,
successIds,
failureIds,
results,
totalDuration
if (response && response.trim() !== "") {
log(
`[ChatAIBatchTest] 角色 ${roleId} 测试成功: ${response.substring(
0,
50
)}...`
);
return {
roleId,
success: true,
response: response.substring(0, 100), // 只记录前100字符
duration,
};
} else {
log(`[ChatAIBatchTest] 角色 ${roleId} 返回空响应`);
return {
roleId,
success: false,
error: "返回空响应",
duration,
};
}
} catch (error) {
const duration = Date.now() - startTime;
const errorMessage =
error instanceof Error ? error.message : String(error);
log(`[ChatAIBatchTest] 角色 ${roleId} 测试失败: ${errorMessage}`);
// 输出详细报告到控制台
this.logDetailedReport(this.testReport);
return {
roleId,
success: false,
error: errorMessage,
duration,
};
}
}
// 更新UI显示
this.updateResult(this.testReport);
this.updateStatus("批量测试完成!");
if (this.startTestButton) {
this.startTestButton.interactable = true;
}
this.isTestRunning = false;
/**
* 开始批量测试
*/
public async startBatchTest(): Promise<void> {
if (this.isTestRunning) {
this.updateStatus("测试正在进行中,请等待...");
return;
}
/**
* 输出详细测试报告到控制台
*/
private logDetailedReport(report: TestReport): void {
log("========== ChatAI 批量测试报告 ==========");
log(`测试时间: ${new Date().toLocaleString()}`);
log(`总测试数: ${report.totalTests}`);
log(`成功数: ${report.successCount}`);
log(`失败数: ${report.failureCount}`);
log(`成功率: ${((report.successCount / report.totalTests) * 100).toFixed(2)}%`);
log(`总耗时: ${(report.totalDuration / 1000).toFixed(2)}`);
log(`平均耗时: ${(report.totalDuration / report.totalTests / 1000).toFixed(2)}秒/测试`);
this.isTestRunning = true;
log("\n===== 成功的角色ID =====");
if (report.successIds.length > 0) {
log(report.successIds.join(', '));
if (this.startTestButton) {
this.startTestButton.interactable = false;
}
if (this.resultLabel) {
this.resultLabel.string = "";
}
const startTime = Date.now();
const results: TestResult[] = [];
const successIds: number[] = [];
const failureIds: number[] = [];
log("[ChatAIBatchTest] 开始批量测试角色ID 10001-10030");
this.updateStatus("开始批量测试...");
const list = [10010, 10018];
// 测试角色ID 10001-10030
for (let i = 0; i <= list.length; i++) {
const roleId = list[i];
try {
// 测试单个角色
const result = await this.testSingleRole(roleId);
results.push(result);
if (result.success) {
successIds.push(roleId);
} else {
log("无");
failureIds.push(roleId);
}
log("\n===== 失败的角色ID =====");
if (report.failureIds.length > 0) {
log(report.failureIds.join(', '));
log("\n===== 失败详情 =====");
report.results
.filter(r => !r.success)
.forEach(result => {
log(`角色 ${result.roleId}: ${result.error}`);
});
} else {
log("无");
// 等待10秒(最后一个不需要等待)
if (roleId < 10030) {
this.updateStatus(`角色 ${roleId} 测试完成,等待10秒...`);
await this.delay(10);
}
log("\n===== 详细测试结果 =====");
report.results.forEach(result => {
const status = result.success ? "成功" : "失败";
const duration = (result.duration / 1000).toFixed(2);
const extra = result.success
? `响应: ${result.response?.substring(0, 30)}...`
: `错误: ${result.error}`;
log(`角色 ${result.roleId}: ${status} (${duration}s) - ${extra}`);
} catch (error) {
log(`[ChatAIBatchTest] 测试角色 ${roleId} 时发生意外错误: ${error}`);
results.push({
roleId,
success: false,
error: `意外错误: ${error}`,
duration: 0,
});
log("========================================");
failureIds.push(roleId);
}
}
/**
* 获取测试报告(供外部调用)
*/
public getTestReport(): TestReport {
return this.testReport;
const totalDuration = Date.now() - startTime;
// 生成测试报告
this.testReport = {
totalTests: 30,
successCount: successIds.length,
failureCount: failureIds.length,
successIds,
failureIds,
results,
totalDuration,
};
// 输出详细报告到控制台
this.logDetailedReport(this.testReport);
// 更新UI显示
this.updateResult(this.testReport);
this.updateStatus("批量测试完成!");
if (this.startTestButton) {
this.startTestButton.interactable = true;
}
/**
* 重置测试状态
*/
public resetTest(): void {
this.isTestRunning = false;
this.testReport = null;
this.isTestRunning = false;
}
if (this.statusLabel) {
this.statusLabel.string = "点击开始按钮进行批量测试";
}
/**
* 输出详细测试报告到控制台
*/
private logDetailedReport(report: TestReport): void {
log("========== ChatAI 批量测试报告 ==========");
log(`测试时间: ${new Date().toLocaleString()}`);
log(`总测试数: ${report.totalTests}`);
log(`成功数: ${report.successCount}`);
log(`失败数: ${report.failureCount}`);
log(
`成功率: ${((report.successCount / report.totalTests) * 100).toFixed(2)}%`
);
log(`总耗时: ${(report.totalDuration / 1000).toFixed(2)}`);
log(
`平均耗时: ${(report.totalDuration / report.totalTests / 1000).toFixed(
2
)}秒/测试`
);
if (this.resultLabel) {
this.resultLabel.string = "";
}
if (this.startTestButton) {
this.startTestButton.interactable = true;
}
log("\n===== 成功的角色ID =====");
if (report.successIds.length > 0) {
log(report.successIds.join(", "));
} else {
log("无");
}
}
log("\n===== 失败的角色ID =====");
if (report.failureIds.length > 0) {
log(report.failureIds.join(", "));
log("\n===== 失败详情 =====");
report.results
.filter((r) => !r.success)
.forEach((result) => {
log(`角色 ${result.roleId}: ${result.error}`);
});
} else {
log("无");
}
log("\n===== 详细测试结果 =====");
report.results.forEach((result) => {
const status = result.success ? "成功" : "失败";
const duration = (result.duration / 1000).toFixed(2);
const extra = result.success
? `响应: ${result.response?.substring(0, 30)}...`
: `错误: ${result.error}`;
log(`角色 ${result.roleId}: ${status} (${duration}s) - ${extra}`);
});
log("========================================");
}
/**
* 获取测试报告(供外部调用)
*/
public getTestReport(): TestReport {
return this.testReport;
}
/**
* 重置测试状态
*/
public resetTest(): void {
this.isTestRunning = false;
this.testReport = null;
if (this.statusLabel) {
this.statusLabel.string = "点击开始按钮进行批量测试";
}
if (this.resultLabel) {
this.resultLabel.string = "";
}
if (this.startTestButton) {
this.startTestButton.interactable = true;
}
}
}