Skip to content

Commit 0e636c2

Browse files
committed
refactor: 大幅简化基准测试输出
改进内容: 1. 删除冗余的详细对比表格 2. 删除重复的内存使用表格 3. 删除冗长的性能总结文字 4. 保留一个清晰的综合对比表格 5. 简化适用场景描述 现在输出更加简洁清晰: - 只显示一个综合表格(内存+耗时+倍率) - 简短的选择建议 - 去掉所有重复信息
1 parent 2a3c1fe commit 0e636c2

File tree

1 file changed

+26
-166
lines changed

1 file changed

+26
-166
lines changed

benchmark/run.php

Lines changed: 26 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -86,101 +86,12 @@
8686
CachedConverter::clearCache();
8787
SmartConverter::clearCache();
8888

89-
// 生成策略对比表格
90-
$comparisonHtml = [];
91-
92-
// 标题行
93-
$comparisonHtml[] = '<tr>';
94-
$comparisonHtml[] = '<th>Method</th>';
95-
foreach ($strategies as $strategy) {
96-
$comparisonHtml[] = '<th class="text-center">' . $strategy['short_name'] . '</th>';
97-
}
98-
$comparisonHtml[] = '<th class="text-center">Fastest</th>';
99-
$comparisonHtml[] = '<th class="text-center">Speedup</th>';
100-
$comparisonHtml[] = '</tr>';
101-
102-
// 数据行
103-
foreach ($methods as $method) {
104-
$comparisonHtml[] = '<tr>';
105-
$comparisonHtml[] = '<td class="text-teal-500">' . $method . '</td>';
106-
107-
$times = [];
108-
foreach ($strategies as $strategyKey => $strategy) {
109-
$time = $results[$strategyKey][$method]['time'];
110-
$times[$strategyKey] = $time;
111-
$comparisonHtml[] = '<td class="text-center">' . sprintf('%.2f ms', $time) . '</td>';
112-
}
113-
114-
// 找出最快的策略
115-
$minTime = min($times);
116-
$maxTime = max($times);
117-
$bestStrategy = array_search($minTime, $times);
118-
119-
$comparisonHtml[] = '<td class="text-center ' . $strategies[$bestStrategy]['color'] . '">' .
120-
$strategies[$bestStrategy]['short_name'] . '</td>';
121-
122-
// 计算加速比
123-
$speedup = $maxTime > 0 ? sprintf('%.1fx', $maxTime / $minTime) : '-';
124-
$comparisonHtml[] = '<td class="text-center">' . $speedup . '</td>';
125-
$comparisonHtml[] = '</tr>';
126-
}
127-
128-
// 分隔线
129-
$comparisonHtml[] = '<tr><td colspan="6" class="text-gray-500">────────────────────────────────────────────────</td></tr>';
130-
131-
// 总计行标题(添加列说明)
132-
$comparisonHtml[] = '<tr class="text-gray-400">';
133-
$comparisonHtml[] = '<td></td>';
134-
$comparisonHtml[] = '<td class="text-center text-blue-400">Memory</td>';
135-
$comparisonHtml[] = '<td class="text-center text-green-400">Cached</td>';
136-
$comparisonHtml[] = '<td class="text-center text-yellow-400">Smart</td>';
137-
$comparisonHtml[] = '<td></td>';
138-
$comparisonHtml[] = '<td></td>';
139-
$comparisonHtml[] = '</tr>';
140-
141-
// 总计行
142-
$comparisonHtml[] = '<tr>';
143-
$comparisonHtml[] = '<td class="font-bold text-white">TOTAL</td>';
144-
89+
// 收集总时间数据(供后面使用)
14590
$totalTimes = [];
14691
foreach ($strategies as $strategyKey => $strategy) {
147-
$totalTime = $results[$strategyKey]['total'];
148-
$totalTimes[$strategyKey] = $totalTime;
149-
$isFastest = false;
150-
151-
// 预先检查是否是最快的
152-
$minTotal = min(
153-
array_values($results)['memory']['total'] ?? PHP_FLOAT_MAX,
154-
array_values($results)['cached']['total'] ?? PHP_FLOAT_MAX,
155-
array_values($results)['smart']['total'] ?? PHP_FLOAT_MAX
156-
);
157-
158-
if ($totalTime == $minTotal) {
159-
$comparisonHtml[] = '<td class="text-center font-bold ' . $strategy['color'] . '">' . sprintf('%.2f ms', $totalTime) . '</td>';
160-
} else {
161-
$comparisonHtml[] = '<td class="text-center">' . sprintf('%.2f ms', $totalTime) . '</td>';
162-
}
92+
$totalTimes[$strategyKey] = $results[$strategyKey]['total'];
16393
}
16494

165-
$minTotal = min($totalTimes);
166-
$maxTotal = max($totalTimes);
167-
$bestTotal = array_search($minTotal, $totalTimes);
168-
169-
$comparisonHtml[] = '<td class="text-center font-bold ' . $strategies[$bestTotal]['color'] . '">' .
170-
$strategies[$bestTotal]['short_name'] . '</td>';
171-
172-
// 总体加速比
173-
$totalSpeedup = $maxTotal > 0 ? sprintf('%.1fx', $maxTotal / $minTotal) : '-';
174-
$comparisonHtml[] = '<td class="text-center font-bold">' . $totalSpeedup . '</td>';
175-
$comparisonHtml[] = '</tr>';
176-
177-
// 添加说明行
178-
$comparisonHtml[] = '<tr class="text-gray-500">';
179-
$comparisonHtml[] = '<td colspan="6" class="text-center">↑ 三列数字分别是:内存优化策略、缓存策略、智能策略的总耗时</td>';
180-
$comparisonHtml[] = '</tr>';
181-
182-
$comparisonTable = implode("\n", $comparisonHtml);
183-
18495
// 计算内存使用情况
18596
$memoryInfo = [];
18697
foreach (['memory', 'cached', 'smart'] as $strategyKey) {
@@ -190,22 +101,7 @@
190101
$memoryInfo[$strategyKey] = $info;
191102
}
192103

193-
// 生成内存使用对比
194-
$memoryHtml = [];
195-
foreach ($memoryInfo as $strategy => $info) {
196-
$memoryHtml[] = sprintf(
197-
'<tr>
198-
<td class="%s">%s</td>
199-
<td>%s</td>
200-
<td class="text-gray-500">%s</td>
201-
</tr>',
202-
$strategies[$strategy]['color'],
203-
$strategies[$strategy]['name'],
204-
$info['peak_memory'],
205-
$info['description']
206-
);
207-
}
208-
$memoryTable = implode("\n", $memoryHtml);
104+
// 不再需要单独的内存表格
209105

210106
// 创建综合对比表格
211107
$summaryHtml = [];
@@ -247,6 +143,20 @@ function parseMemory($memStr) {
247143
$performanceIcon = ''; // 比基准快
248144
}
249145

146+
// 简化的适用场景描述
147+
$scenario = '';
148+
switch($strategyKey) {
149+
case 'memory':
150+
$scenario = 'Web请求、内存受限';
151+
break;
152+
case 'cached':
153+
$scenario = '批量处理、重复转换';
154+
break;
155+
case 'smart':
156+
$scenario = '通用场景、自适应';
157+
break;
158+
}
159+
250160
$rowClass = $isFastest ? 'font-bold' : '';
251161
$memoryClass = $isLeastMemory ? 'text-green-500' : ($memoryVal == $maxMemory ? 'text-red-500' : '');
252162
$timeClass = $isFastest ? 'text-green-500' : ($time == $maxTime ? 'text-red-500' : '');
@@ -269,33 +179,12 @@ function parseMemory($memStr) {
269179
$time,
270180
$speedup >= 1.2 ? 'text-green-500' : ($speedup <= 0.8 ? 'text-red-500' : ''),
271181
$speedup,
272-
$memoryInfo[$strategyKey]['description']
182+
$scenario
273183
);
274184
}
275185

276186
$summaryTable = implode("\n", $summaryHtml);
277187

278-
// 性能提升总结
279-
$speedupSummary = '';
280-
if (isset($totalTimes['cached']) && isset($totalTimes['memory'])) {
281-
$cacheSpeedup = round($totalTimes['memory'] / $totalTimes['cached'], 2);
282-
$speedupSummary = sprintf(
283-
'<div class="mt-2">📊 Performance Summary:</div>
284-
<div>• <span class="text-green-500">Cached strategy</span> is <span class="font-bold">%.2fx faster</span> than Memory Optimized</div>',
285-
$cacheSpeedup
286-
);
287-
288-
if (isset($totalTimes['smart'])) {
289-
$smartVsMemory = round($totalTimes['memory'] / $totalTimes['smart'], 2);
290-
$smartVsCached = round($totalTimes['smart'] / $totalTimes['cached'], 2);
291-
$speedupSummary .= sprintf(
292-
'<div>• <span class="text-yellow-500">Smart strategy</span> is <span class="font-bold">%.2fx faster</span> than Memory, <span class="font-bold">%.2fx slower</span> than Cached</div>',
293-
$smartVsMemory,
294-
$smartVsCached
295-
);
296-
}
297-
}
298-
299188
$totalUsage = round(microtime(true) - $totalStart, 5) * 1000;
300189

301190
render(<<<"HTML"
@@ -322,63 +211,34 @@ function parseMemory($memStr) {
322211
Default strategy usage: <span class="text-green-500">{$defaultTotalUsage}</span>ms
323212
</div>
324213
325-
<div class="my-1 text-yellow-500">Strategy Comparison:</div>
326-
<table>
327-
<thead>
328-
{$comparisonTable}
329-
</thead>
330-
</table>
331-
332-
<div class="mt-3 mb-1 text-yellow-500">📊 综合性能对比(内存 + 耗时 + 倍率):</div>
214+
<div class="mt-4 mb-1 text-yellow-500">📊 策略性能对比:</div>
333215
<table>
334216
<thead>
335217
<tr>
336218
<th>策略</th>
337219
<th class="text-center">内存占用</th>
338220
<th class="text-center">总耗时</th>
339221
<th class="text-center">速度倍率</th>
340-
<th>特点说明</th>
222+
<th>适用场景</th>
341223
</tr>
342224
</thead>
343225
{$summaryTable}
344226
</table>
345227
346228
<div class="mt-2 text-gray-500">
347-
<div>速度倍率说明:以 Memory Optimized 为基准 (1.0x)</div>
348-
<div class="mt-1">图标说明:⚡速度最快 | 💚内存最少 | 🏆综合最优 | ✨比基准快</div>
229+
<div>* 速度倍率以 Memory Optimized 为基准 (1.0x)</div>
349230
</div>
350231
351232
<div class="mt-2 px-2 py-1 bg-blue-800 text-white">
352-
<div class="font-bold">🎯 快速选择建议:</div>
353-
<div>• 内存受限环境(如Web请求)→ 选择 <span class="text-blue-400">Memory Optimized</span></div>
354-
<div>• 批量处理大量文本选择 <span class="text-green-400">Cached</span></div>
355-
<div>• 平衡性能和内存选择 <span class="text-yellow-400">Smart</span></div>
233+
<div class="font-bold">🎯 如何选择:</div>
234+
<div>• Web请求 → <span class="text-blue-400">Memory Optimized</span> (省内存)</div>
235+
<div>• 批量处理 → <span class="text-green-400">Cached</span> (最快)</div>
236+
<div>• 通用场景 → <span class="text-yellow-400">Smart</span> (平衡)</div>
356237
</div>
357238
358-
{$speedupSummary}
359-
360-
<div class="mt-3 mb-1 text-yellow-500">Memory Usage Details:</div>
361-
<table>
362-
<thead>
363-
<tr>
364-
<th>Strategy</th>
365-
<th>Peak Memory</th>
366-
<th>Description</th>
367-
</tr>
368-
</thead>
369-
{$memoryTable}
370-
</table>
371-
372-
<div class="mt-1">
239+
<div class="mt-3">
373240
<div>Total benchmark time: <span class="text-green-500">{$totalUsage}</span>ms</div>
374241
</div>
375-
376-
<div class="mt-3 text-gray-500">
377-
<div>💡 Tips:</div>
378-
<div>• <span class="text-blue-500">Memory Optimized</span>: Best for web requests with limited memory</div>
379-
<div>• <span class="text-green-500">Cached</span>: Best for batch processing and repeated conversions</div>
380-
<div>• <span class="text-yellow-500">Smart</span>: Balanced approach with adaptive optimization</div>
381-
</div>
382242
</div>
383243
HTML);
384244

0 commit comments

Comments
 (0)