/** * @dataProvider getStorage * * @param \Closure $getStorage */ public function test_nestedParallelWithStorage(\Closure $getStorage) { $Parallel = new Parallel($getStorage()); $time = microtime(true); for ($i = 1; $i <= 2; ++$i) { $Parallel->run('n:' . $i, function () use($getStorage) { $Parallel = new Parallel($getStorage()); $Parallel->run('n:1', function () { sleep(2); return 'foo'; }); $Parallel->run('n:2', function () { sleep(2); return 'bar'; }); // wait all return $Parallel->wait(); }); } sleep(2); $result = $Parallel->wait(['n:1', 'n:2']); $time = microtime(true) - $time; $this->assertGreaterThanOrEqual(2, $time); $this->assertLessThan(3, $time); $this->assertSame(['n:1' => ['n:1' => 'foo', 'n:2' => 'bar'], 'n:2' => ['n:1' => 'foo', 'n:2' => 'bar']], $result); }
public function test_parallel() { $MC = $this->getMemcached(); $MC->flush(); unset($MC); $Storage = new MemcachedStorage(['servers' => [explode(':', MEMCACHED_TEST_SERVER)]]); $Parallel = new Parallel($Storage); $start = microtime(true) + 2; for ($i = 1; $i <= 5; $i++) { $Parallel->run('foo' . $i, function () use($start) { $MemcachedTags = new MemcachedTags($MC = $this->getMemcached()); while (microtime(true) < $start) { // wait for start } for ($i = 1; $i <= 1000; ++$i) { $j = mt_rand(1, 100); $MemcachedTags->setKeyWithTags('key_1_' . $j, $j, 'tag1'); $MemcachedTags->setKeysWithTags(['key_2_' . $j => $j, 'key_3_' . $j => $j], ['tag2', 'tag3']); } return 1; }); } $MemcachedTags = new MemcachedTags($MC = $this->getMemcached()); while (microtime(true) < $start) { // wait for start } for ($i = 1; $i <= 1000; ++$i) { $MemcachedTags->deleteKeysByTag('tag1'); $MemcachedTags->deleteKeysByTags(['tag2', 'tag3']); } $Parallel->wait(['foo1', 'foo2', 'foo3', 'foo4', 'foo5']); $keys1 = $MemcachedTags->getKeysByTag('tag1'); $keys2 = $MemcachedTags->getKeysByTag('tag2'); $keys3 = $MemcachedTags->getKeysByTag('tag3'); for ($i = 1; $i <= 100; ++$i) { $value = $MC->get('key_1_' . $i); $this->assertSame($value ? true : false, in_array('key_1_' . $i, $keys1)); $value = $MC->get('key_2_' . $i); $this->assertSame($value ? true : false, in_array('key_2_' . $i, $keys2)); $value = $MC->get('key_3_' . $i); $this->assertSame($value ? true : false, in_array('key_3_' . $i, $keys3)); } $MC->flush(); }
public function test_parallel() { $MC = $this->getMemcached(); $MC->flush(); $this->assertSame(true, $MC->set('testcount', '1000000')); unset($MC); $Storage = new MemcachedStorage(['servers' => [explode(':', MEMCACHED_TEST_SERVER)]]); $Parallel = new Parallel($Storage); $start = microtime(true) + 2; // 1st operation $Parallel->run('foo', function () use($start) { $MemcachedLock = new MemcachedLock($MC = $this->getMemcached(), 'lock_'); while (microtime(true) < $start) { // wait for start } $c = 0; for ($i = 1; $i <= 10000; ++$i) { if ($MemcachedLock->acquire(2, 3)) { $count = (int) $MC->get('testcount'); ++$count; $MC->set('testcount', $count); $MemcachedLock->release(); ++$c; } } return $c; }); // 2st operation $Parallel->run('bar', function () use($start) { $MemcachedLock = new MemcachedLock($MC = $this->getMemcached(), 'lock_'); while (microtime(true) < $start) { // wait for start } $c = 0; for ($i = 1; $i <= 10000; ++$i) { if ($MemcachedLock->acquire(2, 3)) { $count = (int) $MC->get('testcount'); ++$count; $MC->set('testcount', $count); $MemcachedLock->release(); ++$c; } } return $c; }); $MemcachedLock = new MemcachedLock($MC = $this->getMemcached(), 'lock_'); while (microtime(true) < $start) { // wait for start } $c = 0; for ($i = 1; $i <= 10000; ++$i) { if ($MemcachedLock->acquire(2, 3)) { $count = (int) $MC->get('testcount'); ++$count; $MC->set('testcount', $count); $MemcachedLock->release(); ++$c; } } $result = $Parallel->wait(['foo', 'bar']); $this->assertSame(10000, (int) $result['foo']); $this->assertSame(10000, (int) $result['bar']); $this->assertSame(10000, $c); $this->assertSame(1030000, (int) $MC->get('testcount')); $MC->flush(); }
// You can use Parallel inside run function by creating new objects Parallel. // Example: $Parallel = new Parallel(new \Parallel\Storage\ApcuStorage()); sleep(2); return ['hello' => 'world']; }); // 2nd operation $Parallel->run('obj', function () { sleep(2); return (object) ['a' => 1, 'b' => 2, 'c' => 3]; }); // 3th operation // do some thing ... sleep(2); // waiting for <foo> and <obj> and get results. // use wait() without parameters for wait all forks. Example: $Parallel->wait(); $result = $Parallel->wait(['foo', 'obj']); print_r($result); print_r(microtime(true) - $time); // 3 parallel operations by 2 seconds take about 2 seconds, instead 6 seconds. // Array // ( // [foo] => Array // ( // [hello] => world // ) // // [obj] => stdClass Object // ( // [a] => 1 // [b] => 2 // [c] => 3