├── LICENSE ├── README.md ├── composer.json ├── examples ├── analytics.php ├── composite.php ├── custom.php ├── leader.php ├── lowrate.php ├── microbench-nontemporal.php ├── microbench-temporal.php ├── servermon.php ├── tick.php └── tuplejoin.php ├── phpunit.xml ├── src └── React │ └── EEP │ ├── Aggregator.php │ ├── Clock.php │ ├── Clock │ ├── Counting.php │ └── Wall.php │ ├── Composite.php │ ├── Event │ ├── Muxed.php │ └── Temporal.php │ ├── Stats │ ├── All.php │ ├── Count.php │ ├── Max.php │ ├── Mean.php │ ├── Min.php │ ├── Stdev.php │ ├── Sum.php │ └── Variance.php │ ├── Util │ ├── Noop.php │ └── Temporal.php │ ├── Window.php │ └── Window │ ├── Monotonic.php │ ├── Periodic.php │ ├── Sliding.php │ └── Tumbling.php └── tests ├── React └── EEP │ ├── Clock │ ├── CountingTest.php │ └── WallTest.php │ ├── Stats │ ├── AllTest.php │ ├── CountTest.php │ ├── MaxTest.php │ ├── MeanTest.php │ ├── MinTest.php │ ├── StdevTest.php │ ├── SumTest.php │ └── VarianceTest.php │ ├── TestFn.php │ └── Window │ ├── MonotonicTest.php │ ├── PeriodicTest.php │ ├── SlidingTest.php │ └── TumblingTest.php └── bootstrap.php /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React/EEP 2 | 3 | A port of Darach Ennis' Embedding Event Processing (eep.js - https://github.com/darach/eep-js) to PHP and React. Take a look at the README there for background and motivation. 4 | 5 | ## Install 6 | 7 | The recommended way to install react/eep is [through composer](http://getcomposer.org). 8 | 9 | ```JSON 10 | { 11 | "require": { 12 | "react/eep": "v0.1.0" 13 | } 14 | } 15 | ``` 16 | 17 | ## Example 18 | 19 | Here is an example of a sum tumbling window 20 | 21 | ```php 22 | on('emit', function($value) { echo "sum:\t", $value, "\n";}); 32 | 33 | // Pump data into the tumbling windows 34 | foreach($values as $v) { 35 | $tumbling_sum->enqueue($v); 36 | } 37 | ``` 38 | 39 | ## Other Examples 40 | 41 | The examples mainly come from Darach's, but also others from my talk on Event Stream Processing in PHP: 42 | 43 | 1. analytics.php - example using the stats functions 44 | 2. composite.php - example combining stats functions 45 | 3. custom.php - Tim Bray's widefinder with a monotonic clock 46 | 4. leader.php - A beast of a custom window + agg that does lagged correlation for leader detection between two streams 47 | 5. lowrate.php - Simple periodic low rate detector example 48 | 6. microbench-nontemporal.php - Darach's event based benchmark 49 | 7. microbench-temporal.php - Darach's periodic benchmark 50 | 8. servermon.php - An example of using the All stats function to track several variables 51 | 9. tick.php - An aggreagate function which implements a simple state machine for pattern detection 52 | 10. tuplejoin.php - An auction themed example doing a hash based semijoin between two streams 53 | 54 | ## Tests 55 | 56 | To run the test suite, you need PHPUnit. 57 | 58 | $ phpunit 59 | 60 | ## License 61 | 62 | See LICENSE. 63 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react/eep", 3 | "description": "EEP event process for React.", 4 | "keywords": ["eep"], 5 | "license": "LGPL", 6 | "require": { 7 | "php": ">=5.3.2", 8 | "evenement/evenement": "1.0 - 2.0", 9 | "react/event-loop": "0.2 - 0.5" 10 | }, 11 | "require-dev": { 12 | "ext-pcntl": "*", 13 | "phpunit/phpunit": "3.7.*" 14 | }, 15 | "autoload": { 16 | "psr-0": { "React\\EEP": "src" } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /examples/analytics.php: -------------------------------------------------------------------------------- 1 | on('emit', function($value) { echo "count:\t", $value, "\n";}); 24 | $tumbling_sum->on('emit', function($value) { echo "sum:\t", $value, "\n";}); 25 | $tumbling_min->on('emit', function($value) { echo "min:\t", $value, "\n";}); 26 | $tumbling_max->on('emit', function($value) { echo "max:\t", $value, "\n";}); 27 | $tumbling_mean->on('emit', function($value) { echo "mean:\t", $value, "\n";}); 28 | $tumbling_stdevs->on('emit', function($value) { echo "stdevs:\t", $value, "\n";}); 29 | $tumbling_vars->on('emit', function($value) { echo "vars:\t", $value, "\n";}); 30 | 31 | echo "\nIndividual tumbling windows\n\n"; 32 | 33 | // Pump data into the tumbling windows 34 | foreach($values as $v) { 35 | $tumbling_count->enqueue($v); 36 | $tumbling_sum->enqueue($v); 37 | $tumbling_min->enqueue($v); 38 | $tumbling_max->enqueue($v); 39 | $tumbling_mean->enqueue($v); 40 | $tumbling_stdevs->enqueue($v); 41 | $tumbling_vars->enqueue($v); 42 | } 43 | 44 | // Alternatively, use a composite aggregate function 45 | $stats = array( 46 | $count_fn, $sum_fn, $min_fn, $max_fn, $mean_fn, $stdevs_fn, $vars_fn 47 | ); 48 | $headers = array("Count\t", "Sum\t", "Min\t", "Max\t", "Mean\t", "Stdev\t", "Vars\t"); 49 | 50 | $comp_fn = new React\EEP\Composite($stats); 51 | $tumbling = new React\EEP\Window\Tumbling($comp_fn, count($values)); 52 | $tumbling->on('emit', function($value) use($headers) { 53 | for($i = 0; $i < count($value); $i++) { 54 | echo $headers[$i], "\t", $value[$i], "\n"; 55 | } 56 | }); 57 | 58 | echo "\nComposite tumbling window\n\n"; 59 | 60 | // Pump data into the tumbling window 61 | foreach($values as $v) { 62 | $tumbling->enqueue($v); 63 | } 64 | 65 | echo "\n"; -------------------------------------------------------------------------------- /examples/composite.php: -------------------------------------------------------------------------------- 1 | count = new React\EEP\Stats\Count; 13 | $this->sum = new React\EEP\Stats\Sum; 14 | $this->min = new React\EEP\Stats\Min; 15 | $this->max = new React\EEP\Stats\Max; 16 | $this->mean = new React\EEP\Stats\Mean; 17 | $this->vars = new React\EEP\Stats\Variance; 18 | $this->stdevs = new React\EEP\Stats\Stdev; 19 | } 20 | public function init() { 21 | $this->count->init(); 22 | $this->sum->init(); 23 | $this->min->init(); 24 | $this->max->init(); 25 | $this->mean->init(); 26 | $this->vars->init(); 27 | $this->stdevs->init(); 28 | } 29 | public function accumulate($v) { 30 | $this->count->accumulate($v); 31 | $this->sum->accumulate($v); 32 | $this->min->accumulate($v); 33 | $this->max->accumulate($v); 34 | $this->mean->accumulate($v); 35 | $this->vars->accumulate($v); 36 | $this->stdevs->accumulate($v); 37 | } 38 | public function compensate($v) { 39 | $this->count->compensate($v); 40 | $this->sum->compensate($v); 41 | $this->min->compensate($v); 42 | $this->max->compensate($v); 43 | $this->mean->compensate($v); 44 | $this->vars->compensate($v); 45 | $this->stdevs->compensate($v); 46 | } 47 | public function emit() { 48 | return array( 49 | "count" => $this->count->emit(), 50 | "sum" => $this->sum->emit(), 51 | "min" => $this->min->emit(), 52 | "max" => $this->max->emit(), 53 | "mean" => $this->mean->emit(), 54 | "vars" => $this->vars->emit(), 55 | "stdevs" => $this->stdevs->emit()); 56 | } 57 | } 58 | 59 | $fns = array( 60 | "count" => new React\EEP\Stats\Count, 61 | "sum" => new React\EEP\Stats\Sum, 62 | "min" => new React\EEP\Stats\Min, 63 | "max" => new React\EEP\Stats\Max, 64 | "mean" => new React\EEP\Stats\Mean, 65 | "vars" => new React\EEP\Stats\Variance, 66 | "stdevs" => new React\EEP\Stats\Stdev, 67 | ); 68 | $headers = array("Count\t\t", "Sum\t\t", "Min\t\t", "Max\t\t", "Mean\t\t", "Variance\t", "Stdev\t\t"); 69 | 70 | // Convenient. But should only be used for composing independant, not related functions 71 | $comp_fn = new React\EEP\Composite($fns); 72 | $clock = new React\EEP\Clock\Wall(0); 73 | $m1 = new React\EEP\Window\Monotonic($comp_fn, $clock); 74 | 75 | // Correct. The stats functions can be coalesced into a single function. 76 | // Faster by 4X 77 | $fn = new React\EEP\Stats\All(); 78 | $clock = new React\EEP\Clock\Counting(); 79 | $m2 = new React\EEP\Window\Monotonic($fn, $clock); 80 | 81 | // Neither. Faster than m1, but slower than m2. Save some cycles, use m2! 82 | $clock = new React\EEP\Clock\Counting(); 83 | $fn = new LeStatFunction(); 84 | $m3 = new React\EEP\Window\Monotonic($fn, $clock); 85 | 86 | $m1->on("emit", function($values) use ($headers) { 87 | foreach($values as $i => $v) { 88 | echo $headers[$i], "\t\t", $v, "\n"; 89 | } 90 | }); 91 | 92 | $m2->on("emit", function($values) { 93 | echo json_encode($values), "\n"; 94 | }); 95 | 96 | $m3->on("emit", function($values) { 97 | echo json_encode($values), "\n"; 98 | }); 99 | 100 | if($_SERVER['argc'] != 2) { 101 | echo "\nUsage: ", $_SERVER['argv'][0], " \n"; 102 | exit; 103 | } 104 | 105 | $items = $_SERVER['argv'][1]; 106 | 107 | $start = microtime(true); 108 | for($i = 1; $i <= $items; $i++) { 109 | $m1->enqueue($i); 110 | } 111 | $m1->tick(); 112 | $end = microtime(true); 113 | printf("V1. Elapsed: %.2f meps: %.3f\n", ($end-$start), $items/($end-$start)/1e6); 114 | 115 | $start = microtime(true); 116 | for($i = 1; $i <= $items; $i++) { 117 | $m2->enqueue($i); 118 | } 119 | $m2->tick(); 120 | $end = microtime(true); 121 | printf("V2. Elapsed: %.2f meps: %.3f\n", ($end-$start), $items/($end-$start)/1e6); 122 | 123 | $start = microtime(true); 124 | for($i = 1; $i <= $items; $i++) { 125 | $m3->enqueue($i); 126 | } 127 | $m3->tick(); 128 | $end = microtime(true); 129 | printf("V3. Elapsed: %.2f\n", ($end-$start)); -------------------------------------------------------------------------------- /examples/custom.php: -------------------------------------------------------------------------------- 1 | re = $regex; 13 | } 14 | 15 | public function init() { 16 | $this->keys = array(); 17 | } 18 | 19 | public function accumulate($line) { 20 | preg_match($this->re, $line, $matches); 21 | if(count($matches) == 0) { 22 | return; 23 | } 24 | $key = $matches[1]; 25 | if(!isset($this->keys[$key])) { 26 | $this->keys[$key] = 1; 27 | } else { 28 | $this->keys[$key]++; 29 | } 30 | } 31 | 32 | public function compensate($line) { 33 | preg_match($this->re, $line, $matches); 34 | if(count($matches) == 0) { 35 | return; 36 | } 37 | $key = $matches[1]; 38 | $this->keys[$key]--; 39 | } 40 | 41 | public function emit() { 42 | return $this->keys; 43 | } 44 | } 45 | 46 | $regex = "/GET \/ongoing\/When\/\d\d\dx\/(\d\d\d\d\/\d\d\/\d\d\/[^ .]+) /"; 47 | $fn = new WideFinderFunction($regex); 48 | $clock = new React\EEP\Clock\Wall(0); 49 | $win = new React\EEP\Window\Monotonic($fn, $clock); 50 | 51 | // On emit, log top 10 hits to standard output 52 | $win->on('emit', function($matches) { 53 | arsort($matches); 54 | $i = 0; 55 | foreach(array_slice($matches, 0, 10) as $url => $n) { 56 | printf("%d:\t%s with %d hits\n", $i++, $url, $n); 57 | } 58 | echo "\n\n"; 59 | }); 60 | 61 | $fh; 62 | if(file_exists("./data.txt")) { 63 | $fh = fopen("data.txt", "r"); 64 | } else { 65 | echo "Fetching data file\n"; 66 | $fh = fopen("http://www.tbray.org/tmp/o10k.ap", "r"); 67 | } 68 | 69 | while($line = fgets($fh)) { 70 | $win->enqueue($line); 71 | } 72 | $win->tick(); 73 | fclose($fh); 74 | -------------------------------------------------------------------------------- /examples/leader.php: -------------------------------------------------------------------------------- 1 | size = $size; 18 | $this->buffers = new SplFixedArray(2); 19 | $this->buffers[0] = new SplFixedArray($size); 20 | $this->buffers[1] = new SplFixedArray($size); 21 | $this->index = new SplFixedArray(2); 22 | $this->index[0] = $this->index[1] = 0; 23 | 24 | // This is used to divide the main window into the lagged offsets. 25 | $this->sub = $sub_window_size; 26 | 27 | // Don't emit until we have collected one master set. 28 | $this->mark = $size; 29 | 30 | // Maintain a set of aggregate functions. 31 | $num_splits = $this->size / $this->sub; 32 | $this->aggregates = new SplFixedArray(2); 33 | $this->aggregates[0] = new SplFixedArray($num_splits); 34 | $this->aggregates[1] = new SplFixedArray($num_splits); 35 | for($i = 0; $i < $num_splits; $i++) { 36 | $this->aggregates[0][$i] = new React\EEP\Stats\All(); 37 | $this->aggregates[0][$i]->init(); 38 | $this->aggregates[1][$i] = new React\EEP\Stats\All(); 39 | $this->aggregates[1][$i]->init(); 40 | } 41 | } 42 | 43 | public function enqueue($event) { 44 | // We have to be a bit clever here to get the right windows. 45 | foreach($this->aggregates[$event->stream] as $i => $fn) { 46 | $offset = $this->index[$event->stream] - ($i * $this->sub); 47 | 48 | // If we have enough events for the lagged stream. 49 | if($offset > 0) { 50 | $fn->accumulate($event->value); 51 | } 52 | 53 | // If we have a full window. 54 | if($this->index[$event->stream] > $this->mark) { 55 | $po = ($offset + 1) % $this->size; 56 | $fn->compensate($this->buffers[$event->stream][$po]); 57 | } 58 | } 59 | 60 | // If both streams are past the mark, we can correlate 61 | $other = $event->stream ^ 1; 62 | $max_cor = 0; 63 | $cor = 0; 64 | if($this->index[$event->stream] > $this->mark && 65 | $this->index[$other] > $this->mark ) { 66 | // Cross compare windows 67 | foreach($this->aggregates[0] as $i => $fn) { 68 | foreach($this->aggregates[1] as $j => $ofn) { 69 | $xo = ($this->index[0] - ((1+$i) * $this->sub)) % $this->size; 70 | $x_stats = $fn->emit(); 71 | $yo = ($this->index[1] - ((1+$j) * $this->sub)) % $this->size; 72 | $y_stats = $ofn->emit(); 73 | $c = $this->correlate($xo, $x_stats, $yo, $y_stats); 74 | if(abs($c) - abs($max_cor) > 0.01) { 75 | $max_cor = $c; 76 | $cor = $i - $j; 77 | } 78 | } 79 | } 80 | } 81 | 82 | // If 0, we have no leader 83 | if($cor != 0) { 84 | $this->emit('emit', array(array($cor, $max_cor))); 85 | } 86 | 87 | $buf_index = $this->index[$event->stream] % $this->size; 88 | $this->buffers[$event->stream][$buf_index] = $event->value; 89 | $this->index[$event->stream] += 1; 90 | } 91 | 92 | /** 93 | * Bog Standard Pearson correlation 94 | * correlation(x,y) = mean((x - mean(x)) * (y - mean(y))) / 95 | * (stddev(x) * stddev(y)) 96 | */ 97 | private function correlate($xo, $x_stats, $yo, $y_stats) { 98 | $acc = 0; 99 | for($i = 0; $i < $this->sub; $i++) { 100 | $acc += ($this->buffers[0][($xo + $i) % $this->size] - $x_stats['mean']) * 101 | ($this->buffers[1][($yo + $i) % $this->size] - $y_stats['mean']); 102 | } 103 | $acc /= $this->sub; 104 | $stddevs = ($x_stats['stdevs'] * $y_stats['stdevs']); 105 | return ($stddevs == 0 ? 0 : $acc /$stddevs) ; 106 | } 107 | } 108 | 109 | // Lets calculate 4 subwindows. 110 | $size = 12; $subwin = 3; 111 | $win = new LeaderFinder($size, $subwin); 112 | $win->on('emit', function($correlation) use ($subwin) { 113 | list($lead, $follow) = $correlation[0] > 0 ? array(0, 1) : array(1, 0); 114 | $diff = abs($correlation[0]) * $subwin; 115 | printf("Stream %d leads stream %d by %d ticks with a %.2f correlation\n", 116 | $lead, $follow, $diff, $correlation[1]); 117 | }); 118 | 119 | // Generate some correlated streams 120 | $a = array(1, 1, 1, 3, 3, 3, 5, 5, 5, 7, 7, 7, 9, 9, 9, 11, 11, 11); 121 | $b = array(1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 5, 5, 5, 7, 7, 7); 122 | 123 | for($i = 0; $i < count($a); $i++) { 124 | $win->enqueue(new React\EEP\Event\Muxed($a[$i], 0)); 125 | $win->enqueue(new React\EEP\Event\Muxed($b[$i], 1)); 126 | } -------------------------------------------------------------------------------- /examples/lowrate.php: -------------------------------------------------------------------------------- 1 | on('emit', function($count) { 10 | if($count < 50) { 11 | echo "Alert - Low Rate! - $count\n"; 12 | } else { 13 | echo "$count :)\n"; 14 | } 15 | }); 16 | 17 | while(true) { 18 | $win->enqueue(array(300, 4, 5, 10)); 19 | $win->tick(); 20 | usleep(100000 + rand(-20000, 20000)); 21 | } 22 | -------------------------------------------------------------------------------- /examples/microbench-nontemporal.php: -------------------------------------------------------------------------------- 1 | enqueue(1); 18 | } while(--$count > 0); 19 | $elapsed = microtime(true) - $start; 20 | $meps = ($items/$elapsed) / 10e6; 21 | printf("%s,%s,%d,%.3f,%.3f\n", $type, $name, $size, $elapsed, $meps); 22 | } 23 | 24 | $fns = array( 25 | "noop" => new React\EEP\Util\Noop, 26 | "count" => new React\EEP\Stats\Count, 27 | "sum" => new React\EEP\Stats\Sum, 28 | "min" => new React\EEP\Stats\Min, 29 | "max" => new React\EEP\Stats\Max, 30 | "mean" => new React\EEP\Stats\Mean, 31 | "vars" => new React\EEP\Stats\Variance, 32 | "stdevs" => new React\EEP\Stats\Stdev, 33 | // TODO: All 34 | ); 35 | 36 | $sizes = array(2,4,8,16,32,64,128,256,512); 37 | $items = 100000; // 100k :( 38 | 39 | echo "Tumbling\n"; 40 | foreach($fns as $name => $fn) { 41 | head("Tumbling", $name); 42 | foreach($sizes as $size) { 43 | $tumbling = new React\EEP\Window\Tumbling($fn, $size); 44 | bench('tumbling', $name, $tumbling, $size, $items); 45 | } 46 | foot(); 47 | } 48 | 49 | echo "Sliding\n"; 50 | foreach($fns as $name => $fn) { 51 | head("Sliding", $name); 52 | foreach($sizes as $size) { 53 | $tumbling = new React\EEP\Window\Sliding($fn, $size); 54 | bench('sliding', $name, $tumbling, $size, $items); 55 | } 56 | foot(); 57 | } -------------------------------------------------------------------------------- /examples/microbench-temporal.php: -------------------------------------------------------------------------------- 1 | max = $max; 10 | $this->count = $this->n = 0; 11 | } 12 | 13 | public function run($spec, $items, $size, $fn, $cb) { 14 | $start = microtime(true); 15 | $this->n = $fn(); 16 | $end = microtime(true); 17 | $delta = $end - $start; 18 | $rate = ($this->n / $delta) / 1e6; 19 | $this->emit('end', array($cb, $size, $spec, $delta, $rate)); 20 | } 21 | 22 | public function runSuite($op, $size, $fn) { 23 | $this->n = 0; $this->count= 0; 24 | $win = new React\EEP\Window\Periodic($op, $size); 25 | $win->on('emit', function($value) { $this->count++; }); 26 | $this->run('Inline Periodic', null, $size, function() use ($win, $size) { 27 | $ticks = floor($this->max / $size); 28 | $n = 0; 29 | do { 30 | $win->tick(); 31 | $win->enqueue($n++); 32 | } while($this->count < $ticks); 33 | return $n; 34 | }, $fn); 35 | } 36 | 37 | public function suite($op, $size, $fn) { 38 | $context = $this->runSuite($op, $size, $fn); 39 | return array("context" => $context); 40 | } 41 | } 42 | echo 'Micro Benching Embedded Event Processing', "\n"; 43 | 44 | $millis = 5000; 45 | $bench = new MicroBench($millis); 46 | $bench->on('end', function($fn, $size, $spec, $secs, $rate) { 47 | $fn($size, $spec, $secs, $rate); 48 | }); 49 | 50 | $ops = array( 51 | "noop" => new React\EEP\Util\Noop, 52 | "count" => new React\EEP\Stats\Count, 53 | "sum" => new React\EEP\Stats\Sum, 54 | "min" => new React\EEP\Stats\Min, 55 | "max" => new React\EEP\Stats\Max, 56 | "mean" => new React\EEP\Stats\Mean, 57 | "stdevs" => new React\EEP\Stats\Stdev, 58 | "vars" => new React\EEP\Stats\Variance, 59 | ); 60 | 61 | $sizes = array(1000, 100, 10, 1); 62 | echo "1000\t100\t10\t1\top\n"; 63 | foreach($ops as $name => $op) { 64 | $results = array(); 65 | $record = function($size, $spec, $secs, $rate) use (&$results) { 66 | $results[] = array("size" => $size, 67 | "name" => $spec, 68 | "elapsed" => $secs, 69 | "rate" => $rate); 70 | }; 71 | foreach($sizes as $size) { 72 | $bench->suite($op, $size, $record); 73 | } 74 | printf("%.2f\t%.2f\t%.2f\t%.2f\t%s\n", 75 | $results[0]['rate'], $results[1]['rate'], 76 | $results[2]['rate'], $results[3]['rate'], 77 | $name); 78 | // Verify that we have 4*5s window 79 | // echo time(), "\n"; 80 | } -------------------------------------------------------------------------------- /examples/servermon.php: -------------------------------------------------------------------------------- 1 | on('emit', function($avg) { 8 | if($avg > 290) { 9 | printf("Average %dms - ALERT!\n", $avg); 10 | } 11 | }); 12 | 13 | $start = microtime(true); 14 | for($i = 0; $i < 50000; $i++) { 15 | $var = 275 + rand(-150, 150); 16 | $awin->enqueue($var); 17 | } 18 | 19 | echo "\n\n#########\n\n"; 20 | 21 | echo "Monitor combined variables\n"; 22 | 23 | $all_fn = new React\EEP\Stats\All; 24 | $all_win = new React\EEP\Window\Tumbling($all_fn, 100); 25 | 26 | $all_win->on('emit', function($vals) { 27 | if($vals['stdevs'] > 92 && $vals['mean'] > 280 && $vals['max'] > 400) { 28 | printf("Stddev %.2fms Average %dms Max %dms - ALERT!\n", 29 | $vals['stdevs'], $vals['mean'], $vals['max']); 30 | } 31 | }); 32 | 33 | $start = microtime(true); 34 | for($i = 0; $i < 50000; $i++) { 35 | $var = 275 + rand(-150, 150); 36 | $all_win->enqueue($var); 37 | } -------------------------------------------------------------------------------- /examples/tick.php: -------------------------------------------------------------------------------- 1 | state = 0; 10 | $this->data = array(); 11 | $this->time = null; 12 | } 13 | 14 | public function accumulate($v) { 15 | switch($this->state) { 16 | case 0: 17 | // Initial state. Always accept the value. 18 | $this->data[0] = $v->value; 19 | $this->time = $v->at; 20 | $this->state = 1; 21 | break; 22 | case 1: 23 | // Look for a drop. 24 | if($v->value < $this->data[0]) { 25 | $this->data[1] = $v->value; 26 | $this->state = 2; 27 | } else { 28 | $this->reset($v); 29 | } 30 | break; 31 | case 2: 32 | // Look for an uptick to a higher than original price. 33 | if($v->value > $this->data[0]) { 34 | $this->data[2] = $v->value; 35 | $this->state = 3; 36 | } else { 37 | $this->reset($v); 38 | } 39 | break; 40 | case 3: 41 | // We're detecting a drop again, so we can look for any price. 42 | $this->data[3] = $v->value; 43 | $this->state = 4; 44 | break; 45 | case 4: 46 | // We need a drop, if not, update value and wait. 47 | if($v->value < $this->data[3]) { 48 | // We got one! 49 | $this->state = 5; 50 | } 51 | $this->data[3] = $v->value; 52 | break; 53 | case 5: 54 | // Just reset. 55 | $this->reset($v); 56 | break; 57 | } 58 | } 59 | 60 | private function reset($v) { 61 | $this->state = 0; 62 | $this->accumulate($v); 63 | } 64 | 65 | public function compensate($v) { 66 | // We only care about our example sliding out. 67 | if($this->state > 0 && $v->at == $this->time && $v->value == $this->time) { 68 | // If we've expired, reset. 69 | $this->init(); 70 | } 71 | } 72 | 73 | public function emit() { 74 | if($this->state == 5) { 75 | return $this->data; 76 | } 77 | } 78 | } 79 | 80 | $tick_fn = new TickDetector(); 81 | $cb = function($value) { 82 | if($value) { 83 | list($start, $low, $high, $cur) = $value; 84 | printf("Value: %d Prev Gain: %d\n", $cur, $high - $low); 85 | } 86 | }; 87 | 88 | echo "Test on known data\n\n"; 89 | 90 | // Pump a little data. 91 | $values = array(290, 290, 300, 300, 320, 280, 340, 340, 350, 300, 290, 300); 92 | $win = new React\EEP\Window\Sliding($tick_fn, 7); 93 | $win->on('emit', $cb); 94 | foreach($values as $at => $value) { 95 | $win->enqueue(new React\EEP\Event\Temporal($value, $at)); 96 | } -------------------------------------------------------------------------------- /examples/tuplejoin.php: -------------------------------------------------------------------------------- 1 | stream = array(); 12 | } 13 | public function init() { 14 | $this->stream[0] = array(); 15 | $this->stream[1] = array(); 16 | $this->last_value = null; 17 | } 18 | 19 | /** 20 | * Take a muxed tuple, and treat the first 21 | * value as the join key. 22 | */ 23 | public function accumulate($v) { 24 | $this->last_value = null; 25 | $key = $v->value[0]; 26 | if(!isset($this->stream[$v->stream][$key])) { 27 | $this->stream[$v->stream][$key] = 0; 28 | } 29 | $this->stream[$v->stream][$key] += 1; 30 | 31 | if(isset($this->stream[$v->stream ^ 1][$key])) { 32 | $this->last_value = $v->value; 33 | } 34 | } 35 | 36 | public function compensate($v) { 37 | $key = $v->value[0]; 38 | $this->stream[$v->stream][$key]--; 39 | if($this->stream[$v->stream][$key] == 0) { 40 | unset($this->stream[$v->stream][$key]); 41 | } 42 | } 43 | 44 | public function emit() { 45 | return $this->last_value; 46 | } 47 | } 48 | 49 | $fn = new Semijoin(); 50 | $a_win = new React\EEP\Window\Sliding($fn, 100); 51 | $b_win = new React\EEP\Window\Sliding($fn, 100); 52 | 53 | $counter = 0; 54 | $match = function($match) use (&$counter) { 55 | if($match) { 56 | $counter++; 57 | printf("%s \n", $match[1]); 58 | } 59 | }; 60 | $a_win->on("emit", $match); 61 | $b_win->on("emit", $match); 62 | 63 | $event_count = 50000; 64 | $cats = array("toys", "electrical", "cars", "clothing", "homewares"); 65 | $start = microtime(true); 66 | $i = 0; 67 | for($i = 0; $i < $event_count; $i++) { 68 | // Key - e.g auction ID. 69 | $key = rand(1, 5000); 70 | 71 | // Randomly select a stream. 72 | if(rand(0, 1) == 0) { 73 | // Generate an list item event. (auction id, category) 74 | $value = array($key, $cats[array_rand($cats)]); 75 | $a_win->enqueue(new React\EEP\Event\Muxed($value, 0)); 76 | } else { 77 | // Generate an end auction event. (action id, category, price) 78 | $value = array($key, $cats[array_rand($cats)], rand(10, 500)); 79 | $b_win->enqueue(new React\EEP\Event\Muxed($value, 1)); 80 | } 81 | } 82 | $time = microtime(true) - $start; 83 | 84 | // printf("%.2feps - %d matches\n", $event_count/$time, $counter); -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 14 | 15 | 16 | ./tests/React/ 17 | 18 | 19 | 20 | 21 | 22 | ./src/ 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/React/EEP/Aggregator.php: -------------------------------------------------------------------------------- 1 | mark = null; 11 | } 12 | 13 | public function at() { 14 | return $this->at; 15 | } 16 | 17 | public function init() { 18 | $this->at = $this->mark = 0; 19 | return $this->at; 20 | } 21 | 22 | public function inc() { 23 | $this->at++; 24 | } 25 | 26 | public function tick() { 27 | if($this->mark === null) { 28 | $this->mark = $this->at + 1; 29 | } 30 | return (($this->at - $this->mark) >= 1); 31 | } 32 | 33 | public function tock($elapsed) { 34 | $delta = $this->at - $elapsed; 35 | if($delta >= 1) { 36 | $this->mark += 1; 37 | return true; 38 | } 39 | return false; 40 | } 41 | } -------------------------------------------------------------------------------- /src/React/EEP/Clock/Wall.php: -------------------------------------------------------------------------------- 1 | interval = $interval; 14 | $this->mark = null; 15 | } 16 | 17 | public function at() { 18 | return $this->at; 19 | } 20 | 21 | public function init() { 22 | $this->at = intval(microtime(true) * 1000); 23 | $this->mark = $this->at + $this->interval; 24 | return $this->at; 25 | } 26 | 27 | public function inc() { 28 | $this->at = intval(microtime(true) * 1000); 29 | return $this->at; 30 | } 31 | 32 | public function tick() { 33 | if($this->mark === null) { 34 | $this->mark = $this->at + $this->interval; 35 | } 36 | 37 | // The mark is our point in time to trip over. If 38 | // its in the future ("at") being the current time 39 | // we return false. If it's in the past then at - mark 40 | // will be positive, and we can tick. The tock() below 41 | // will handle an update. 42 | return ($this->at - $this->mark) >= 0; 43 | } 44 | 45 | public function tock($time) { 46 | // If time is in the past, at (now) - time will yield a 47 | // positive number. If a window has passed (>= interval) 48 | // then we update the mark and move on. 49 | $delta = $this->at - $time; 50 | if($delta >= $this->interval) { 51 | // Added this check to ensure we don't race the clock forwards 52 | // with some reckless tocking. 53 | if($this->tick()) { 54 | $this->mark += $this->interval; 55 | } 56 | return true; 57 | } 58 | return false; 59 | } 60 | } -------------------------------------------------------------------------------- /src/React/EEP/Composite.php: -------------------------------------------------------------------------------- 1 | fns = $fns; 16 | } 17 | 18 | public function init() { 19 | foreach($this->fns as $fn) { 20 | $fn->init(); 21 | } 22 | } 23 | 24 | public function accumulate($v) { 25 | foreach($this->fns as $fn) { 26 | $fn->accumulate($v); 27 | } 28 | } 29 | 30 | public function compensate($v) { 31 | foreach($this->fns as $fn) { 32 | $fn->compensate($v); 33 | } 34 | } 35 | 36 | public function emit() { 37 | $return = array(); 38 | foreach($this->fns as $fn) { 39 | $return[] = $fn->emit(); 40 | } 41 | return $return; 42 | } 43 | } -------------------------------------------------------------------------------- /src/React/EEP/Event/Muxed.php: -------------------------------------------------------------------------------- 1 | value = $value; 13 | $this->stream = $stream; 14 | } 15 | } -------------------------------------------------------------------------------- /src/React/EEP/Event/Temporal.php: -------------------------------------------------------------------------------- 1 | value = $value; 15 | $this->at = $instant; 16 | } 17 | } -------------------------------------------------------------------------------- /src/React/EEP/Stats/All.php: -------------------------------------------------------------------------------- 1 | mean = $this->m2 = $this->delta = $this->n = $this->sum = 0; 17 | $this->min = $this->max = null; 18 | } 19 | 20 | public function accumulate($v) { 21 | if($v == null) { 22 | return; 23 | } 24 | 25 | $cur = $this->n; 26 | $this->sum += $v; 27 | 28 | if($this->min == null || $v < $this->min) { 29 | $this->min = $v; 30 | } 31 | 32 | if($this->max == null || $v > $this->max) { 33 | $this->max = $v; 34 | } 35 | 36 | $this->n++; 37 | 38 | $this->d = $v - $this->mean; 39 | $dn = $this->d / $this->n; 40 | $t1 = $this->d * $dn * $cur; 41 | $this->mean = $this->mean + $dn; 42 | $this->m2 = $this->m2 + $t1; 43 | } 44 | 45 | public function compensate($v) { 46 | if($v == null) { 47 | return; 48 | } 49 | 50 | $cur = $this->n; 51 | $this->sum -= $v; 52 | 53 | $this->n--; 54 | 55 | $this->d = $this->mean - $v; 56 | $dn = $this->d / $this->n; 57 | $t1 = $this->d * $dn * $cur; 58 | $this->mean = $dn; 59 | $this->m2 = $this->m2 - $t1; 60 | } 61 | 62 | public function emit() { 63 | $variance = $this->m2/($this->n-1); 64 | return array( 65 | "count" => $this->n, 66 | "sum" => $this->sum, 67 | "min" => $this->min, 68 | "max" => $this->max, 69 | "mean" => $this->mean, 70 | "vars" => $variance, 71 | "stdevs" => sqrt($variance) 72 | ); 73 | } 74 | } -------------------------------------------------------------------------------- /src/React/EEP/Stats/Count.php: -------------------------------------------------------------------------------- 1 | n = 0; 15 | } 16 | 17 | public function accumulate($_) { 18 | $this->n += 1; 19 | } 20 | 21 | public function compensate($_) { 22 | $this->n -= 1; 23 | } 24 | 25 | public function emit() { 26 | return $this->n; 27 | } 28 | } -------------------------------------------------------------------------------- /src/React/EEP/Stats/Max.php: -------------------------------------------------------------------------------- 1 | cmp = $compare_fn ? $compare_fn : function($a, $b) { 17 | return $a == $b ? 0 : ($a < $b ? -1 : 1); 18 | }; 19 | } 20 | 21 | public function init() { 22 | $this->max = null; 23 | } 24 | 25 | public function accumulate($v) { 26 | $c = $this->cmp; 27 | if($v === null) { 28 | return; 29 | } else if($this->max === null) { 30 | $this->max = $v; 31 | } else if($c($v, $this->max) > 0) { 32 | $this->max = $v; 33 | } 34 | } 35 | 36 | public function compensate($v) { 37 | return; 38 | } 39 | 40 | public function emit() { 41 | return $this->max; 42 | } 43 | } -------------------------------------------------------------------------------- /src/React/EEP/Stats/Mean.php: -------------------------------------------------------------------------------- 1 | count = $this->diff = $this->mean = 0; 18 | } 19 | 20 | public function accumulate($v) { 21 | $this->count += 1; 22 | $this->diff = $v - $this->mean; 23 | $this->mean += ($this->diff/$this->count); 24 | } 25 | 26 | public function compensate($v) { 27 | $this->count -= 1; 28 | $this->diff = $this->mean - $v; 29 | $this->mean += ($this->diff/$this->count); 30 | } 31 | 32 | public function emit() { 33 | return $this->mean; 34 | } 35 | } -------------------------------------------------------------------------------- /src/React/EEP/Stats/Min.php: -------------------------------------------------------------------------------- 1 | cmp = $compare_fn ? $compare_fn : function($a, $b) { 17 | return $a == $b ? 0 : ($a < $b ? -1 : 1); 18 | }; 19 | } 20 | 21 | public function init() { 22 | $this->min = null; 23 | } 24 | 25 | public function accumulate($v) { 26 | $c = $this->cmp; 27 | if($v === null) { 28 | return; 29 | } else if($this->min === null) { 30 | $this->min = $v; 31 | } else if($c($v, $this->min) < 0) { 32 | $this->min = $v; 33 | } 34 | } 35 | 36 | public function compensate($v) { 37 | return; 38 | } 39 | 40 | public function emit() { 41 | return $this->min; 42 | } 43 | 44 | private function default_comparator($a, $b) { 45 | return $a < $b; 46 | } 47 | } -------------------------------------------------------------------------------- /src/React/EEP/Stats/Stdev.php: -------------------------------------------------------------------------------- 1 | total = 0; 16 | } 17 | 18 | public function accumulate($v) { 19 | $this->total += $v; 20 | } 21 | 22 | public function compensate($v) { 23 | $this->total -= $v; 24 | } 25 | 26 | public function emit() { 27 | return $this->total; 28 | } 29 | } -------------------------------------------------------------------------------- /src/React/EEP/Stats/Variance.php: -------------------------------------------------------------------------------- 1 | m_n = 0; 18 | } 19 | 20 | public function accumulate($v) { 21 | $this->m_n += 1; 22 | 23 | if($this->m_n == 1) { 24 | $this->old_m = $this->new_m = $v; 25 | $this->old_s = 0; 26 | } else { 27 | $this->new_m = $this->old_m + ($v - $this->old_m)/$this->m_n; 28 | $this->new_s = $this->old_s + ($v - $this->old_m)*($v - $this->new_m); 29 | 30 | $this->old_m = $this->new_m; 31 | $this->old_s = $this->new_s; 32 | } 33 | } 34 | 35 | public function compensate($v) { 36 | $this->m_n -= 1; 37 | 38 | $this->new_m = $this->old_m + ($this->old_m - $v)/$this->m_n; 39 | $this->new_s = $this->old_s + ($this->old_m - $v)*($v - $this->new_m); 40 | 41 | $this->old_m = $this->new_m; 42 | $this->old_s = $this->new_s; 43 | } 44 | 45 | public function emit() { 46 | return ($this->m_n > 1) ? $this->new_s/($this->m_n - 1) : 0; 47 | } 48 | } -------------------------------------------------------------------------------- /src/React/EEP/Util/Noop.php: -------------------------------------------------------------------------------- 1 | inner = $fn; 17 | $this->instant = $instant; 18 | } 19 | 20 | public function init() { 21 | $this->inner->init(); 22 | // Slight difference to EEP.js here. Not triggering an emit here 23 | // as this value will never make it anywhere, it could confuse 24 | // emit-sensitive functions (mainly me, during testing). 25 | $this->value = new \React\EEP\Event\Temporal(null,$this->instant); 26 | } 27 | 28 | public function accumulate($v) { 29 | $this->value->value = $this->inner->accumulate($v->value); 30 | } 31 | 32 | public function compensate($_) { } 33 | 34 | public function emit() { 35 | return new \React\EEP\Event\Temporal($this->inner->emit(), $this->instant); 36 | } 37 | 38 | public function at() { 39 | return $this->value->at; 40 | } 41 | 42 | public function update($time) { 43 | $this->instant = $time; 44 | } 45 | } -------------------------------------------------------------------------------- /src/React/EEP/Window.php: -------------------------------------------------------------------------------- 1 | clock = $clock; 21 | $this->aggregator = new \React\EEP\Util\Temporal( $aggregator, 22 | $this->clock->init()); 23 | $this->aggregator->init(); 24 | } 25 | 26 | public function enqueue($event) { 27 | $this->aggregator->accumulate(new Temporal($event, $this->clock->inc())); 28 | } 29 | 30 | public function tick() { 31 | // If time hasn't passed, we're done 32 | if (!$this->clock->tick()) { 33 | return; 34 | } 35 | 36 | // Otherwise, emit if an interval worth of time has passed 37 | if ($this->clock->tock($this->aggregator->at())) { 38 | $this->emit('emit', array($this->aggregator->emit()->value)); 39 | // this updates the at value of the aggregator for the next window 40 | $this->aggregator->update($this->clock->at()); 41 | // 'close' current time window and 'open' a fresh one 42 | $this->aggregator->init(); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /src/React/EEP/Window/Periodic.php: -------------------------------------------------------------------------------- 1 | size = $size; 15 | $this->aggregator = $aggregator; 16 | $this->aggregator->init(); 17 | $this->index = 0; 18 | $this->mark = $size - 1; 19 | $this->buffer = new \SPLFixedArray($size); 20 | } 21 | 22 | public function enqueue($event) { 23 | $this->aggregator->accumulate($event); 24 | if($this->index >= $this->mark) { 25 | $po = ($this->index + 1) % $this->size; 26 | $this->emit('emit', array($this->aggregator->emit())); 27 | $this->aggregator->compensate($this->buffer[$po]); 28 | } 29 | $this->buffer[$this->index % $this->size] = $event; 30 | $this->index++; 31 | } 32 | } -------------------------------------------------------------------------------- /src/React/EEP/Window/Tumbling.php: -------------------------------------------------------------------------------- 1 | size = $size; 15 | $this->aggregator = $aggregator; 16 | $this->aggregator->init(); 17 | $this->index = 0; 18 | } 19 | 20 | public function enqueue($event) { 21 | $this->aggregator->accumulate($event); 22 | $this->index += 1; 23 | if ($this->index == $this->size) { 24 | $this->emit('emit', array($this->aggregator->emit())); 25 | $this->aggregator->init(); 26 | $this->index = 0; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/React/EEP/Clock/CountingTest.php: -------------------------------------------------------------------------------- 1 | init(); 11 | $t = $clock->at(); 12 | $clock->inc(); 13 | $this->assertTrue($clock->tick(), "Clock has moved but did not tick"); 14 | $this->assertGreaterThan($t, $clock->at()); 15 | } 16 | 17 | /** @test */ 18 | public function clockShouldTockEachTime() { 19 | $clock = new Counting(); 20 | $clock->init(); 21 | $this->assertFalse($clock->tick(), "Tick without time moving"); 22 | $this->assertFalse($clock->tock(1), "Tock without time moving"); 23 | $clock->inc(); 24 | $this->assertTrue($clock->tick(), "No tick, even though time inc'd"); 25 | $this->assertFalse($clock->tock(2), "Tock without time moving"); 26 | $clock->inc(); 27 | $this->assertTrue($clock->tock(1), "No tock!"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/React/EEP/Clock/WallTest.php: -------------------------------------------------------------------------------- 1 | init(); 11 | $t = $clock->at(); 12 | usleep(3000); // Sleep for a milli, a milli, a milli 13 | $clock->inc(); 14 | $this->assertTrue($clock->tick(), "Clock has moved but did not tick"); 15 | $this->assertGreaterThan($t, $clock->at()); 16 | } 17 | 18 | /** @test */ 19 | public function clockShouldTockEachTime() { 20 | $clock = new Wall(1); 21 | $clock->init(); 22 | $to = intval(microtime(true) * 1000); 23 | $this->assertFalse($clock->tick(), "Tick without time moving"); 24 | $this->assertFalse($clock->tock($to), "Tock without time moving"); 25 | usleep(2000); 26 | $clock->inc(); 27 | $this->assertTrue($clock->tick(), "No tick, even though time inc'd"); 28 | $to += 4; 29 | $this->assertFalse($clock->tock($to), "Tock without time moving"); 30 | usleep(2000); 31 | $clock->inc(); 32 | $this->assertTrue($clock->tock($to), "No tock!"); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/React/EEP/Stats/AllTest.php: -------------------------------------------------------------------------------- 1 | init(); 11 | $all->accumulate(1); 12 | $all->accumulate(10); 13 | $all->accumulate(100); 14 | $this->assertEquals(100, $all->emit()['max']); 15 | } 16 | 17 | /** @test */ 18 | public function compensateRemovesNumber() { 19 | $all = new All(); 20 | $all->init(); 21 | $all->accumulate(1); 22 | $all->accumulate(1); 23 | $all->accumulate(1); 24 | $all->accumulate(5); 25 | $all->compensate(5); 26 | $this->assertEquals(0, $all->emit()['vars']); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/React/EEP/Stats/CountTest.php: -------------------------------------------------------------------------------- 1 | init(); 11 | $count->accumulate(1); 12 | $count->accumulate(1); 13 | $this->assertEquals(2, $count->emit()); 14 | } 15 | 16 | /** @test */ 17 | public function compensateRemovesNumber() { 18 | $count = new Count(); 19 | $count->init(); 20 | $count->accumulate(1); 21 | $count->accumulate(1); 22 | $count->compensate(1); 23 | $this->assertEquals(1, $count->emit()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/React/EEP/Stats/MaxTest.php: -------------------------------------------------------------------------------- 1 | init(); 11 | $max->accumulate(1); 12 | $max->accumulate(10); 13 | $max->accumulate(100); 14 | $this->assertEquals(100, $max->emit()); 15 | $max->accumulate(-10); 16 | $max->accumulate(150); 17 | $this->assertEquals(150, $max->emit()); 18 | $max->init(); 19 | $max->accumulate(10); 20 | $this->assertEquals(10, $max->emit()); 21 | } 22 | 23 | /** 24 | * @test 25 | * 26 | * So this test is skipped, because compensate doesn't work. Need to 27 | * think up a way of doing this without effective keeping the whole 28 | * window in memory. 29 | */ 30 | public function compensateRemovesNumber() { 31 | $this->markTestIncomplete(); return; 32 | $max = new Max(); 33 | $max->init(); 34 | $max->accumulate(1); 35 | $max->accumulate(5); 36 | $max->compensate(5); 37 | $this->assertEquals(1, $max->emit()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/React/EEP/Stats/MeanTest.php: -------------------------------------------------------------------------------- 1 | init(); 11 | $mean->accumulate(4); 12 | $mean->accumulate(8); 13 | $this->assertEquals(6, $mean->emit()); 14 | } 15 | 16 | /** @test */ 17 | public function compensateRemovesNumber() { 18 | $mean = new Mean(); 19 | $mean->init(); 20 | $mean->accumulate(4); 21 | $mean->accumulate(8); 22 | $mean->compensate(4); 23 | $this->assertEquals(8, $mean->emit()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/React/EEP/Stats/MinTest.php: -------------------------------------------------------------------------------- 1 | init(); 11 | $min->accumulate(1); 12 | $min->accumulate(10); 13 | $min->accumulate(100); 14 | $this->assertEquals(1, $min->emit()); 15 | $min->accumulate(-10); 16 | $this->assertEquals(-10, $min->emit()); 17 | } 18 | 19 | /** 20 | * @test 21 | * 22 | * So this test is skipped, because compensate doesn't work. Need to 23 | * think up a way of doing this without effective keeping the whole 24 | * window in memory. 25 | */ 26 | public function compensateRemovesNumber() { 27 | $this->markTestIncomplete(); return; 28 | $min = new Min(); 29 | $min->init(); 30 | $min->accumulate(1); 31 | $min->accumulate(5); 32 | $min->compensate(1); 33 | $this->assertEquals(5, $min->emit()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/React/EEP/Stats/StdevTest.php: -------------------------------------------------------------------------------- 1 | init(); 11 | $stdev->accumulate(1); 12 | $stdev->accumulate(1); 13 | $this->assertEquals(0, $stdev->emit(), "", 0.01); 14 | $stdev->init(); 15 | $stdev->accumulate(5); 16 | $stdev->accumulate(7); 17 | $stdev->accumulate(9); 18 | $this->assertEquals(2, $stdev->emit(), "", 0.01); 19 | } 20 | 21 | /** @test */ 22 | public function compensateRemovesNumber() { 23 | $stdev = new Stdev(); 24 | $stdev->init(); 25 | $stdev->accumulate(5); 26 | $stdev->accumulate(7); 27 | $stdev->accumulate(9); 28 | $stdev->compensate(5); 29 | $this->assertEquals(1.41421, $stdev->emit(), "", 0.01); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/React/EEP/Stats/SumTest.php: -------------------------------------------------------------------------------- 1 | init(); 11 | $sum->accumulate(4); 12 | $sum->accumulate(8); 13 | $sum->accumulate(2); 14 | $this->assertEquals(14, $sum->emit()); 15 | } 16 | 17 | /** @test */ 18 | public function compensateRemovesNumber() { 19 | $sum = new Sum(); 20 | $sum->init(); 21 | $sum->accumulate(4); 22 | $sum->accumulate(8); 23 | $sum->compensate(4); 24 | $this->assertEquals(8, $sum->emit()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/React/EEP/Stats/VarianceTest.php: -------------------------------------------------------------------------------- 1 | init(); 11 | $vars->accumulate(1); 12 | $vars->accumulate(1); 13 | $this->assertEquals(0, $vars->emit(), "", 0.01); 14 | $vars->init(); 15 | $vars->accumulate(5); 16 | $vars->accumulate(7); 17 | $vars->accumulate(9); 18 | $this->assertEquals(4, $vars->emit(), "", 0.01); 19 | } 20 | 21 | /** @test */ 22 | public function compensateRemovesNumber() { 23 | $vars = new Variance(); 24 | $vars->init(); 25 | $vars->accumulate(5); 26 | $vars->accumulate(7); 27 | $vars->accumulate(9); 28 | $vars->compensate(5); 29 | $this->assertEquals(2, $vars->emit(), "", 0.01); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/React/EEP/TestFn.php: -------------------------------------------------------------------------------- 1 | test = $test; 11 | $this->data = null; 12 | $this->has_emit = 0; 13 | } 14 | public function init() { 15 | $this->data = array(); 16 | } 17 | public function accumulate($v) { 18 | if($this->data === null) { 19 | $this->test->assertTrue(false, "Aggregator not initialised"); 20 | } 21 | $this->data[] = $v; 22 | } 23 | public function compensate($v) { 24 | unset($this->data[array_search($v, $this->data)]); 25 | } 26 | public function emit() { 27 | $this->has_emit++; 28 | return $this->data; 29 | } 30 | public function hasEmitted() { 31 | return $this->has_emit; 32 | } 33 | } -------------------------------------------------------------------------------- /tests/React/EEP/Window/MonotonicTest.php: -------------------------------------------------------------------------------- 1 | init(); 15 | $fn = new TestFn($this); 16 | $win = new Monotonic($fn, $clock); 17 | $win->on("emit", function($data) use(&$test_data) { 18 | $this->assertEquals(count($data), count($test_data), 19 | "Wrong count on emit"); 20 | $this->assertEquals($data[0], $test_data[0], "Wrong data"); 21 | }); 22 | $win->enqueue($test_data[0]); 23 | $win->enqueue($test_data[1]); 24 | $win->enqueue($test_data[2]); 25 | $win->tick(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/React/EEP/Window/PeriodicTest.php: -------------------------------------------------------------------------------- 1 | assertEquals(0, $fn->hasEmitted()); 16 | $win = new Periodic($fn, 0); 17 | $win->on("emit", function($data) use($size) { 18 | $this->assertEquals(count($data), $size, "Wrong count on emit"); 19 | }); 20 | $this->assertEquals(0, $fn->hasEmitted()); 21 | 22 | // Time Window 23 | $win->enqueue($test_data[0]); 24 | $win->enqueue($test_data[1]); 25 | $win->enqueue($test_data[2]); 26 | $win->tick(); 27 | 28 | // Time window 2. 29 | $win->enqueue($test_data[3]); 30 | $win->enqueue($test_data[4]); 31 | $win->enqueue($test_data[5]); 32 | $win->tick(); 33 | 34 | $this->assertEquals(2, $fn->hasEmitted(), "Failed to tick"); 35 | } 36 | 37 | /** @test */ 38 | public function timeKeepsOnSlipping() { 39 | $test_data = array(0, 1, 2, 4, 5, 6); 40 | $size = 3; 41 | $fn = new TestFn($this); 42 | $win = new Periodic($fn, 2); // 2 millisecond window 43 | $win->on("emit", function($data) use($size) { 44 | //$this->assertEquals(count($data), $size, "Wrong count on emit"); 45 | }); 46 | 47 | // Time Window 48 | $win->enqueue($test_data[0]); 49 | $win->tick(); 50 | $win->enqueue($test_data[1]); 51 | $win->tick(); 52 | 53 | // Ensure the clock has advanced enough we'll be in a new period. 54 | usleep(2100); 55 | $win->enqueue($test_data[2]); 56 | $win->tick(); // we should have a output now 57 | 58 | // Time window 2. We actually only update the clock in the enqueue. 59 | $win->enqueue($test_data[3]); 60 | $win->tick(); 61 | $win->enqueue($test_data[4]); 62 | $win->tick(); 63 | 64 | // Ensure the clock has advanced enough we'll be in a new period. 65 | usleep(2100); 66 | 67 | $win->enqueue($test_data[5]); 68 | $win->tick(); 69 | 70 | $this->assertEquals(2, $fn->hasEmitted(), "Failed to tick"); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /tests/React/EEP/Window/SlidingTest.php: -------------------------------------------------------------------------------- 1 | on("emit", function($data) use(&$test_data, $size) { 17 | $this->assertEquals(count($data), $size, 18 | "Wrong count on emit"); 19 | }); 20 | $win->enqueue($test_data[0]); 21 | $win->enqueue($test_data[1]); 22 | $win->enqueue($test_data[2]); 23 | $win->enqueue($test_data[3]); 24 | $win->enqueue($test_data[4]); 25 | $this->assertEquals(3, $fn->hasEmitted(), "Failed to slide"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/React/EEP/Window/TumblingTest.php: -------------------------------------------------------------------------------- 1 | on("emit", function($data) use($size) { 19 | $this->assertEquals(count($data), $size, "Wrong count on emit"); 20 | }); 21 | $win->enqueue($test_data[0]); 22 | $win->enqueue($test_data[1]); 23 | $win->enqueue($test_data[2]); 24 | $win->enqueue($test_data[3]); 25 | $win->enqueue($test_data[4]); 26 | $win->enqueue($test_data[5]); 27 | $this->assertEquals(2, $fn->hasEmitted(), "Failed to tumble"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | add('React\EEP', __DIR__); --------------------------------------------------------------------------------