聊天相关fix
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
import { ChatAIService } from '../core/ChatAIService';
|
||||
|
||||
/**
|
||||
* ChatAI 批量测试运行器 - 纯脚本版本
|
||||
*
|
||||
* 用法:
|
||||
* ```typescript
|
||||
* const testRunner = new ChatAIBatchTestRunner();
|
||||
* testRunner.runBatchTest();
|
||||
* ```
|
||||
*/
|
||||
export class ChatAIBatchTestRunner {
|
||||
|
||||
private isRunning: boolean = false;
|
||||
|
||||
constructor() {
|
||||
console.log("[ChatAIBatchTestRunner] 测试运行器已初始化");
|
||||
}
|
||||
|
||||
/**
|
||||
* 延时工具函数
|
||||
* @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) {
|
||||
console.log("[ChatAIBatchTestRunner] 测试已在运行中,请等待完成...");
|
||||
return;
|
||||
}
|
||||
|
||||
this.isRunning = true;
|
||||
console.log("[ChatAIBatchTestRunner] 开始批量测试角色ID 10001-10030");
|
||||
console.log("[ChatAIBatchTestRunner] 每次测试间隔10秒");
|
||||
|
||||
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++) {
|
||||
console.log(`[ChatAIBatchTestRunner] 正在测试角色ID: ${roleId}`);
|
||||
|
||||
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);
|
||||
console.log(`[ChatAIBatchTestRunner] ✅ 角色 ${roleId} 测试成功 (${testDuration}ms)`);
|
||||
console.log(`[ChatAIBatchTestRunner] 响应预览: ${response.substring(0, 50)}...`);
|
||||
} else {
|
||||
failureIds.push(roleId);
|
||||
errorDetails[roleId] = "返回空响应";
|
||||
console.log(`[ChatAIBatchTestRunner] ❌ 角色 ${roleId} 返回空响应 (${testDuration}ms)`);
|
||||
}
|
||||
} catch (error) {
|
||||
const testDuration = Date.now() - testStartTime;
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
|
||||
failureIds.push(roleId);
|
||||
errorDetails[roleId] = errorMessage;
|
||||
console.log(`[ChatAIBatchTestRunner] ❌ 角色 ${roleId} 测试失败 (${testDuration}ms): ${errorMessage}`);
|
||||
}
|
||||
|
||||
// 等待10秒(最后一个不需要等待)
|
||||
if (roleId < 10030) {
|
||||
console.log(`[ChatAIBatchTestRunner] 等待10秒后继续下一个测试...`);
|
||||
await this.delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
const totalDuration = Date.now() - startTime;
|
||||
|
||||
// 输出最终报告
|
||||
this.printFinalReport(successIds, failureIds, errorDetails, totalDuration);
|
||||
|
||||
this.isRunning = false;
|
||||
console.log("[ChatAIBatchTestRunner] 批量测试完成!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出最终测试报告
|
||||
*/
|
||||
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);
|
||||
|
||||
console.log("\n" + "=".repeat(60));
|
||||
console.log(" ChatAI 批量测试报告");
|
||||
console.log("=".repeat(60));
|
||||
console.log(`测试时间: ${new Date().toLocaleString()}`);
|
||||
console.log(`总测试数: ${totalTests}`);
|
||||
console.log(`成功数量: ${successCount}`);
|
||||
console.log(`失败数量: ${failureCount}`);
|
||||
console.log(`成功率: ${successRate}%`);
|
||||
console.log(`总耗时: ${(totalDuration / 1000).toFixed(2)}秒`);
|
||||
console.log(`平均耗时: ${(totalDuration / totalTests / 1000).toFixed(2)}秒/测试`);
|
||||
|
||||
console.log("\n" + "-".repeat(30) + " 成功的角色ID " + "-".repeat(30));
|
||||
if (successIds.length > 0) {
|
||||
const successList = this.formatIdList(successIds);
|
||||
console.log(successList);
|
||||
} else {
|
||||
console.log("无成功案例");
|
||||
}
|
||||
|
||||
console.log("\n" + "-".repeat(30) + " 失败的角色ID " + "-".repeat(30));
|
||||
if (failureIds.length > 0) {
|
||||
const failureList = this.formatIdList(failureIds);
|
||||
console.log(failureList);
|
||||
|
||||
console.log("\n" + "-".repeat(25) + " 失败详情 " + "-".repeat(25));
|
||||
failureIds.forEach(roleId => {
|
||||
console.log(`角色 ${roleId}: ${errorDetails[roleId]}`);
|
||||
});
|
||||
} else {
|
||||
console.log("无失败案例");
|
||||
}
|
||||
|
||||
console.log("\n" + "=".repeat(60));
|
||||
|
||||
// 输出简洁版结果供复制使用
|
||||
console.log("\n简洁结果:");
|
||||
console.log(`成功(${successCount}): ${successIds.join(',')}`);
|
||||
console.log(`失败(${failureCount}): ${failureIds.join(',')}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化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();
|
||||
Reference in New Issue
Block a user