Last active
March 8, 2025 05:05
-
-
Save Krinkle/02e3dacdc8e32cc04efdc464593aa262 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
require_once __DIR__ . '/lib/Less/Autoloader.php'; | |
Less_Autoloader::register(); | |
$opt = [ | |
'cache_dir' => '/tmp/lessphp-example' | |
]; | |
$input = __DIR__ . '/test/Fixtures/bootstrap-3.2/less/bootstrap.less'; | |
$uri_root = '/assets/boostrap'; | |
$fileToUri = [ $input => $uri_root ]; | |
const ITERATIONS = 10; | |
function measure(string $label, $fn) { | |
// Use child processes so that each has its own peak memory | |
$pid = pcntl_fork(); | |
if ($pid) { | |
// main | |
return; | |
} | |
// Warmup | |
$fn(); | |
$t = hrtime(true); | |
for ($i = 0; $i < ITERATIONS; $i++) { | |
$fn(); | |
} | |
$durationNanos = hrtime(true) - $t; | |
$durationMs = round( $durationNanos * 1e-6 ); | |
$peakMemMiB = memory_get_peak_usage() / 1024 / 1024; | |
print sprintf( "%s: %.2f ms, %.2f MiB\n", | |
$label, | |
( $durationMs / ITERATIONS ), | |
$peakMemMiB | |
); | |
exit; | |
} | |
measure('Less_Cache', fn () => Less_Cache::Get($fileToUri, $opt)); | |
pcntl_wait($status); | |
measure('Less_Parser-nocache', static function () use ($opt, $input, $uri_root) { | |
Less_Cache::$cache_dir = false; | |
$parser = new Less_Parser([ 'cache_dir' => null ] + $opt); | |
$parser->parseFile($input, $uri_root); | |
$parser->getCss(); | |
} ); | |
pcntl_wait($status); | |
measure('Less_Parser-cache', static function () use ($opt, $input, $uri_root) { | |
$parser = new Less_Parser($opt); | |
$parser->parseFile($input, $uri_root); | |
$parser->getCss(); | |
} ); | |
pcntl_wait($status); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment