-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_engine.py
More file actions
1695 lines (1430 loc) · 75 KB
/
Copy pathtest_engine.py
File metadata and controls
1695 lines (1430 loc) · 75 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
测试引擎 - 执行代码生成、文生文和文生图测试(带重试机制)
版本 2.2 - 增强版:支持代码生成、写作能力、文生图三类测评
"""
import json
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
import base64
import re
import time
import random
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime
from functools import wraps
from dataclasses import dataclass, field, asdict
from typing import Optional, Dict, List, Any
def sanitize_filename(name: str) -> str:
"""清理文件名中的非法字符"""
# Windows和Linux都不允许的字符
invalid_chars = r'<>:"/\|?*'
# 中文括号转英文括号
name = name.replace('(', '(').replace(')', ')')
# 替换非法字符为下划线
for char in invalid_chars:
name = name.replace(char, '_')
# 去除首尾空格和点
name = name.strip(' .')
# 限制文件名长度
if len(name) > 100:
name = name[:100]
return name
@dataclass
class TokenUsage:
"""Token使用统计"""
prompt_tokens: int = 0
completion_tokens: int = 0
total_tokens: int = 0
def add(self, other: 'TokenUsage'):
self.prompt_tokens += other.prompt_tokens
self.completion_tokens += other.completion_tokens
self.total_tokens += other.total_tokens
@dataclass
class TestStats:
"""测试统计信息"""
total_cases: int = 0
success_count: int = 0
failed_count: int = 0
html_extracted_count: int = 0
no_html_count: int = 0
total_tokens: TokenUsage = field(default_factory=TokenUsage)
total_time_seconds: float = 0.0 # 多线程总耗时(墙钟时间)
sum_case_time_seconds: float = 0.0 # 单case耗时总和(真实累计时间)
avg_time_per_case: float = 0.0 # 单case平均耗时
avg_output_tokens_per_case: float = 0.0 # 单case平均输出tokens
avg_tokens_per_second: float = 0.0 # 平均输出速率 (tokens/s)
timeout_count: int = 0
retry_count: int = 0
incomplete_count: int = 0 # 输出不完整次数
def to_dict(self) -> Dict:
return {
"total_cases": self.total_cases,
"success_count": self.success_count,
"failed_count": self.failed_count,
"html_extracted_count": self.html_extracted_count,
"no_html_count": self.no_html_count,
"total_tokens": asdict(self.total_tokens),
"total_time_seconds": round(self.total_time_seconds, 2),
"sum_case_time_seconds": round(self.sum_case_time_seconds, 2),
"avg_time_per_case": round(self.avg_time_per_case, 2),
"avg_output_tokens_per_case": round(self.avg_output_tokens_per_case, 2),
"avg_tokens_per_second": round(self.avg_tokens_per_second, 2),
"timeout_count": self.timeout_count,
"retry_count": self.retry_count,
"incomplete_count": self.incomplete_count
}
class TestEngine:
# 重试配置
MAX_RETRIES = 3
BASE_DELAY = 2
MAX_DELAY = 30
REQUEST_TIMEOUT = 1200 # 请求超时时间(秒)
# 不完整响应检测配置
INCOMPLETE_RETRY_MAX = 2 # 不完整响应最大重试次数
CONTINUE_CONVERSATION_MAX = 3 # 连续对话最大轮数(用于续写被截断的内容)
def __init__(self, api_url, api_key, text_model, image_model,
max_threads, output_dir, log_callback=None, progress_callback=None,
enable_thinking=False, max_tokens=None):
"""
初始化测试引擎
Args:
api_url: API地址
api_key: API密钥
text_model: 文生文模型
image_model: 文生图模型
max_threads: 最大线程数
output_dir: 输出目录
log_callback: 日志回调
progress_callback: 进度回调
enable_thinking: 是否启用thinking模式(兼容DeepSeek等支持思维链的模型)
max_tokens: 最大输出tokens,默认None表示使用最大值
"""
self.api_url = api_url.rstrip("/")
self.api_key = api_key
self.text_model = text_model
self.image_model = image_model
self.max_threads = max_threads
self.output_dir = Path(output_dir)
self.log = log_callback or print
self.update_progress = progress_callback or (lambda x: None)
# thinking模式配置
self.enable_thinking = enable_thinking
# max_tokens:默认设置为较大值,兼容各家API
self.max_tokens = max_tokens if max_tokens else 16384 # 默认16K,可配置
self.is_running = True
self.results = {"text": [], "image": [], "writing": []}
# 统计信息
self.text_stats = TestStats()
self.image_stats = TestStats()
self.writing_stats = TestStats()
self.start_time: Optional[float] = None
self.end_time: Optional[float] = None
# 创建带有自动重试机制的HTTP Session
self.session = self._create_robust_session()
# 确保输出目录存在
(self.output_dir / "text").mkdir(parents=True, exist_ok=True)
(self.output_dir / "image").mkdir(parents=True, exist_ok=True)
(self.output_dir / "writing").mkdir(parents=True, exist_ok=True)
(self.output_dir / "website").mkdir(parents=True, exist_ok=True)
(self.output_dir / "logs").mkdir(parents=True, exist_ok=True)
def _create_robust_session(self) -> requests.Session:
"""创建带有自动重试和连接池的HTTP Session"""
session = requests.Session()
# 配置重试策略
retry_strategy = Retry(
total=3, # 总重试次数
backoff_factor=1, # 退避因子:1, 2, 4秒
status_forcelist=[429, 500, 502, 503, 504], # 需要重试的状态码
allowed_methods=["POST"], # 允许重试的方法
raise_on_status=False # 不自动抛出异常,让我们手动处理
)
# 配置HTTP适配器
adapter = HTTPAdapter(
max_retries=retry_strategy,
pool_connections=10, # 连接池大小
pool_maxsize=20, # 最大连接数
pool_block=False
)
session.mount("http://", adapter)
session.mount("https://", adapter)
return session
def stop(self):
"""停止测试"""
self.is_running = False
def get_stats_summary(self) -> Dict:
"""获取统计摘要"""
total_time = self.end_time - self.start_time if self.end_time and self.start_time else 0
return {
"text_stats": self.text_stats.to_dict(),
"image_stats": self.image_stats.to_dict(),
"total_time_seconds": round(total_time, 2),
"total_tokens": {
"prompt_tokens": self.text_stats.total_tokens.prompt_tokens + self.image_stats.total_tokens.prompt_tokens,
"completion_tokens": self.text_stats.total_tokens.completion_tokens + self.image_stats.total_tokens.completion_tokens,
"total_tokens": self.text_stats.total_tokens.total_tokens + self.image_stats.total_tokens.total_tokens
}
}
def load_test_cases(self, test_type):
"""加载测试用例"""
base_dir = Path(__file__).parent
if test_type == "text":
case_file = base_dir / "test_cases" / "text_cases.json"
elif test_type == "writing":
case_file = base_dir / "test_cases" / "writing_cases.json"
else:
case_file = base_dir / "test_cases" / "image_cases.json"
if not case_file.exists():
self.log(f"警告: 测试用例文件不存在 {case_file}")
return []
with open(case_file, "r", encoding="utf-8") as f:
data = json.load(f)
return data.get("cases", [])
def call_api_with_retry(self, prompt, model, is_image=False, case_id="") -> Dict[str, Any]:
"""
调用API(带重试机制),自动兼容流式和非流式响应
Args:
prompt: 提示词
model: 模型名称
is_image: 是否为图像生成
case_id: 案例ID(用于日志)
Returns:
包含响应内容、token使用量、耗时等信息的字典
"""
# 首先尝试流式响应
try:
return self._call_api_streaming(prompt, model, is_image, case_id)
except Exception as e:
error_str = str(e).lower()
# 判断是否应该切换到非流式模式
# 1. 如果明确提示不支持流式
# 2. 如果是SSE相关错误
# 3. 如果已经重试多次仍然失败
should_try_non_stream = any([
"stream" in error_str and "not" in error_str,
"sse" in error_str,
"event-stream" in error_str,
"chunk" in error_str and "invalid" in error_str,
"已重试" in error_str # 已经多次重试失败
])
if should_try_non_stream:
self.log(f" 💡 [{case_id}] 检测到流式响应不兼容,尝试非流式模式...")
try:
return self._call_api_non_streaming(prompt, model, is_image, case_id)
except Exception as non_stream_error:
# 如果非流式也失败,抛出更详细的错误
raise Exception(f"流式和非流式响应均失败。流式错误: {str(e)[:100]}; 非流式错误: {str(non_stream_error)[:100]}")
else:
# 如果不是流式相关问题,直接抛出原始错误
raise
def _call_api_streaming(self, prompt, model, is_image=False, case_id="") -> Dict[str, Any]:
"""流式API调用(原有逻辑)"""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.api_key}",
"Expect": "", # 禁用100-continue
"Connection": "keep-alive",
"Accept": "text/event-stream" # SSE流式响应
}
# 构建payload,兼容OpenAI格式
# 对于推理模型使用流式响应,避免中转服务超时
payload = {
"model": model,
"messages": [{"role": "user", "content": prompt}],
"max_tokens": self.max_tokens,
"stream": True # 启用流式响应,避免Response ended prematurely
}
# 添加thinking模式支持(兼容多种格式)
if self.enable_thinking:
# 方式1: DeepSeek V3.2风格
payload["enable_thinking"] = True
# 方式2: 也可以通过extra_body传递(某些SDK需要)
# 这里直接在payload中添加,兼容更多情况
endpoint = f"{self.api_url}/chat/completions"
last_exception = None
total_retry_count = 0
incomplete_retry_count = 0
request_start_time = time.time()
for attempt in range(self.MAX_RETRIES + 1):
if not self.is_running:
raise Exception("测试已停止")
attempt_start_time = time.time()
response = None
try:
self.log(f" [{case_id}] 开始请求 (第{attempt + 1}次尝试)...")
# 使用流式响应避免中转服务超时
response = self.session.post(
endpoint,
json=payload,
headers=headers,
timeout=(30, self.REQUEST_TIMEOUT),
stream=True
)
response.raise_for_status()
# 显式设置编码为UTF-8 (修复Windows乱码问题)
response.encoding = 'utf-8'
# 收集SSE流式响应数据
collected_content = ""
collected_reasoning = ""
token_usage = TokenUsage()
finish_reason = None
for line in response.iter_lines(decode_unicode=True):
if not self.is_running:
raise Exception("测试已停止")
if not line:
continue
# SSE格式: data: {...}
if line.startswith("data: "):
data_str = line[6:] # 去掉 "data: " 前缀
if data_str.strip() == "[DONE]":
break
try:
chunk = json.loads(data_str)
# 提取delta内容
if "choices" in chunk and len(chunk["choices"]) > 0:
delta = chunk["choices"][0].get("delta", {})
# 收集content
if "content" in delta and delta["content"]:
collected_content += delta["content"]
# 收集reasoning_content (DeepSeek推理模型)
if "reasoning_content" in delta and delta["reasoning_content"]:
collected_reasoning += delta["reasoning_content"]
# 获取finish_reason
if chunk["choices"][0].get("finish_reason"):
finish_reason = chunk["choices"][0]["finish_reason"]
# 提取usage (某些API在最后一个chunk返回)
if "usage" in chunk and chunk["usage"]:
usage = chunk["usage"]
token_usage.prompt_tokens = usage.get("prompt_tokens", 0)
token_usage.completion_tokens = usage.get("completion_tokens", 0)
token_usage.total_tokens = usage.get("total_tokens", 0)
except json.JSONDecodeError:
continue
attempt_duration = time.time() - attempt_start_time
self.log(f" [{case_id}] 请求完成,耗时 {attempt_duration:.1f}秒")
# 如果content为空但reasoning_content有内容,使用reasoning_content
if not collected_content and collected_reasoning:
collected_content = collected_reasoning
self.log(f" 📝 [{case_id}] 使用reasoning_content作为响应内容")
# 构建兼容的response_json格式
response_json = {
"choices": [{
"message": {
"content": collected_content,
"reasoning_content": collected_reasoning if collected_reasoning else None
},
"finish_reason": finish_reason
}],
"usage": {
"prompt_tokens": token_usage.prompt_tokens,
"completion_tokens": token_usage.completion_tokens,
"total_tokens": token_usage.total_tokens
}
}
# 如果没有从流中获取到usage,估算tokens
if token_usage.total_tokens == 0:
# 粗略估算:4个字符约等于1个token
estimated_completion = len(collected_content + collected_reasoning) // 4
token_usage.completion_tokens = estimated_completion
token_usage.total_tokens = estimated_completion
self.log(f" [{case_id}] Tokens (估算): 输出≈{estimated_completion}")
else:
self.log(f" [{case_id}] Tokens: 输入={token_usage.prompt_tokens}, 输出={token_usage.completion_tokens}, 总计={token_usage.total_tokens}")
# 检查响应完整性(finish_reason)
is_incomplete = False
if finish_reason == "length":
is_incomplete = True
self.log(f" ⚠️ [{case_id}] 输出达到max_tokens上限被截断 (finish_reason=length)")
self.log(f" 💡 [{case_id}] 提示: 截断无法通过重试解决,将检查HTML是否已完整")
# 计算输出速率
tokens_per_second = 0.0
if attempt_duration > 0 and token_usage.completion_tokens > 0:
tokens_per_second = token_usage.completion_tokens / attempt_duration
self.log(f" [{case_id}] 输出速率: {tokens_per_second:.1f} tokens/s")
# 注意: 不对length截断进行重试,因为重试不能解决max_tokens限制问题
# 后续会在HTML提取时检测内容是否完整
total_duration = time.time() - request_start_time
return {
"response": response_json,
"token_usage": token_usage,
"duration_seconds": round(total_duration, 2),
"retry_count": total_retry_count,
"incomplete_retry_count": incomplete_retry_count,
"is_incomplete": is_incomplete,
"finish_reason": finish_reason,
"tokens_per_second": round(tokens_per_second, 2),
"success": True
}
except requests.exceptions.Timeout as e:
attempt_duration = time.time() - attempt_start_time
last_exception = Exception(f"请求超时 ({self.REQUEST_TIMEOUT}秒): {str(e)}")
self.log(f" ⏰ [{case_id}] 请求超时! 已等待 {attempt_duration:.1f}秒 (超时限制: {self.REQUEST_TIMEOUT}秒)")
except requests.exceptions.ChunkedEncodingError as e:
# 处理 "Response ended prematurely" 错误
attempt_duration = time.time() - attempt_start_time
last_exception = Exception(f"响应传输中断: {str(e)}")
self.log(f" 📡 [{case_id}] 响应传输中断 (Response ended prematurely),耗时 {attempt_duration:.1f}秒")
self.log(f" 💡 [{case_id}] 这通常是服务器负载过高导致的,将增加延迟后重试")
except requests.exceptions.ConnectionError as e:
attempt_duration = time.time() - attempt_start_time
error_str = str(e)
# 检测是否是 Response ended prematurely 类型的错误
if "ended prematurely" in error_str.lower() or "incomplete" in error_str.lower():
last_exception = Exception(f"响应传输中断: {error_str}")
self.log(f" 📡 [{case_id}] 响应传输中断,耗时 {attempt_duration:.1f}秒")
else:
last_exception = Exception(f"连接错误: {error_str}")
self.log(f" 🔌 [{case_id}] 连接错误,耗时 {attempt_duration:.1f}秒: {error_str[:100]}")
except requests.exceptions.HTTPError as e:
attempt_duration = time.time() - attempt_start_time
status_code = response.status_code if response is not None else 'unknown'
# 检查是否是可重试的错误
if isinstance(status_code, int) and status_code in [429, 500, 502, 503, 504]:
error_messages = {
429: "请求过于频繁",
500: "服务器内部错误",
502: "网关错误",
503: "服务暂时不可用",
504: "网关超时"
}
error_desc = error_messages.get(status_code, "HTTP错误")
last_exception = Exception(f"HTTP {status_code} ({error_desc}): {str(e)}")
self.log(f" 🚫 [{case_id}] HTTP {status_code} ({error_desc}),耗时 {attempt_duration:.1f}秒")
else:
# 不可重试的错误,直接抛出
raise Exception(f"API调用失败: HTTP {status_code} - {str(e)}")
except json.JSONDecodeError as e:
attempt_duration = time.time() - attempt_start_time
last_exception = Exception(f"响应JSON解析失败: {str(e)}")
self.log(f" ❌ [{case_id}] 响应JSON解析失败: {str(e)[:100]}")
# 如果能获取到响应文本,记录下来便于调试
if response is not None:
try:
raw_text = response.text[:500] if response.text else "空响应"
self.log(f" 📋 [{case_id}] 原始响应: {raw_text}")
except:
pass
except Exception as e:
attempt_duration = time.time() - attempt_start_time
error_str = str(e)
# 检测常见的网络中断错误
if any(keyword in error_str.lower() for keyword in ["prematurely", "incomplete", "broken pipe", "reset by peer"]):
last_exception = Exception(f"网络传输中断: {error_str}")
self.log(f" 📡 [{case_id}] 网络传输中断,耗时 {attempt_duration:.1f}秒: {error_str[:100]}")
else:
last_exception = Exception(f"未知错误: {error_str}")
self.log(f" ❌ [{case_id}] 未知错误: {error_str[:100]}")
# 重试逻辑 - 增加延迟时间
if attempt < self.MAX_RETRIES:
total_retry_count += 1
# 使用更长的基础延迟,特别是对于网络中断错误
base_delay = self.BASE_DELAY * 2 if "传输中断" in str(last_exception) or "prematurely" in str(last_exception).lower() else self.BASE_DELAY
delay = min(base_delay * (2 ** attempt) + random.uniform(0, 2), self.MAX_DELAY)
self.log(f" 🔄 [{case_id}] 第{attempt + 1}次尝试失败,{delay:.1f}秒后重试 (剩余{self.MAX_RETRIES - attempt}次)...")
time.sleep(delay)
total_duration = time.time() - request_start_time
# 记录失败日志到文件
self._log_failure(case_id, prompt, model, last_exception, total_duration)
raise Exception(f"API调用失败(已重试{self.MAX_RETRIES}次,总耗时{total_duration:.1f}秒): {str(last_exception)}")
def _call_api_non_streaming(self, prompt, model, is_image=False, case_id="") -> Dict[str, Any]:
"""非流式API调用(兼容更多模型)"""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.api_key}",
"Accept": "application/json"
}
payload = {
"model": model,
"messages": [{"role": "user", "content": prompt}],
"max_tokens": self.max_tokens,
"stream": False # 非流式响应
}
# 可选:添加thinking模式(某些模型可能不支持)
if self.enable_thinking:
payload["enable_thinking"] = True
endpoint = f"{self.api_url}/chat/completions"
last_exception = None
total_retry_count = 0
request_start_time = time.time()
for attempt in range(self.MAX_RETRIES + 1):
if not self.is_running:
raise Exception("测试已停止")
attempt_start_time = time.time()
response = None
try:
self.log(f" [{case_id}] 开始非流式请求 (第{attempt + 1}次尝试)...")
response = self.session.post(
endpoint,
json=payload,
headers=headers,
timeout=(30, self.REQUEST_TIMEOUT)
)
response.raise_for_status()
# 显式设置编码为UTF-8 (修复Windows乱码问题)
response.encoding = 'utf-8'
attempt_duration = time.time() - attempt_start_time
self.log(f" [{case_id}] 请求完成,耗时 {attempt_duration:.1f}秒")
# 解析JSON响应
response_json = response.json()
# 兼容多种响应格式
content = ""
reasoning_content = ""
finish_reason = None
token_usage = TokenUsage()
# 提取choices和message
if "choices" in response_json and len(response_json["choices"]) > 0:
choice = response_json["choices"][0]
# 提取message内容
message = choice.get("message", {})
content = message.get("content", "")
reasoning_content = message.get("reasoning_content", "")
# 提取finish_reason
finish_reason = choice.get("finish_reason")
# 如果content为空但reasoning_content有内容,使用reasoning_content
if not content and reasoning_content:
content = reasoning_content
self.log(f" 📝 [{case_id}] 使用reasoning_content作为响应内容")
# 提取usage
if "usage" in response_json:
usage = response_json["usage"]
token_usage.prompt_tokens = usage.get("prompt_tokens", 0)
token_usage.completion_tokens = usage.get("completion_tokens", 0)
token_usage.total_tokens = usage.get("total_tokens", 0)
# 如果没有usage信息,估算tokens
if token_usage.total_tokens == 0:
estimated_completion = len(content + reasoning_content) // 4
token_usage.completion_tokens = estimated_completion
token_usage.total_tokens = estimated_completion
self.log(f" [{case_id}] Tokens (估算): 输出≈{estimated_completion}")
else:
self.log(f" [{case_id}] Tokens: 输入={token_usage.prompt_tokens}, 输出={token_usage.completion_tokens}, 总计={token_usage.total_tokens}")
# 检查响应完整性
is_incomplete = False
if finish_reason == "length":
is_incomplete = True
self.log(f" ⚠️ [{case_id}] 输出达到max_tokens上限被截断")
# 计算输出速率
tokens_per_second = 0.0
if attempt_duration > 0 and token_usage.completion_tokens > 0:
tokens_per_second = token_usage.completion_tokens / attempt_duration
self.log(f" [{case_id}] 输出速率: {tokens_per_second:.1f} tokens/s")
total_duration = time.time() - request_start_time
return {
"response": response_json,
"token_usage": token_usage,
"duration_seconds": round(total_duration, 2),
"retry_count": total_retry_count,
"incomplete_retry_count": 0,
"is_incomplete": is_incomplete,
"finish_reason": finish_reason,
"tokens_per_second": round(tokens_per_second, 2),
"success": True
}
except requests.exceptions.Timeout as e:
attempt_duration = time.time() - attempt_start_time
last_exception = Exception(f"请求超时: {str(e)}")
self.log(f" ⏰ [{case_id}] 请求超时! 已等待 {attempt_duration:.1f}秒")
except requests.exceptions.HTTPError as e:
attempt_duration = time.time() - attempt_start_time
status_code = response.status_code if response is not None else 'unknown'
# 记录详细错误信息
error_body = ""
try:
if response is not None and response.text:
error_body = response.text[:500]
self.log(f" 📋 [{case_id}] 错误响应: {error_body}")
except:
pass
# 可重试的错误
if isinstance(status_code, int) and status_code in [429, 500, 502, 503, 504]:
error_messages = {
429: "请求过于频繁",
500: "服务器内部错误",
502: "网关错误",
503: "服务暂时不可用",
504: "网关超时"
}
error_desc = error_messages.get(status_code, "HTTP错误")
last_exception = Exception(f"HTTP {status_code} ({error_desc}): {str(e)}")
self.log(f" 🚫 [{case_id}] HTTP {status_code} ({error_desc})")
else:
# 不可重试的错误
raise Exception(f"API调用失败: HTTP {status_code} - {error_body if error_body else str(e)}")
except json.JSONDecodeError as e:
attempt_duration = time.time() - attempt_start_time
last_exception = Exception(f"响应JSON解析失败: {str(e)}")
self.log(f" ❌ [{case_id}] 响应JSON解析失败")
if response is not None:
try:
raw_text = response.text[:500] if response.text else "空响应"
self.log(f" 📋 [{case_id}] 原始响应: {raw_text}")
except:
pass
except Exception as e:
attempt_duration = time.time() - attempt_start_time
last_exception = Exception(f"未知错误: {str(e)}")
self.log(f" ❌ [{case_id}] 未知错误: {str(e)[:100]}")
# 重试逻辑
if attempt < self.MAX_RETRIES:
total_retry_count += 1
delay = min(self.BASE_DELAY * (2 ** attempt) + random.uniform(0, 2), self.MAX_DELAY)
self.log(f" 🔄 [{case_id}] 第{attempt + 1}次尝试失败,{delay:.1f}秒后重试...")
time.sleep(delay)
total_duration = time.time() - request_start_time
# 记录失败日志到文件
self._log_failure(case_id, prompt, model, last_exception, total_duration)
raise Exception(f"非流式API调用失败(已重试{self.MAX_RETRIES}次,总耗时{total_duration:.1f}秒): {str(last_exception)}")
def _log_failure(self, case_id, prompt, model, exception, duration):
"""记录失败日志到文件"""
try:
log_dir = self.output_dir / "logs"
log_dir.mkdir(parents=True, exist_ok=True)
log_file = log_dir / f"failures_{datetime.now().strftime('%Y%m%d')}.log"
with open(log_file, "a", encoding="utf-8") as f:
f.write(f"\n{'='*80}\n")
f.write(f"时间: {datetime.now().isoformat()}\n")
f.write(f"案例ID: {case_id}\n")
f.write(f"模型: {model}\n")
f.write(f"耗时: {duration:.1f}秒\n")
f.write(f"错误: {str(exception)}\n")
f.write(f"提示词前100字: {prompt[:100]}...\n")
f.write(f"{'='*80}\n")
except Exception as e:
self.log(f" ⚠️ 写入失败日志失败: {str(e)}")
def continue_conversation(self, messages: List[Dict], model: str, case_id: str = "") -> Dict[str, Any]:
"""
连续对话 - 用于续写被截断的内容(带重试)
Args:
messages: 对话历史
model: 模型名称
case_id: 案例ID(用于日志)
Returns:
包含响应内容、token使用量等信息的字典
"""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.api_key}",
"Expect": "",
"Connection": "keep-alive",
"Accept": "text/event-stream"
}
payload = {
"model": model,
"messages": messages,
"max_tokens": self.max_tokens,
"stream": True # 启用流式响应
}
if self.enable_thinking:
payload["enable_thinking"] = True
endpoint = f"{self.api_url}/chat/completions"
# 续写请求也支持重试
max_retries = 2
for attempt in range(max_retries + 1):
try:
self.log(f" 🔄 [{case_id}] 发送续写请求 (第{attempt + 1}次)...")
start_time = time.time()
response = self.session.post(
endpoint,
json=payload,
headers=headers,
timeout=(30, self.REQUEST_TIMEOUT),
stream=True
)
response.raise_for_status()
# 显式设置编码为UTF-8 (修复Windows乱码问题)
response.encoding = 'utf-8'
# 收集SSE流式响应数据
collected_content = ""
token_usage = TokenUsage()
finish_reason = None
for line in response.iter_lines(decode_unicode=True):
if not line:
continue
if line.startswith("data: "):
data_str = line[6:]
if data_str.strip() == "[DONE]":
break
try:
chunk = json.loads(data_str)
if "choices" in chunk and len(chunk["choices"]) > 0:
delta = chunk["choices"][0].get("delta", {})
if "content" in delta and delta["content"]:
collected_content += delta["content"]
if "reasoning_content" in delta and delta["reasoning_content"]:
collected_content += delta["reasoning_content"]
if chunk["choices"][0].get("finish_reason"):
finish_reason = chunk["choices"][0]["finish_reason"]
if "usage" in chunk and chunk["usage"]:
usage = chunk["usage"]
token_usage.prompt_tokens = usage.get("prompt_tokens", 0)
token_usage.completion_tokens = usage.get("completion_tokens", 0)
token_usage.total_tokens = usage.get("total_tokens", 0)
except json.JSONDecodeError:
continue
duration = time.time() - start_time
self.log(f" 🔄 [{case_id}] 续写完成,耗时 {duration:.1f}秒,输出 {token_usage.completion_tokens} tokens")
return {
"content": collected_content,
"token_usage": token_usage,
"duration_seconds": round(duration, 2),
"finish_reason": finish_reason,
"success": True
}
except (requests.exceptions.ChunkedEncodingError, requests.exceptions.ConnectionError) as e:
error_str = str(e)
if attempt < max_retries:
delay = (attempt + 1) * 5 # 5, 10秒
self.log(f" 📡 [{case_id}] 续写传输中断,{delay}秒后重试...")
time.sleep(delay)
else:
self.log(f" ❌ [{case_id}] 续写请求失败: {error_str[:100]}")
return {
"content": "",
"token_usage": TokenUsage(),
"duration_seconds": 0,
"finish_reason": "error",
"success": False
}
except Exception as e:
self.log(f" ❌ [{case_id}] 续写请求失败: {str(e)[:100]}")
return {
"content": "",
"token_usage": TokenUsage(),
"duration_seconds": 0,
"finish_reason": "error",
"success": False
}
return {
"content": "",
"token_usage": TokenUsage(),
"duration_seconds": 0,
"finish_reason": "error",
"success": False
}
def run_text_tests(self):
"""执行代码生成测试"""
cases = self.load_test_cases("text")
if not cases:
self.log("未找到代码生成测试用例")
return []
self.log(f"开始代码生成测试,共 {len(cases)} 个案例,使用模型: {self.text_model}")
self.text_stats = TestStats()
self.text_stats.total_cases = len(cases)
test_start_time = time.time()
results = []
# 记录失败的案例,用于最后统计
failed_cases = []
with ThreadPoolExecutor(max_workers=self.max_threads) as executor:
futures = {}
for case in cases:
if not self.is_running:
break
future = executor.submit(self.run_single_text_test, case)
futures[future] = case
for i, future in enumerate(as_completed(futures)):
if not self.is_running:
break
case = futures[future]
try:
result = future.result()
results.append(result)
self.text_stats.success_count += 1
# 统计tokens
if "token_usage" in result:
self.text_stats.total_tokens.add(result["token_usage"])
# 累加单case实际耗时(用于计算真实平均值)
case_duration = result.get("duration_seconds", 0)
self.text_stats.sum_case_time_seconds += case_duration
# 统计HTML提取情况
if result.get("html_file"):
self.text_stats.html_extracted_count += 1
elif result.get("txt_file"):
self.text_stats.no_html_count += 1
# 统计重试次数
self.text_stats.retry_count += result.get("retry_count", 0)
# 统计不完整响应
if result.get("is_incomplete"):
self.text_stats.incomplete_count += 1
self.log(f"✅ [代码生成] {case['id']} {case['name']} - 成功 (耗时{case_duration}秒, {result.get('tokens_per_second', 0):.1f} tok/s)")
except Exception as e:
error_msg = str(e)
self.text_stats.failed_count += 1
# 检测是否为超时错误
if "超时" in error_msg or "timeout" in error_msg.lower():
self.text_stats.timeout_count += 1
self.log(f"❌ [代码生成] {case['id']} {case['name']} - 失败: {error_msg}")
failed_result = {
"id": case["id"],
"name": case["name"],
"category": case.get("category", "未分类"),
"difficulty": case.get("difficulty", "中"),
"tags": case.get("tags", []),
"icon": case.get("icon", "📄"),
"prompt": case["prompt"],
"success": False,
"error": error_msg,
"timestamp": datetime.now().isoformat()
}
results.append(failed_result)
failed_cases.append(case)
progress = (i + 1) / len(cases) * 50
self.update_progress(progress)
# 计算统计信息
self.text_stats.total_time_seconds = time.time() - test_start_time
if self.text_stats.success_count > 0:
# 使用单case实际耗时总和计算平均值(正确反映单case能力)
self.text_stats.avg_time_per_case = self.text_stats.sum_case_time_seconds / self.text_stats.success_count
# 计算平均输出tokens
self.text_stats.avg_output_tokens_per_case = self.text_stats.total_tokens.completion_tokens / self.text_stats.success_count
# 计算平均输出速率
if self.text_stats.sum_case_time_seconds > 0:
self.text_stats.avg_tokens_per_second = self.text_stats.total_tokens.completion_tokens / self.text_stats.sum_case_time_seconds
# 输出统计摘要
self.log(f"📊 代码生成测试完成:")
self.log(f" 成功: {self.text_stats.success_count}/{self.text_stats.total_cases}")
self.log(f" HTML提取: {self.text_stats.html_extracted_count}, 未提取: {self.text_stats.no_html_count}")
self.log(f" 总Tokens: {self.text_stats.total_tokens.total_tokens} (输入: {self.text_stats.total_tokens.prompt_tokens}, 输出: {self.text_stats.total_tokens.completion_tokens})")
self.log(f" 多线程总耗时: {self.text_stats.total_time_seconds:.1f}秒")
self.log(f" 单case平均耗时: {self.text_stats.avg_time_per_case:.1f}秒 (基于{self.text_stats.success_count}个成功案例)")
self.log(f" 单case平均输出: {self.text_stats.avg_output_tokens_per_case:.0f} tokens")
self.log(f" 平均输出速率: {self.text_stats.avg_tokens_per_second:.1f} tokens/s")
if self.text_stats.timeout_count > 0:
self.log(f" ⏰ 超时次数: {self.text_stats.timeout_count}")
if self.text_stats.retry_count > 0:
self.log(f" 🔄 重试次数: {self.text_stats.retry_count}")
if self.text_stats.incomplete_count > 0:
self.log(f" ⚠️ 不完整响应: {self.text_stats.incomplete_count}")
# 保存统计信息
stats_file = self.output_dir / "text" / "_stats.json"
with open(stats_file, "w", encoding="utf-8") as f:
json.dump(self.text_stats.to_dict(), f, ensure_ascii=False, indent=2)
self.results["text"] = results
return results
def run_single_text_test(self, case) -> Dict[str, Any]:
"""执行单个代码生成测试(带重试)"""
api_result = self.call_api_with_retry(
case["prompt"],
self.text_model,
is_image=False,
case_id=case["id"]
)
response_json = api_result["response"]
token_usage = api_result["token_usage"]
duration_seconds = api_result["duration_seconds"]
retry_count = api_result["retry_count"]
tokens_per_second = api_result.get("tokens_per_second", 0)
is_incomplete = api_result.get("is_incomplete", False)
finish_reason = api_result.get("finish_reason", "")
# 安全提取内容 - 支持多种响应格式
content = ""
reasoning_content = ""
raw_response = ""
try:
message = response_json.get("choices", [{}])[0].get("message", {})
# 尝试获取常规content
content = message.get("content") or ""
# 尝试获取reasoning_content (deepseek-reasoner等推理模型)
reasoning_content = message.get("reasoning_content") or ""
# 如果content为空但reasoning_content有内容,使用reasoning_content
if not content and reasoning_content:
content = reasoning_content
self.log(f" 📝 [{case['id']}] 使用reasoning_content作为响应内容")
# 如果两者都为空,保存完整响应用于调试
if not content and not reasoning_content:
raw_response = json.dumps(response_json, ensure_ascii=False, indent=2)
self.log(f" ⚠️ [{case['id']}] content和reasoning_content均为空,保存原始响应")
content = raw_response
except (KeyError, IndexError, TypeError) as e:
self.log(f" ⚠️ [{case['id']}] 响应格式异常: {str(e)}")
# 保存原始响应用于调试
raw_response = json.dumps(response_json, ensure_ascii=False, indent=2)