-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathtest_header_parser_perf.js
More file actions
162 lines (143 loc) · 4.71 KB
/
test_header_parser_perf.js
File metadata and controls
162 lines (143 loc) · 4.71 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
import FileHeaderManager from '../../utils/fileHeaderManager.js';
// 旧的算法实现 (为了对比)
function findHeaderBlockOld(text, ext) {
const startMarker = '@header(';
const startIndex = text.indexOf(startMarker);
if (startIndex === -1) return null;
let index = startIndex + startMarker.length;
let balance = 1;
let inString = false;
let stringChar = '';
let escape = false;
let inLineComment = false;
let inBlockComment = false;
for (; index < text.length; index++) {
const char = text[index];
if (inLineComment) {
if (char === '\n') inLineComment = false;
continue;
}
if (inBlockComment) {
if (char === '*' && text[index + 1] === '/') {
inBlockComment = false;
index++;
}
continue;
}
if (inString) {
if (escape) {
escape = false;
} else if (char === '\\') {
escape = true;
} else if (char === stringChar) {
inString = false;
}
continue;
}
// Start of comment
if (char === '/' && text[index + 1] === '/') {
inLineComment = true;
index++;
} else if (char === '/' && text[index + 1] === '*') {
inBlockComment = true;
index++;
} else if (ext === '.py' && char === '#') {
inLineComment = true;
} else if (char === '"' || char === "'") {
inString = true;
stringChar = char;
} else if (char === '(') {
balance++;
} else if (char === ')') {
balance--;
if (balance === 0) {
return {
start: startIndex,
end: index + 1,
content: text.substring(startIndex + startMarker.length, index)
};
}
}
}
return null;
}
// 构造测试数据
const simpleHeader = `
/*
@header({
title: "Simple",
version: 1
})
*/
`;
const complexHeader = `
/*
@header({
title: "Complex Header",
description: "Contains strings with parens ) and comments // inside strings",
nested: {
obj: { a: 1, b: 2 },
arr: [1, 2, 3, "string with )"]
},
regex: "cctv://(.+)",
// This is a comment inside the block
/* Another block comment */
code: "function() { return 'ok'; }"
})
*/
`;
const largeHeader = `
/*
@header({
title: "Large Header",
data: "${'x'.repeat(5000)}",
items: [
${Array(100).fill('{ name: "item", value: "test string with ) inside" }').join(',\n')}
]
})
*/
`;
async function runBenchmark() {
console.log('=== findHeaderBlock 算法性能基准测试 ===');
const iterations = 100000; // 增加循环次数以放大差异
const testCases = [
{ name: 'Simple Header', data: simpleHeader },
{ name: 'Complex Header', data: complexHeader },
{ name: 'Large Header (~10KB)', data: largeHeader }
];
for (const testCase of testCases) {
console.log(`\n测试场景: ${testCase.name} (循环 ${iterations} 次)`);
// 1. 测试旧算法
const startOld = performance.now();
for (let i = 0; i < iterations; i++) {
findHeaderBlockOld(testCase.data, '.js');
}
const endOld = performance.now();
const timeOld = endOld - startOld;
console.log(`[旧算法] 总耗时: ${timeOld.toFixed(4)} ms | 平均: ${(timeOld/iterations).toFixed(5)} ms`);
// 2. 测试新算法
const startNew = performance.now();
for (let i = 0; i < iterations; i++) {
FileHeaderManager.findHeaderBlock(testCase.data, '.js');
}
const endNew = performance.now();
const timeNew = endNew - startNew;
console.log(`[新算法] 总耗时: ${timeNew.toFixed(4)} ms | 平均: ${(timeNew/iterations).toFixed(5)} ms`);
// 3. 计算提升
const improvement = ((timeOld - timeNew) / timeOld * 100).toFixed(2);
console.log(`🚀 性能提升: ${improvement}%`);
// 验证正确性
const resOld = findHeaderBlockOld(testCase.data, '.js');
const resNew = FileHeaderManager.findHeaderBlock(testCase.data, '.js');
if (resOld?.content !== resNew?.content) {
console.error('❌ 结果不一致!');
console.log('Old Content Length:', resOld?.content?.length);
console.log('New Content Length:', resNew?.content?.length);
// console.log('Old:', resOld?.content);
// console.log('New:', resNew?.content);
} else {
console.log('✅ 结果一致');
}
}
}
runBenchmark();