Skip to content

Commit 9a06bc2

Browse files
martinsikdavidwdan
authored andcommitted
added tests using EventLoopScheduler
1 parent 8f9879d commit 9a06bc2

File tree

7 files changed

+122
-10
lines changed

7 files changed

+122
-10
lines changed
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+
};
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_eventloop.php

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: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,26 @@
1313
define('MIN_TOTAL_DURATION', 5);
1414
$start = microtime(true);
1515

16-
if ($_SERVER['argc'] == 1) {
16+
if ($_SERVER['argc'] === 1) {
1717
$files = glob(__DIR__ . '/**/*.php');
1818
} else {
19-
// Force absolute path
20-
$files = array_map(function($file) {
21-
return $file[0] == DIRECTORY_SEPARATOR ? $file : $_SERVER['PWD'] . DIRECTORY_SEPARATOR . $file;
22-
}, array_slice($_SERVER['argv'], 1));
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+
}
2328
}
2429

2530

2631
Observable::just($files)
2732
->doOnNext(function(array $files) {
2833
printf("Benchmarking %d file/s (min %ds each)\n", count($files), MIN_TOTAL_DURATION);
29-
printf("script_name - total_runs (single_run_mean ±standard_variant)\n");
30-
printf("============================================================\n");
34+
printf("script_name - total_runs (single_run_mean ±standard_deviation)\n");
35+
printf("==============================================================\n");
3136
})
3237
->concatMap(function($files) { // Flatten the array
3338
return Observable::fromArray($files);
@@ -73,7 +78,7 @@ function () { }
7378
->doOnNext(function(array $result) { // Print the number of successful runs
7479
printf(' - %d', count($result['durations']));
7580
})
76-
->map(function(array $result) { // Calculate the standard deviance
81+
->map(function(array $result) { // Calculate the standard deviation
7782
$count = count($result['durations']);
7883
$mean = array_sum($result['durations']) / $count;
7984

@@ -84,12 +89,12 @@ function () { }
8489
return [
8590
'file' => $result['file'],
8691
'mean' => $mean,
87-
'standard_variance' => pow($variance / $count, 0.5),
92+
'standard_deviation' => pow($variance / $count, 0.5),
8893
];
8994
})
9095
->subscribe(new CallbackObserver(
9196
function(array $result) {
92-
printf(" (%.2fms ±%.2fms)\n", $result['mean'], $result['standard_variance']);
97+
printf(" (%.2fms ±%.2fms)\n", $result['mean'], $result['standard_deviation']);
9398
},
9499
function(\Exception $error) {
95100
printf("\nError: %s\n", $error->getMessage());
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+
};
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+
->takeLast(50)
13+
->subscribe($dummyObserver, $scheduler);
14+
15+
$loop->run();
16+
};

benchmark/zip/zip_eventloop.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
->zip([Observable::range(0, 25)], function($a, $b) {
13+
return $a + $b;
14+
})
15+
->subscribe($dummyObserver, $scheduler);
16+
17+
$loop->run();
18+
};

0 commit comments

Comments
 (0)