|
| 1 | +<?php |
| 2 | + |
| 3 | +if (file_exists($file = __DIR__.'/../vendor/autoload.php')) { |
| 4 | + $autoload = require_once $file; |
| 5 | + $autoload->addPsr4('Vendor\\Rx\\Operator\\', __DIR__ . '/custom-operator'); |
| 6 | +} else { |
| 7 | + throw new RuntimeException('Install dependencies to run benchmark suite.'); |
| 8 | +} |
| 9 | + |
| 10 | +use Rx\Observable; |
| 11 | +use Rx\Observer\CallbackObserver; |
| 12 | + |
| 13 | +define('MIN_TOTAL_DURATION', 5); |
| 14 | +$start = microtime(true); |
| 15 | + |
| 16 | +if ($_SERVER['argc'] === 1) { |
| 17 | + $files = glob(__DIR__ . '/**/*.php'); |
| 18 | +} else { |
| 19 | + $files = []; |
| 20 | + foreach (array_slice($_SERVER['argv'], 1) as $fileOrDir) { |
| 21 | + if (is_dir($fileOrDir)) { |
| 22 | + $files = array_merge($files, glob($fileOrDir . '/*.php')); |
| 23 | + } else { |
| 24 | + // Force absolute path |
| 25 | + $files[] = $file[0] === DIRECTORY_SEPARATOR ? $file : $_SERVER['PWD'] . DIRECTORY_SEPARATOR . $file; |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +Observable::just($files) |
| 32 | + ->doOnNext(function(array $files) { |
| 33 | + printf("Benchmarking %d file/s (min %ds each)\n", count($files), MIN_TOTAL_DURATION); |
| 34 | + printf("script_name - total_runs (single_run_mean ±standard_deviation)\n"); |
| 35 | + printf("==============================================================\n"); |
| 36 | + }) |
| 37 | + ->concatMap(function($files) { // Flatten the array |
| 38 | + return Observable::fromArray($files); |
| 39 | + }) |
| 40 | + ->doOnNext(function($file) { |
| 41 | + printf('%s', pathinfo($file, PATHINFO_FILENAME)); |
| 42 | + }) |
| 43 | + ->map(function($file) { // Run benchmark |
| 44 | + $totalDuration = 0.0; |
| 45 | + $durations = []; |
| 46 | + |
| 47 | + ob_start(); |
| 48 | + |
| 49 | + $dummyObserver = new Rx\Observer\CallbackObserver( |
| 50 | + function ($value) { }, |
| 51 | + function ($error) { }, |
| 52 | + function () { } |
| 53 | + ); |
| 54 | + |
| 55 | + $testClosure = @include $file; |
| 56 | + if (!$testClosure) { |
| 57 | + throw new Exception("Unable to load file \"$file\""); |
| 58 | + } |
| 59 | + |
| 60 | + while ($totalDuration < MIN_TOTAL_DURATION) { |
| 61 | + $start = microtime(true); |
| 62 | + |
| 63 | + $testClosure(); |
| 64 | + |
| 65 | + $duration = microtime(true) - $start; |
| 66 | + |
| 67 | + $durations[] = $duration * 1000; |
| 68 | + $totalDuration += $duration; |
| 69 | + } |
| 70 | + |
| 71 | + ob_end_clean(); |
| 72 | + |
| 73 | + return [ |
| 74 | + 'file' => $file, |
| 75 | + 'durations' => $durations, |
| 76 | + ]; |
| 77 | + }) |
| 78 | + ->doOnNext(function(array $result) { // Print the number of successful runs |
| 79 | + printf(' - %d', count($result['durations'])); |
| 80 | + }) |
| 81 | + ->map(function(array $result) { // Calculate the standard deviation |
| 82 | + $count = count($result['durations']); |
| 83 | + $mean = array_sum($result['durations']) / $count; |
| 84 | + |
| 85 | + $variance = array_sum(array_map(function($duration) use ($mean) { |
| 86 | + return pow($mean - $duration, 2); |
| 87 | + }, $result['durations'])); |
| 88 | + |
| 89 | + return [ |
| 90 | + 'file' => $result['file'], |
| 91 | + 'mean' => $mean, |
| 92 | + 'standard_deviation' => pow($variance / $count, 0.5), |
| 93 | + ]; |
| 94 | + }) |
| 95 | + ->subscribe(new CallbackObserver( |
| 96 | + function(array $result) { |
| 97 | + printf(" (%.2fms ±%.2fms)\n", $result['mean'], $result['standard_deviation']); |
| 98 | + }, |
| 99 | + function(\Exception $error) { |
| 100 | + printf("\nError: %s\n", $error->getMessage()); |
| 101 | + }, |
| 102 | + function() use ($start) { |
| 103 | + printf("============================================================\n"); |
| 104 | + printf("total duration: %.2fs\n", microtime(true) - $start); |
| 105 | + } |
| 106 | + )); |
0 commit comments