1- /* global console, setTimeout */
1+ /* global console */
22
3- import { runBenchmark } from './benchmark.js ' ;
3+ import { Worker } from 'worker_threads ' ;
44import { testModels , testCases } from './testsuit.js' ;
55import { analyzeReports } from './analyze.js' ;
66import path from 'path' ;
77import fs from 'fs' ;
88import { fileURLToPath } from 'url' ;
99
10+ // 获取当前文件的目录路径
11+ const __filename = fileURLToPath ( import . meta. url ) ;
12+ const __dirname = path . dirname ( __filename ) ;
13+
1014// 获取 report 目录的路径
11- const reportDir = path . join ( path . dirname ( fileURLToPath ( import . meta . url ) ) , 'report' ) ;
15+ const reportDir = path . join ( __dirname , 'report' ) ;
1216
1317// 确保 report 目录存在
1418if ( ! fs . existsSync ( reportDir ) ) {
1519 fs . mkdirSync ( reportDir , { recursive : true } ) ;
1620}
1721
18- async function runBenchmarkWithInterval ( iterations = 10 , intervalMs = 1000 ) {
19- for ( let i = 0 ; i < iterations ; i ++ ) {
20- for ( const model of testModels ) {
21- for ( const testCase of testCases ) {
22- console . log ( `执行基准测试 [ ${ i + 1 } / ${ iterations } ]: ${ model . name } / ${ testCase . name } ` ) ;
22+ // 创建worker实例
23+ function createWorker ( model , iteration , intervalMs ) {
24+ return new Promise ( ( resolve , reject ) => {
25+ const workerPath = path . join ( __dirname , 'worker.js' ) ;
26+ const worker = new Worker ( workerPath ) ;
2327
24- try {
25- const result = await runBenchmark ( model , testCase ) ;
26- reportOutput ( result ) ;
28+ worker . on ( 'message' , ( message ) => {
29+ switch ( message . type ) {
30+ case 'progress' :
31+ console . log ( message . message ) ;
32+ break ;
33+ case 'result' :
34+ reportOutput ( message . result ) ;
2735 console . log (
28- `完成! 耗时: ${ result . timeCost . toFixed ( 2 ) } s, 成本: $${ (
29- result . inputTokensCost + result . outputTokensCost
30- ) . toFixed ( 5 ) } `,
36+ `完成! ${ message . model } / ${ message . testCase } (耗时: ${ message . timeCost . toFixed ( 2 ) } s, 成本: $${ message . cost . toFixed ( 5 ) } )`
3137 ) ;
38+ break ;
39+ case 'complete' :
40+ console . log ( `模型 ${ message . model } 的所有测试完成` ) ;
41+ resolve ( ) ;
42+ worker . terminate ( ) ;
43+ break ;
44+ case 'error' :
45+ console . error ( `基准测试失败 (${ message . model } ): ${ message . error } ` ) ;
46+ resolve ( ) ;
47+ worker . terminate ( ) ;
48+ break ;
49+ }
50+ } ) ;
51+
52+ worker . on ( 'error' , reject ) ;
53+ worker . on ( 'exit' , ( code ) => {
54+ if ( code !== 0 ) {
55+ reject ( new Error ( `Worker stopped with exit code ${ code } ` ) ) ;
56+ }
57+ } ) ;
58+
59+ // 将测试用例数据也传递给worker
60+ worker . postMessage ( {
61+ model,
62+ testCases,
63+ iteration,
64+ intervalMs
65+ } ) ;
66+ } ) ;
67+ }
68+
69+ async function runBenchmarkWithInterval ( iterations = 10 , intervalMs = 1000 ) {
70+ const startTime = new Date ( ) ;
71+ console . log ( `开始基准测试: ${ startTime . toLocaleString ( ) } ` ) ;
72+ console . log ( `加载了${ testModels . length } 个模型` ) ;
73+
74+ // 设置并发执行的worker数量
75+ const maxWorkers = 3 ;
76+
77+ for ( let i = 0 ; i < iterations ; i ++ ) {
78+ // 将模型列表分成多个批次
79+ for ( let j = 0 ; j < testModels . length ; j += maxWorkers ) {
80+ const modelBatch = testModels . slice ( j , Math . min ( j + maxWorkers , testModels . length ) ) ;
81+ console . log ( `\n执行第 ${ i + 1 } /${ iterations } 轮,批次 ${ Math . floor ( j / maxWorkers ) + 1 } /${ Math . ceil ( testModels . length / maxWorkers ) } ` ) ;
82+
83+ // 创建并发worker
84+ const workerPromises = modelBatch . map ( model =>
85+ createWorker ( model , i + 1 , intervalMs )
86+ ) ;
87+
88+ // 等待当前批次完成
89+ await Promise . all ( workerPromises ) ;
3290
33- // 添加间隔,避免API限流
34- if (
35- i < iterations - 1 ||
36- model !== testModels [ testModels . length - 1 ] ||
37- testCase !== testCases [ testCases . length - 1 ]
38- ) {
39- await new Promise ( ( resolve ) => setTimeout ( resolve , intervalMs ) ) ;
40- }
41- } catch ( error ) {
42- console . error ( `基准测试失败: ${ error . message } ` ) ;
43- }
91+ // 批次之间添加间隔,避免API限流
92+ if ( j + maxWorkers < testModels . length || i < iterations - 1 ) {
93+ await new Promise ( ( resolve ) => setTimeout ( resolve , intervalMs ) ) ;
4494 }
4595 }
4696 }
4797
98+ const endTime = new Date ( ) ;
99+ const totalTimeInSeconds = ( endTime - startTime ) / 1000 ;
100+ console . log ( `\n基准测试结束: ${ endTime . toLocaleString ( ) } ` ) ;
101+ console . log ( `总运行时间: ${ totalTimeInSeconds . toFixed ( 2 ) } 秒` ) ;
48102 console . log ( '所有基准测试完成!' ) ;
49103
50104 // 分析报告
@@ -57,14 +111,13 @@ async function runBenchmarkWithInterval(iterations = 10, intervalMs = 1000) {
57111
58112function reportOutput ( result ) {
59113 // 生成文件名: 模型名_测试用例名_目标语言_日期.json
60- const fileName = `${ result . modelName } _${ result . testCaseName } _${ result . targetLanguage || 'unknown'
61- } _${ new Date ( ) . toISOString ( ) . replace ( / [: .] / g, '-' ) } .json`;
114+ const fileName = `${ result . modelName } _${ result . testCaseName } _${ result . targetLanguage || 'unknown' } _${ new Date ( ) . toISOString ( ) . replace ( / [: .] / g, '-' ) } .json` ;
62115 const filePath = path . join ( reportDir , fileName ) ;
63116
64117 // 将结果保存为JSON文件
65118 fs . writeFileSync ( filePath , JSON . stringify ( result , null , 2 ) ) ;
66119}
67120
68121// 运行基准测试
69- // 每个模型运行1次 ,每次间隔1秒,确保总消耗token不超过0.2美元
70- runBenchmarkWithInterval ( 1 , 1000 ) ;
122+ // 每个模型运行2次 ,每次间隔1秒,确保总消耗token不超过0.2美元
123+ runBenchmarkWithInterval ( 2 , 1000 ) ;
0 commit comments