|
| 1 | + |
| 2 | +import FileHeaderManager from '../../utils/fileHeaderManager.js'; |
| 3 | + |
| 4 | +// 旧的算法实现 (为了对比) |
| 5 | +function findHeaderBlockOld(text, ext) { |
| 6 | + const startMarker = '@header('; |
| 7 | + const startIndex = text.indexOf(startMarker); |
| 8 | + if (startIndex === -1) return null; |
| 9 | + |
| 10 | + let index = startIndex + startMarker.length; |
| 11 | + let balance = 1; |
| 12 | + let inString = false; |
| 13 | + let stringChar = ''; |
| 14 | + let escape = false; |
| 15 | + let inLineComment = false; |
| 16 | + let inBlockComment = false; |
| 17 | + |
| 18 | + for (; index < text.length; index++) { |
| 19 | + const char = text[index]; |
| 20 | + |
| 21 | + if (inLineComment) { |
| 22 | + if (char === '\n') inLineComment = false; |
| 23 | + continue; |
| 24 | + } |
| 25 | + |
| 26 | + if (inBlockComment) { |
| 27 | + if (char === '*' && text[index + 1] === '/') { |
| 28 | + inBlockComment = false; |
| 29 | + index++; |
| 30 | + } |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + if (inString) { |
| 35 | + if (escape) { |
| 36 | + escape = false; |
| 37 | + } else if (char === '\\') { |
| 38 | + escape = true; |
| 39 | + } else if (char === stringChar) { |
| 40 | + inString = false; |
| 41 | + } |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + // Start of comment |
| 46 | + if (char === '/' && text[index + 1] === '/') { |
| 47 | + inLineComment = true; |
| 48 | + index++; |
| 49 | + } else if (char === '/' && text[index + 1] === '*') { |
| 50 | + inBlockComment = true; |
| 51 | + index++; |
| 52 | + } else if (ext === '.py' && char === '#') { |
| 53 | + inLineComment = true; |
| 54 | + } else if (char === '"' || char === "'") { |
| 55 | + inString = true; |
| 56 | + stringChar = char; |
| 57 | + } else if (char === '(') { |
| 58 | + balance++; |
| 59 | + } else if (char === ')') { |
| 60 | + balance--; |
| 61 | + if (balance === 0) { |
| 62 | + return { |
| 63 | + start: startIndex, |
| 64 | + end: index + 1, |
| 65 | + content: text.substring(startIndex + startMarker.length, index) |
| 66 | + }; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + return null; |
| 71 | +} |
| 72 | + |
| 73 | +// 构造测试数据 |
| 74 | +const simpleHeader = ` |
| 75 | +/* |
| 76 | +@header({ |
| 77 | + title: "Simple", |
| 78 | + version: 1 |
| 79 | +}) |
| 80 | +*/ |
| 81 | +`; |
| 82 | + |
| 83 | +const complexHeader = ` |
| 84 | +/* |
| 85 | +@header({ |
| 86 | + title: "Complex Header", |
| 87 | + description: "Contains strings with parens ) and comments // inside strings", |
| 88 | + nested: { |
| 89 | + obj: { a: 1, b: 2 }, |
| 90 | + arr: [1, 2, 3, "string with )"] |
| 91 | + }, |
| 92 | + regex: "cctv://(.+)", |
| 93 | + // This is a comment inside the block |
| 94 | + /* Another block comment */ |
| 95 | + code: "function() { return 'ok'; }" |
| 96 | +}) |
| 97 | +*/ |
| 98 | +`; |
| 99 | + |
| 100 | +const largeHeader = ` |
| 101 | +/* |
| 102 | +@header({ |
| 103 | + title: "Large Header", |
| 104 | + data: "${'x'.repeat(5000)}", |
| 105 | + items: [ |
| 106 | + ${Array(100).fill('{ name: "item", value: "test string with ) inside" }').join(',\n')} |
| 107 | + ] |
| 108 | +}) |
| 109 | +*/ |
| 110 | +`; |
| 111 | + |
| 112 | +async function runBenchmark() { |
| 113 | + console.log('=== findHeaderBlock 算法性能基准测试 ==='); |
| 114 | + const iterations = 100000; // 增加循环次数以放大差异 |
| 115 | + |
| 116 | + const testCases = [ |
| 117 | + { name: 'Simple Header', data: simpleHeader }, |
| 118 | + { name: 'Complex Header', data: complexHeader }, |
| 119 | + { name: 'Large Header (~10KB)', data: largeHeader } |
| 120 | + ]; |
| 121 | + |
| 122 | + for (const testCase of testCases) { |
| 123 | + console.log(`\n测试场景: ${testCase.name} (循环 ${iterations} 次)`); |
| 124 | + |
| 125 | + // 1. 测试旧算法 |
| 126 | + const startOld = performance.now(); |
| 127 | + for (let i = 0; i < iterations; i++) { |
| 128 | + findHeaderBlockOld(testCase.data, '.js'); |
| 129 | + } |
| 130 | + const endOld = performance.now(); |
| 131 | + const timeOld = endOld - startOld; |
| 132 | + console.log(`[旧算法] 总耗时: ${timeOld.toFixed(4)} ms | 平均: ${(timeOld/iterations).toFixed(5)} ms`); |
| 133 | + |
| 134 | + // 2. 测试新算法 |
| 135 | + const startNew = performance.now(); |
| 136 | + for (let i = 0; i < iterations; i++) { |
| 137 | + FileHeaderManager.findHeaderBlock(testCase.data, '.js'); |
| 138 | + } |
| 139 | + const endNew = performance.now(); |
| 140 | + const timeNew = endNew - startNew; |
| 141 | + console.log(`[新算法] 总耗时: ${timeNew.toFixed(4)} ms | 平均: ${(timeNew/iterations).toFixed(5)} ms`); |
| 142 | + |
| 143 | + // 3. 计算提升 |
| 144 | + const improvement = ((timeOld - timeNew) / timeOld * 100).toFixed(2); |
| 145 | + console.log(`🚀 性能提升: ${improvement}%`); |
| 146 | + |
| 147 | + // 验证正确性 |
| 148 | + const resOld = findHeaderBlockOld(testCase.data, '.js'); |
| 149 | + const resNew = FileHeaderManager.findHeaderBlock(testCase.data, '.js'); |
| 150 | + if (resOld?.content !== resNew?.content) { |
| 151 | + console.error('❌ 结果不一致!'); |
| 152 | + console.log('Old Content Length:', resOld?.content?.length); |
| 153 | + console.log('New Content Length:', resNew?.content?.length); |
| 154 | + // console.log('Old:', resOld?.content); |
| 155 | + // console.log('New:', resNew?.content); |
| 156 | + } else { |
| 157 | + console.log('✅ 结果一致'); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +runBenchmark(); |
0 commit comments