-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCSSJanusBenchmark.php
81 lines (74 loc) · 2.28 KB
/
CSSJanusBenchmark.php
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
<?php
class CSSJanusBenchmark {
private array $fixtures;
/**
* @param null|array<string,string> $fixtures Map from label to input CSS
*/
public function __construct( ?array $fixtures = null ) {
$this->fixtures = $fixtures ?? self::getFixtures();
}
public function run() {
foreach ( $this->fixtures as $name => $data ) {
$iterations = 10_000;
$total = 0;
$max = -INF;
$i = 0;
for ( $i = 1; $i <= $iterations; $i++ ) {
$start = microtime( true );
CSSJanus::transform( $data, [ 'transformDirInUrl' => true ] );
$took = ( microtime( true ) - $start ) * 1000;
$max = max( $max, $took );
$total += ( microtime( true ) - $start ) * 1000;
}
$this->outputStat( $name, $data, $iterations, $total, $max );
}
}
protected function outputStat( string $name, array $data, int $iterations, float $total, float $max ) {
$version = hash( 'fnv132', $data );
$mean = $total / $iterations; // in milliseconds
$ratePerSecond = 1.0 / ( $mean / 1000.0 );
echo sprintf(
"* %-35s iterations=%d max=%.2fms mean=%.2fms rate=%.0f/s\n",
"$name ($version)",
$iterations,
$max,
$mean,
$ratePerSecond
);
}
private function formatSize( int $size ): int {
$i = floor( log( $size, 1024 ) );
return round( $size / pow( 1024, $i ), [ 0, 0, 2, 2, 3 ][$i] ) . ' ' . [ 'B', 'KB', 'MB', 'GB', 'TB' ][$i];
}
protected function getFixtures(): array {
$fixtures = [
'mediawiki-legacy-shared' => [
'version' => '1064426',
'src' => 'https://github.com/wikimedia/mediawiki/raw/1064426'
. '/resources/src/mediawiki.legacy/shared.css',
],
'ooui-core' => [
'version' => '130344b',
'src' => 'https://github.com/wikimedia/mediawiki/raw/130344b'
. '/resources/lib/oojs-ui/oojs-ui-core-wikimediaui.css',
],
];
$result = [];
$dir = __DIR__;
foreach ( $fixtures as $name => $desc ) {
$file = "{$dir}/data-fixture-{$name}.{$desc['version']}.css";
if ( !is_readable( $file ) ) {
array_map( 'unlink', glob( "{$dir}/data-fixture-{$name}.*" ) );
$data = file_get_contents( $desc['src'] );
if ( $data === false ) {
throw new Exception( "Failed to fetch fixture: {$name}" );
}
file_put_contents( $file, $data );
} else {
$data = file_get_contents( $file );
}
$result[$name] = $data;
}
return $result;
}
}