<?php /** * Copyright 2010-12 Nickolas Whiting. All rights reserved. * Use of this source code is governed by the Apache 2 license * that can be found in the LICENSE file. */ require_once dirname(realpath(__FILE__)) . '/../__init__.php'; xp_import('unittest'); unittest\test(function ($test) { $foo = XP_SIG('foo'); xp_signal($foo, function () { }); xp_before($foo, function ($foo) { $foo->foo = 'foo'; }); xp_emit($foo); $test->equal($foo->foo, 'foo'); }, 'API Before function');
<?php ini_set('memory_limit', '10G'); xp_set_signal_history(true); $sig = XP_SIG('foo'); $time = time(); // Emit a few foo objects for ($i = 0; $i < 10000000; $i++) { xp_emit($sig); } $end = time(); $emitted = 0; foreach (xp_signal_history() as $_node) { if ($_node[0]->compare($sig)) { $emitted++; } } echo 'Emitted ' . $emitted . ' signals in ' . ($end - $time) . ' seconds' . PHP_EOL; echo 'Rate of ' . $emitted / ($end - $time);
<?php /** * Copyright 2010-12 Nickolas Whiting. All rights reserved. * Use of this source code is governed by the Apache 2 license * that can be found in the LICENSE file. */ require_once dirname(realpath(__FILE__)) . '/../__init__.php'; xp_import('unittest'); /** * Emit Unitest */ unittest\test(function ($test) { $foo = XP_SIG(spl_object_hash(new stdClass())); $process = xp_process(null); xp_signal($foo, $process); xp_delete_process($foo, $process); $test->count(xp_get_signal($foo), 0); }, 'API Delete Process');
} xp_emit(XP_SIG($read)); }); // Once a bride, groom and bell signals are emitted we emit the wedding. $wedding = xp_complex_sig(function ($signal) { if (!isset($this->reset) || $this->reset) { $this->reset = false; $this->bride = false; $this->groom = false; $this->bells = false; } switch (true) { case $signal->compare(XP_SIG('groom')): $this->groom = true; break; case $signal->compare(XP_SIG('bride')): $this->bride = true; break; case $signal->compare(XP_SIG('bells')): $this->bells = true; break; } if ($this->groom && $this->bride && $this->bells) { $this->reset = true; return true; } return false; }); xp_signal($wedding, function () { echo 'A wedding just happened.'; });
xp_import('network'); $ops = 0; $thread = xp_threaded_process(function () { $start_time = 0; $this->operations = 0; $sig = XP_SIG('a'); do { echo $ops; xp_emit($sig); $this->operations++; $ops = $this->operations / (microtime(true) - $start_time); } while (true); }); $ops_sig = xp_signal(XP_SIG('op'), xp_exhaust(null, $thread)); xp_emit(XP_SIG('op')); // $server = network\connect('0.0.0.0', ['port'=>8000]); // $server->on_connect(xp_null_exhaust(function($server) use ($ops){ // $server->socket->write($ops); // $server->socket->disconnect(); // })); // $server = // $iterations = 1000000; // $start = microtime(true); // for ($i=0;$i<$iterations;$i++) {} // $end = microtime(true); // $loop_time = $end - $start; // $sig = XP_SIG(null); // $start = microtime(true); // for ($i=0;$i<$iterations;$i++) { // xp_emit($sig);
<?php // Handle Foo xp_signal(XP_SIG('foo'), function ($signal) { echo $signal->bar; }); // When foo is emitted insert bar into the event xp_before(XP_SIG('foo'), function ($signal) { echo "I RAN" . PHP_EOL; $signal->bar = 'foo'; }); xp_before(XP_SIG('foo'), xp_high_priority(function ($signal) { echo "I RAN BEFORE" . PHP_EOL; })); // After foo is emitted unset bar in the event xp_after(XP_SIG('foo'), function ($signal) { unset($signal->bar); }); $signal = xp_emit(XP_SIG('foo')); var_dump(isset($signal->bar));
$tests = ['Installation SIG Process' => function ($processor, $i) { $processor->signal($i, xp_null_exhaust(null)); }, 'Emitting a signal' => function ($processor, $i) { $processor->emit($i); }, 'Registering a signal' => function ($processor, $i) { $processor->register_signal($i); }]; $output::send('Beginning performance tests ... please be patient.'); $results = []; $average_perform = 10; $total_tests = 0; foreach ($tests as $_test => $_func) { $results[$_test] = []; $processor = new \XPSPL\Processor(); $processor->signal_history(false); $sig = XP_SIG('a'); $processor->signal($sig, xp_null_exhaust(null)); for ($i = 1; $i < $average_perform + 1; $i++) { $output::send(sprintf('Running %s test %s of %s', $_test, $i, $average_perform)); for ($a = 1; $a < 1 << 10;) { $a = $a << 1; $tc = $a; if ($a === 1) { $setup = true; } else { $setup = false; } if (!isset($results[$_test][$tc])) { $results[$_test][$tc] = []; } for ($c = 0; $c < $tc; $c++) {
<?php $max_threads = 16 - 1; class My_Thread extends \XPSPL\Process { public function execute($signal, $thread) { while (true) { var_dump($thread); } } } for ($i = 0; $i < $max_threads; $i++) { xp_signal(XP_SIG('test' . $i), xp_threaded_process(new My_Thread())); } for ($i = 0; $i < $max_threads; $i++) { xp_emit(XP_SIG('test' . $i)); }