Skip to content

Commit 04a1594

Browse files
martinsikdavidwdan
authored andcommitted
Moved Benchmark from v1
1 parent 5d90762 commit 04a1594

File tree

14 files changed

+346
-0
lines changed

14 files changed

+346
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Rx\Observable;
4+
5+
return function() use ($dummyObserver) {
6+
Observable::range(0, 25)
7+
->bufferWithCount(5)
8+
->subscribe($dummyObserver);
9+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use Rx\Observable;
4+
use Rx\Scheduler\EventLoopScheduler;
5+
use React\EventLoop\StreamSelectLoop;
6+
7+
$loop = new StreamSelectLoop();
8+
$scheduler = new EventLoopScheduler($loop);
9+
10+
return function() use ($dummyObserver, $scheduler, $loop) {
11+
Observable::range(0, 25)
12+
->bufferWithCount(5)
13+
->subscribe($dummyObserver, $scheduler);
14+
15+
$loop->run();
16+
};

benchmark/distinct/distinct.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use Rx\Observable;
4+
5+
$source = array_map(function($val) {
6+
return $val % 3;
7+
}, range(0, 25));
8+
9+
return function() use ($source, $dummyObserver) {
10+
Observable::fromArray($source)
11+
->distinct()
12+
->subscribe($dummyObserver);
13+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Rx\Observable;
4+
use Rx\Scheduler\EventLoopScheduler;
5+
use React\EventLoop\StreamSelectLoop;
6+
7+
$loop = new StreamSelectLoop();
8+
$scheduler = new EventLoopScheduler($loop);
9+
10+
$source = array_map(function($val) {
11+
return $val % 3;
12+
}, range(0, 25));
13+
14+
return function() use ($source, $dummyObserver, $scheduler, $loop) {
15+
Observable::fromArray($source)
16+
->distinct()
17+
->subscribe($dummyObserver, $scheduler);
18+
19+
$loop->run();
20+
};

benchmark/filter/filter.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
use Rx\Observable;
4+
5+
return function() use ($dummyObserver) {
6+
Observable::range(0, 50)
7+
->filter(function($value) {
8+
return $value % 2 == 0;
9+
})
10+
->filter(function($value) {
11+
return $value % 10 == 0;
12+
})
13+
->subscribe($dummyObserver);
14+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Rx\Observable;
4+
use Rx\Scheduler\EventLoopScheduler;
5+
use React\EventLoop\StreamSelectLoop;
6+
7+
$loop = new StreamSelectLoop();
8+
$scheduler = new EventLoopScheduler($loop);
9+
10+
return function() use ($dummyObserver, $scheduler, $loop) {
11+
Observable::range(0, 50)
12+
->filter(function($value) {
13+
return $value % 2 == 0;
14+
})
15+
->filter(function($value) {
16+
return $value % 10 == 0;
17+
})
18+
->subscribe($dummyObserver, $scheduler);
19+
20+
$loop->run();
21+
};

benchmark/run.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
));

benchmark/skipLast/skipLast.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Rx\Observable;
4+
5+
return function() use ($dummyObserver) {
6+
Observable::range(0, 500)
7+
->skipLast(50)
8+
->subscribe($dummyObserver);
9+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use Rx\Observable;
4+
use Rx\Scheduler\EventLoopScheduler;
5+
use React\EventLoop\StreamSelectLoop;
6+
7+
$loop = new StreamSelectLoop();
8+
$scheduler = new EventLoopScheduler($loop);
9+
10+
return function() use ($dummyObserver, $scheduler, $loop) {
11+
Observable::range(0, 500)
12+
->skipLast(50)
13+
->subscribe($dummyObserver, $scheduler);
14+
15+
$loop->run();
16+
};

benchmark/takeLast/takeLast.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Rx\Observable;
4+
5+
return function() use ($dummyObserver) {
6+
Observable::range(0, 500)
7+
->takeLast(50)
8+
->subscribe($dummyObserver);
9+
};

0 commit comments

Comments
 (0)