Example #1
0
 /**
  * @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();
 }
Example #4
0
/**
 * This file is part of Parallel.
 * git: https://github.com/cheprasov/php-parallel
 *
 * (C) Alexander Cheprasov <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require dirname(__DIR__) . '/vendor/autoload.php';
use Parallel\Parallel;
use Parallel\Storage\ApcuStorage;
// EXAMPLE, how to run parallel 3 operations.
// Using Parallel via ApcuStorage (APCu, see http://php.net/manual/ru/book.apcu.php)
$Parallel = new Parallel(new ApcuStorage());
// if you have not APCu, you can use Memcached or Redis as Storage.
// Note: you can't store objects in Memcached or Redis and you can't store binary strings (use <base64> functions)
//    $Parallel = new Parallel(new \Parallel\Storage\MemcachedStorage([
//        'servers' => [['127.0.0.1', 11211]]
//    ]));
//    $Parallel = new Parallel(new \Parallel\Storage\RedisStorage([
//        'server' => 'tcp://127.0.0.1:6379'
//    ]));
$time = microtime(true);
// 1st operation
$Parallel->run('foo', function () {
    // 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'];