2025-09-20 20:37:31 +08:00
|
|
|
import { ChatAIService } from '../core/ChatAIService';
|
2025-09-21 20:18:24 +08:00
|
|
|
import { logger } from "db://assets/Scripts/Main/Common/Logger";
|
2025-09-20 20:37:31 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ChatAI 批量测试运行器 - 纯脚本版本
|
|
|
|
|
*
|
|
|
|
|
* 用法:
|
|
|
|
|
* ```typescript
|
|
|
|
|
* const testRunner = new ChatAIBatchTestRunner();
|
|
|
|
|
* testRunner.runBatchTest();
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
export class ChatAIBatchTestRunner {
|
|
|
|
|
|
|
|
|
|
private isRunning: boolean = false;
|
|
|
|
|
|
|
|
|
|
constructor() {
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log("[ChatAIBatchTestRunner] 测试运行器已初始化");
|
2025-09-20 20:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 延时工具函数
|
|
|
|
|
* @param seconds 延时秒数
|
|
|
|
|
*/
|
|
|
|
|
private async delay(seconds: number): Promise<void> {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
setTimeout(resolve, seconds * 1000);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 运行批量测试
|
|
|
|
|
*/
|
|
|
|
|
public async runBatchTest(): Promise<void> {
|
|
|
|
|
if (this.isRunning) {
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log("[ChatAIBatchTestRunner] 测试已在运行中,请等待完成...");
|
2025-09-20 20:37:31 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.isRunning = true;
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log("[ChatAIBatchTestRunner] 开始批量测试角色ID 10001-10030");
|
|
|
|
|
logger.log("[ChatAIBatchTestRunner] 每次测试间隔10秒");
|
2025-09-20 20:37:31 +08:00
|
|
|
|
|
|
|
|
const startTime = Date.now();
|
|
|
|
|
const successIds: number[] = [];
|
|
|
|
|
const failureIds: number[] = [];
|
|
|
|
|
const errorDetails: { [roleId: number]: string } = {};
|
|
|
|
|
|
|
|
|
|
// 测试角色ID 10001-10030
|
|
|
|
|
for (let roleId = 10001; roleId <= 10030; roleId++) {
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log(`[ChatAIBatchTestRunner] 正在测试角色ID: ${roleId}`);
|
2025-09-20 20:37:31 +08:00
|
|
|
|
|
|
|
|
const testStartTime = Date.now();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await ChatAIService.Instance.sendMessage(roleId, "hello");
|
|
|
|
|
const testDuration = Date.now() - testStartTime;
|
|
|
|
|
|
|
|
|
|
if (response && response.trim() !== "") {
|
|
|
|
|
successIds.push(roleId);
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log(`[ChatAIBatchTestRunner] ✅ 角色 ${roleId} 测试成功 (${testDuration}ms)`);
|
|
|
|
|
logger.log(`[ChatAIBatchTestRunner] 响应预览: ${response.substring(0, 50)}...`);
|
2025-09-20 20:37:31 +08:00
|
|
|
} else {
|
|
|
|
|
failureIds.push(roleId);
|
|
|
|
|
errorDetails[roleId] = "返回空响应";
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log(`[ChatAIBatchTestRunner] ❌ 角色 ${roleId} 返回空响应 (${testDuration}ms)`);
|
2025-09-20 20:37:31 +08:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const testDuration = Date.now() - testStartTime;
|
|
|
|
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
|
|
|
|
|
|
|
|
failureIds.push(roleId);
|
|
|
|
|
errorDetails[roleId] = errorMessage;
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log(`[ChatAIBatchTestRunner] ❌ 角色 ${roleId} 测试失败 (${testDuration}ms): ${errorMessage}`);
|
2025-09-20 20:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 等待10秒(最后一个不需要等待)
|
|
|
|
|
if (roleId < 10030) {
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log(`[ChatAIBatchTestRunner] 等待10秒后继续下一个测试...`);
|
2025-09-20 20:37:31 +08:00
|
|
|
await this.delay(10);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const totalDuration = Date.now() - startTime;
|
|
|
|
|
|
|
|
|
|
// 输出最终报告
|
|
|
|
|
this.printFinalReport(successIds, failureIds, errorDetails, totalDuration);
|
|
|
|
|
|
|
|
|
|
this.isRunning = false;
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log("[ChatAIBatchTestRunner] 批量测试完成!");
|
2025-09-20 20:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 输出最终测试报告
|
|
|
|
|
*/
|
|
|
|
|
private printFinalReport(
|
|
|
|
|
successIds: number[],
|
|
|
|
|
failureIds: number[],
|
|
|
|
|
errorDetails: { [roleId: number]: string },
|
|
|
|
|
totalDuration: number
|
|
|
|
|
): void {
|
|
|
|
|
const totalTests = 30;
|
|
|
|
|
const successCount = successIds.length;
|
|
|
|
|
const failureCount = failureIds.length;
|
|
|
|
|
const successRate = ((successCount / totalTests) * 100).toFixed(2);
|
|
|
|
|
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log("\n" + "=".repeat(60));
|
|
|
|
|
logger.log(" ChatAI 批量测试报告");
|
|
|
|
|
logger.log("=".repeat(60));
|
|
|
|
|
logger.log(`测试时间: ${new Date().toLocaleString()}`);
|
|
|
|
|
logger.log(`总测试数: ${totalTests}`);
|
|
|
|
|
logger.log(`成功数量: ${successCount}`);
|
|
|
|
|
logger.log(`失败数量: ${failureCount}`);
|
|
|
|
|
logger.log(`成功率: ${successRate}%`);
|
|
|
|
|
logger.log(`总耗时: ${(totalDuration / 1000).toFixed(2)}秒`);
|
|
|
|
|
logger.log(`平均耗时: ${(totalDuration / totalTests / 1000).toFixed(2)}秒/测试`);
|
2025-09-20 20:37:31 +08:00
|
|
|
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log("\n" + "-".repeat(30) + " 成功的角色ID " + "-".repeat(30));
|
2025-09-20 20:37:31 +08:00
|
|
|
if (successIds.length > 0) {
|
|
|
|
|
const successList = this.formatIdList(successIds);
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log(successList);
|
2025-09-20 20:37:31 +08:00
|
|
|
} else {
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log("无成功案例");
|
2025-09-20 20:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log("\n" + "-".repeat(30) + " 失败的角色ID " + "-".repeat(30));
|
2025-09-20 20:37:31 +08:00
|
|
|
if (failureIds.length > 0) {
|
|
|
|
|
const failureList = this.formatIdList(failureIds);
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log(failureList);
|
2025-09-20 20:37:31 +08:00
|
|
|
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log("\n" + "-".repeat(25) + " 失败详情 " + "-".repeat(25));
|
2025-09-20 20:37:31 +08:00
|
|
|
failureIds.forEach(roleId => {
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log(`角色 ${roleId}: ${errorDetails[roleId]}`);
|
2025-09-20 20:37:31 +08:00
|
|
|
});
|
|
|
|
|
} else {
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log("无失败案例");
|
2025-09-20 20:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log("\n" + "=".repeat(60));
|
2025-09-20 20:37:31 +08:00
|
|
|
|
|
|
|
|
// 输出简洁版结果供复制使用
|
2025-09-21 20:18:24 +08:00
|
|
|
logger.log("\n简洁结果:");
|
|
|
|
|
logger.log(`成功(${successCount}): ${successIds.join(',')}`);
|
|
|
|
|
logger.log(`失败(${failureCount}): ${failureIds.join(',')}`);
|
2025-09-20 20:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 格式化ID列表为易读格式
|
|
|
|
|
*/
|
|
|
|
|
private formatIdList(ids: number[]): string {
|
|
|
|
|
if (ids.length === 0) return "无";
|
|
|
|
|
|
|
|
|
|
const sortedIds = ids.sort((a, b) => a - b);
|
|
|
|
|
const groups: string[] = [];
|
|
|
|
|
let start = sortedIds[0];
|
|
|
|
|
let end = sortedIds[0];
|
|
|
|
|
|
|
|
|
|
for (let i = 1; i < sortedIds.length; i++) {
|
|
|
|
|
if (sortedIds[i] === end + 1) {
|
|
|
|
|
end = sortedIds[i];
|
|
|
|
|
} else {
|
|
|
|
|
if (start === end) {
|
|
|
|
|
groups.push(`${start}`);
|
|
|
|
|
} else if (end === start + 1) {
|
|
|
|
|
groups.push(`${start},${end}`);
|
|
|
|
|
} else {
|
|
|
|
|
groups.push(`${start}-${end}`);
|
|
|
|
|
}
|
|
|
|
|
start = end = sortedIds[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加最后一组
|
|
|
|
|
if (start === end) {
|
|
|
|
|
groups.push(`${start}`);
|
|
|
|
|
} else if (end === start + 1) {
|
|
|
|
|
groups.push(`${start},${end}`);
|
|
|
|
|
} else {
|
|
|
|
|
groups.push(`${start}-${end}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return groups.join(', ');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前运行状态
|
|
|
|
|
*/
|
|
|
|
|
public isTestRunning(): boolean {
|
|
|
|
|
return this.isRunning;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 全局实例,可以直接调用
|
|
|
|
|
export const chatAIBatchTester = new ChatAIBatchTestRunner();
|
|
|
|
|
|
|
|
|
|
// 便捷的全局函数
|
|
|
|
|
export async function runChatAIBatchTest(): Promise<void> {
|
|
|
|
|
await chatAIBatchTester.runBatchTest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用示例:
|
|
|
|
|
// import { runChatAIBatchTest } from 'path/to/ChatAIBatchTestRunner';
|
|
|
|
|
// runChatAIBatchTest();
|