-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.php
More file actions
250 lines (203 loc) · 7.93 KB
/
Copy pathbenchmark.php
File metadata and controls
250 lines (203 loc) · 7.93 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';
use Bugo\BenchmarkUtils\BenchmarkRunner;
use Bugo\BenchmarkUtils\CompilationResult;
use Bugo\BenchmarkUtils\CompilerAdapterInterface;
use Bugo\BenchmarkUtils\ReportGenerator;
use Bugo\BenchmarkUtils\ScssGenerator;
use Bugo\Sass\Compiler as EmbeddedCompiler;
use Bugo\Sass\Options;
use Bugo\SCSS\Cache\CachingCompiler;
use Bugo\SCSS\Cache\TrackingLoader;
use Bugo\SCSS\Compiler as SassCompiler;
use Bugo\SCSS\CompilerOptions;
use Bugo\SCSS\Loader;
use Bugo\SCSS\Style;
use ScssPhp\ScssPhp\Compiler as ScssCompiler;
use ScssPhp\ScssPhp\OutputStyle;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Psr16Cache;
final readonly class CachedBenchmarkCompiler implements CompilerAdapterInterface
{
public function __construct(
private CachingCompiler $compiler,
private string $entryPath,
) {}
public function warmup(?string $code, ?string $sourceFile): void {}
/**
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function compile(?string $code, ?string $sourceFile, bool $includeSourceMap = true): CompilationResult
{
if ($code !== null) {
if (! file_exists($this->entryPath) || file_get_contents($this->entryPath) !== $code) {
file_put_contents($this->entryPath, $code, LOCK_EX);
}
}
$css = $this->compiler->compileFile($this->entryPath);
return new CompilationResult($css);
}
}
function parseBoolCliOption(array $args, string $name, bool $default): bool
{
$prefix = '--' . $name . '=';
foreach ($args as $arg) {
if (! str_starts_with($arg, $prefix)) {
continue;
}
$value = substr($arg, strlen($prefix));
if ($value === '1') {
return true;
}
if ($value === '0') {
return false;
}
}
return $default;
}
$args = $_SERVER['argv'] ?? [];
$benchmarkRuns = 5;
if (isset($args[1]) && ctype_digit((string) $args[1])) {
$benchmarkRuns = max(1, (int) $args[1]);
}
$forceRegenerate = in_array('--regenerate', $args, true);
$scssFile = __DIR__ . DIRECTORY_SEPARATOR . 'generated.scss';
if (! $forceRegenerate && file_exists($scssFile)) {
$scss = (string) file_get_contents($scssFile);
echo "Using existing generated.scss\n";
} else {
$scss = ScssGenerator::generate(200, 4);
file_put_contents($scssFile, $scss, LOCK_EX);
echo "Generated SCSS saved to generated.scss\n";
}
echo 'SCSS size: ' . strlen($scss) . " bytes\n";
$sourceMap = parseBoolCliOption($args, 'source-map', false);
$minimize = parseBoolCliOption($args, 'minimize', false);
$singleRuns = 10;
$warmupRuns = 2;
$outputDir = __DIR__;
$allResults = [];
$aggregate = [];
$compilerList = [
'bugo/scss-php',
'bugo/scss-php+cache',
'bugo/sass-embedded-php',
'scssphp/scssphp',
];
for ($i = 0; $i < $benchmarkRuns; $i++) {
$results = (new BenchmarkRunner())
->setSourceFile($scssFile)
->setRuns($singleRuns)
->setWarmupRuns($warmupRuns)
->setOutputDir($outputDir)
->addCompiler('bugo/scss-php', function () use ($sourceMap, $minimize) {
$options = new CompilerOptions(
style: $minimize ? Style::COMPRESSED : Style::EXPANDED,
sourceFile: 'generated.scss',
outputFile: 'result-bugo-scss-php.css',
sourceMapFile: $sourceMap ? 'result-bugo-scss-php.css.map' : null,
includeSources: false,
outputHexColors: true,
);
return new SassCompiler($options);
})
->addCompiler('bugo/scss-php+cache', function () use ($scss, $scssFile, $sourceMap, $minimize) {
$options = new CompilerOptions(
style: $minimize ? Style::COMPRESSED : Style::EXPANDED,
outputFile: 'result-bugo-scss-php-cache.css',
sourceMapFile: $sourceMap ? 'result-bugo-scss-php-cache.css.map' : null,
includeSources: false,
outputHexColors: true,
);
file_put_contents($scssFile, $scss, LOCK_EX);
$trackingLoader = new TrackingLoader(new Loader([__DIR__]));
$compiler = new SassCompiler($options, $trackingLoader);
$cache = new Psr16Cache(new ArrayAdapter());
return new CachedBenchmarkCompiler(
new CachingCompiler($compiler, $cache, $trackingLoader, $options),
$scssFile,
);
})
->addCompiler('bugo/sass-embedded-php', function () use ($sourceMap, $minimize) {
$compiler = new EmbeddedCompiler();
$compiler->setOptions(new Options(
style: $minimize ? 'compressed' : 'expanded',
includeSources: false,
removeEmptyLines: true,
sourceMapPath: $sourceMap ? 'result-sass-embedded-php.css.map' : null,
sourceFile: 'generated.scss',
));
return $compiler;
})
->addCompiler('scssphp/scssphp', function () use ($scssFile, $sourceMap, $minimize) {
$compiler = new ScssCompiler();
$compiler->setOutputStyle($minimize ? OutputStyle::COMPRESSED : OutputStyle::EXPANDED);
$compiler->setSourceMap($sourceMap ? ScssCompiler::SOURCE_MAP_FILE : ScssCompiler::SOURCE_MAP_NONE);
$compiler->setSourceMapOptions($sourceMap ? [
'sourceMapFilename' => 'result-scssphp-scssphp.css',
'sourceMapURL' => 'result-scssphp-scssphp.css.map',
'sourceMapBasepath' => __DIR__,
'outputSourceFiles' => false,
] : []);
return $compiler;
})
->run();
$allResults[] = $results;
foreach ($compilerList as $compilerName) {
if (! isset($results[$compilerName])) {
continue;
}
$time = $results[$compilerName]['time'];
if (! is_numeric($time)) {
continue;
}
$aggregate[$compilerName]['time'][] = (float) $time;
$aggregate[$compilerName]['size'][] = (float) $results[$compilerName]['size'];
$aggregate[$compilerName]['memory'][] = (float) $results[$compilerName]['memory'];
}
echo PHP_EOL . '## Run ' . ($i + 1) . '/' . $benchmarkRuns . PHP_EOL;
echo ReportGenerator::formatTable($results);
}
$median = static function (array $values): float {
sort($values);
$count = count($values);
$mid = intdiv($count, 2);
if ($count % 2 === 0) {
return ($values[$mid - 1] + $values[$mid]) / 2;
}
return $values[$mid];
};
$results = [];
foreach ($aggregate as $compilerName => $stats) {
$times = $stats['time'];
$sizes = $stats['size'];
$memory = $stats['memory'];
$results[$compilerName] = [
'time' => $median($times),
'size' => $median($sizes),
'memory' => $median($memory),
];
}
echo PHP_EOL . '## Aggregated (median)' . PHP_EOL;
echo ReportGenerator::formatTable($results);
echo PHP_EOL . '## Time Stats (sec)' . PHP_EOL;
echo '| Compiler | Min | Median | Max | Avg |' . PHP_EOL;
echo '|------------|-------------|-------------|-------------|-------------|' . PHP_EOL;
foreach ($aggregate as $compilerName => $stats) {
$times = $stats['time'];
sort($times);
$min = $times[0];
$max = $times[count($times) - 1];
$med = $median($times);
$avg = array_sum($times) / count($times);
echo '| ' . $compilerName
. ' | ' . number_format($min, 4)
. ' | ' . number_format($med, 4)
. ' | ' . number_format($max, 4)
. ' | ' . number_format($avg, 4)
. " |\n";
}
echo PHP_EOL . 'Iterations: ' . $benchmarkRuns . ', runs per iteration: ' . $singleRuns . ', warmup: ' . $warmupRuns . PHP_EOL;
echo 'Options: sourceMap=' . ($sourceMap ? '1' : '0') . ', minimize=' . ($minimize ? '1' : '0') . PHP_EOL;
ReportGenerator::updateMarkdownFile('benchmark.md', $results);