Skip to content

Instantly share code, notes, and snippets.

@Krinkle
Last active March 8, 2025 05:05
Show Gist options
  • Save Krinkle/02e3dacdc8e32cc04efdc464593aa262 to your computer and use it in GitHub Desktop.
Save Krinkle/02e3dacdc8e32cc04efdc464593aa262 to your computer and use it in GitHub Desktop.
#!/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