Skip to content

Commit a3087f3

Browse files
committed
fix(ci): make the benchmark table more compact
1 parent 0edbf7c commit a3087f3

2 files changed

Lines changed: 133 additions & 5 deletions

File tree

tests/Benchmark/Extension/MarkdownRenderer.php

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
final readonly class MarkdownRenderer implements RendererInterface
1919
{
20+
private const array COMPACT_HEADERS = ['Benchmark', 'Set', 'Mem. Peak', 'Time', 'Variability'];
21+
22+
private const array COMPACT_SOURCE_COLUMNS = ['benchmark', 'subject', 'set', 'mem_peak', 'mode', 'rstdev'];
23+
2024
public function __construct(
2125
private OutputInterface $output,
2226
private Printer $printer,
@@ -71,11 +75,14 @@ private function renderTable(Table $table): array
7175
return $lines;
7276
}
7377

78+
$rows = array_map($this->renderTableRow(...), $table->rows());
79+
[$columns, $rows] = $this->compactAggregateReportTable($columns, $rows);
80+
7481
$lines[] = $this->renderRow($columns);
7582
$lines[] = $this->renderSeparatorRow($columns);
7683

77-
foreach ($table as $row) {
78-
$lines[] = $this->renderDataRow($row);
84+
foreach ($rows as $row) {
85+
$lines[] = $this->renderRow($row);
7986
}
8087

8188
$lines[] = '';
@@ -96,11 +103,52 @@ private function renderSeparatorRow(array $columns): string
96103
));
97104
}
98105

99-
private function renderDataRow(TableRow $row): string
106+
private function renderTableRow(TableRow $row): array
107+
{
108+
return array_values(array_map($this->formatCell(...), iterator_to_array($row)));
109+
}
110+
111+
private function compactAggregateReportTable(array $columns, array $rows): array
112+
{
113+
$columnIndexes = $this->resolveCompactSourceColumnIndexes($columns);
114+
115+
if ($columnIndexes === null) {
116+
return [$columns, $rows];
117+
}
118+
119+
$rows = array_map(function (array $row) use ($columnIndexes): array {
120+
$set = trim((string) $row[$columnIndexes['set']]);
121+
122+
return [
123+
sprintf('%s(%s)', $row[$columnIndexes['benchmark']], $row[$columnIndexes['subject']]),
124+
$set === '' ? '-' : $set,
125+
$row[$columnIndexes['mem_peak']],
126+
$row[$columnIndexes['mode']],
127+
$row[$columnIndexes['rstdev']],
128+
];
129+
}, $rows);
130+
131+
return [self::COMPACT_HEADERS, $rows];
132+
}
133+
134+
private function resolveCompactSourceColumnIndexes(array $columns): ?array
100135
{
101-
$cells = array_map($this->formatCell(...), iterator_to_array($row));
136+
$columnIndexes = array_flip($columns);
137+
138+
foreach (self::COMPACT_SOURCE_COLUMNS as $column) {
139+
if (! array_key_exists($column, $columnIndexes)) {
140+
return null;
141+
}
142+
}
102143

103-
return $this->renderRow($cells);
144+
return [
145+
'benchmark' => $columnIndexes['benchmark'],
146+
'subject' => $columnIndexes['subject'],
147+
'set' => $columnIndexes['set'],
148+
'mem_peak' => $columnIndexes['mem_peak'],
149+
'mode' => $columnIndexes['mode'],
150+
'rstdev' => $columnIndexes['rstdev'],
151+
];
104152
}
105153

106154
private function formatCell(Node $node): string
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Tempest\Benchmark\Extension;
6+
7+
use PhpBench\Expression\Printer\BareValuePrinter;
8+
use PhpBench\Registry\Config;
9+
use PhpBench\Report\Model\Builder\ReportBuilder;
10+
use PhpBench\Report\Model\Builder\TableBuilder;
11+
use PhpBench\Report\Model\Reports;
12+
use PHPUnit\Framework\Attributes\Test;
13+
use PHPUnit\Framework\TestCase;
14+
use Symfony\Component\Console\Output\BufferedOutput;
15+
16+
final class MarkdownRendererTest extends TestCase
17+
{
18+
#[Test]
19+
public function it_renders_a_compact_aggregate_report_table(): void
20+
{
21+
$output = new BufferedOutput();
22+
$renderer = new MarkdownRenderer($output, new BareValuePrinter());
23+
24+
$table = TableBuilder::create()
25+
->withTitle('Benchmark Results')
26+
->addRowArray([
27+
'benchmark' => 'ContainerBench',
28+
'subject' => 'benchAutowireSimple',
29+
'set' => '',
30+
'revs' => 1000,
31+
'its' => 5,
32+
'mem_peak' => '3.952mb 0.00%',
33+
'mode' => '4.187μs +0.17%',
34+
'rstdev' => '±2.05% +108.06%',
35+
])
36+
->build();
37+
38+
$reports = Reports::fromReport(ReportBuilder::create()->addObject($table)->build());
39+
40+
$renderer->render($reports, new Config('markdown', ['file' => null]));
41+
42+
$this->assertSame(<<<'MARKDOWN'
43+
## Benchmark Results
44+
45+
| Benchmark | Set | Mem. Peak | Time | Variability |
46+
| --------- | --- | --------- | ---- | ----------- |
47+
| ContainerBench(benchAutowireSimple) | - | 3.952mb 0.00% | 4.187μs +0.17% | ±2.05% +108.06% |
48+
49+
50+
MARKDOWN,
51+
52+
$output->fetch());
53+
}
54+
55+
#[Test]
56+
public function it_keeps_non_aggregate_table_columns_unchanged(): void
57+
{
58+
$output = new BufferedOutput();
59+
$renderer = new MarkdownRenderer($output, new BareValuePrinter());
60+
61+
$table = TableBuilder::create()
62+
->addRowArray([
63+
'name' => 'Example',
64+
'value' => '123',
65+
])
66+
->build();
67+
68+
$reports = Reports::fromReport(ReportBuilder::create()->addObject($table)->build());
69+
70+
$renderer->render($reports, new Config('markdown', ['file' => null]));
71+
72+
$this->assertSame(<<<'MARKDOWN'
73+
| name | value |
74+
| ---- | ----- |
75+
| Example | 123 |
76+
77+
78+
MARKDOWN, $output->fetch());
79+
}
80+
}

0 commit comments

Comments
 (0)