-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-smart-pooling.js
More file actions
148 lines (125 loc) · 4.12 KB
/
Copy pathtest-smart-pooling.js
File metadata and controls
148 lines (125 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// 测试智能账号池和自动切换功能
const API_BASE = 'https://ai-api-proxy.2358314123.workers.dev';
async function testSmartPooling() {
console.log('🧪 测试智能账号池功能\n');
try {
// 测试1: 简单聊天请求
console.log('[测试 1] 发送聊天请求...');
const chatResponse = await fetch(`${API_BASE}/kiro/v1/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'claude-sonnet-4.5',
messages: [
{
role: 'user',
content: 'Say "Hello from Kiro!" and nothing else.'
}
],
stream: false,
max_tokens: 50
})
});
console.log(` - 状态码: ${chatResponse.status}`);
if (chatResponse.ok) {
const data = await chatResponse.json();
console.log(` - 响应: ${data.choices[0].message.content}`);
console.log(' ✓ 测试 1 通过');
} else {
const error = await chatResponse.text();
console.log(` ✗ 测试 1 失败: ${error}`);
}
console.log('');
// 测试2: 流式响应
console.log('[测试 2] 发送流式请求...');
const streamResponse = await fetch(`${API_BASE}/kiro/v1/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'claude-sonnet-4.5',
messages: [
{
role: 'user',
content: 'Count from 1 to 5.'
}
],
stream: true,
max_tokens: 100
})
});
console.log(` - 状态码: ${streamResponse.status}`);
if (streamResponse.ok) {
const reader = streamResponse.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
let chunkCount = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
chunkCount++;
}
console.log(` - 接收到 ${chunkCount} 个数据块`);
console.log(` - 总大小: ${buffer.length} 字节`);
console.log(' ✓ 测试 2 通过');
} else {
const error = await streamResponse.text();
console.log(` ✗ 测试 2 失败: ${error}`);
}
console.log('');
// 测试3: 连续请求(测试负载均衡)
console.log('[测试 3] 发送 5 个连续请求...');
const requests = [];
for (let i = 1; i <= 5; i++) {
requests.push(
fetch(`${API_BASE}/kiro/v1/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'claude-sonnet-4.5',
messages: [
{
role: 'user',
content: `Say "Request ${i}" and nothing else.`
}
],
stream: false,
max_tokens: 20
})
})
);
}
const results = await Promise.all(requests);
const successCount = results.filter(r => r.ok).length;
console.log(` - 成功: ${successCount}/5`);
console.log(` - 失败: ${5 - successCount}/5`);
if (successCount === 5) {
console.log(' ✓ 测试 3 通过');
} else {
console.log(` ⚠ 测试 3 部分通过 (${successCount}/5)`);
}
console.log('');
// 测试4: 检查账号池状态
console.log('[测试 4] 检查账号池状态...');
const healthResponse = await fetch(`${API_BASE}/health`);
if (healthResponse.ok) {
const health = await healthResponse.json();
console.log(` - 总账号数: ${health.accounts?.total || 'N/A'}`);
console.log(` - Kiro 账号: ${health.accounts?.kiro || 'N/A'}`);
console.log(' ✓ 测试 4 通过');
} else {
console.log(' ✗ 测试 4 失败');
}
console.log('\n✅ 所有测试完成!');
} catch (error) {
console.error('\n❌ 测试失败:', error.message);
process.exit(1);
}
}
testSmartPooling();