/** * Pool, errors * * @param bool $debug * * @throws Exception */ function processPoolErrorable($debug = false) { $num = 12; $threads = 4; if ($debug) { echo '-----------------------', PHP_EOL, "Errorable thread pool test: ", Thread::$useForks ? 'Async' : 'Sync', PHP_EOL, '-----------------------', PHP_EOL; } $pool = new ThreadPool(__NAMESPACE__ . '\\TestThreadErrorable', $threads, null, null, $debug); $jobs = array(); $i = $j = 0; $left = $num; $maxI = ceil($num * 2.5); do { while ($left > 0 && $pool->hasWaiting()) { $arg = mt_rand(1000000, 200000000); $threadId = $pool->run($arg, $j); $this->assertTrue(!isset($jobs[$threadId]), "Thread #{$threadId} is not failed correctly"); $jobs[$threadId] = $arg; $left--; $j++; } if ($results = $pool->wait($failed)) { foreach ($results as $threadId => $res) { $num--; $this->assertTrue(isset($jobs[$threadId]), "Thread #{$threadId}"); $this->assertEquals($jobs[$threadId], $res, "Thread #{$threadId}"); unset($jobs[$threadId]); } } if ($failed) { foreach ($failed as $threadId => $errArray) { list($errCode, $errMsg) = $errArray; $this->assertTrue(isset($jobs[$threadId]), "Thread #{$threadId}"); $this->assertNotEmpty($errCode, 'Error code needed'); $this->assertTrue(is_int($errCode), 'Error code needed'); $this->assertNotEmpty($errMsg, 'Error message needed'); unset($jobs[$threadId]); $left++; } } $i++; } while ($num > 0 && $i < $maxI); $this->assertSame(0, $num, 'All jobs must be done'); $pool->cleanup(); $this->assertSame(0, $pool->getThreadsCount()); $this->assertEmpty($pool->getThreads()); $this->assertFalse($pool->hasWaiting()); }
foreach ($res as $r) { $sum = bcadd($sum, $r, 99); } $averageOneThreadJps = bcdiv($sum, $tests); $print("Average jobs per second: {$averageOneThreadJps}", 3); $thread->cleanup(); // Test pools $bestJps = 0; $bestThreadsNum = 0; $regression = 0; $lastJps = 0; $print($line); $print("Pool test; Jobs: {$jobsP}; Iterations: {$tests}"); $print($line, 2); $threads = $poolMin; $pool = new ThreadPool($threadClass, $threads); do { $print("Threads: {$threads}"); $print($line); $res = array(); for ($j = 0; $j < $tests; ++$j) { $start = microtime(true); $num = $jobsP; $i = 0; $maxI = ceil($jobsP * 1.5); do { while ($pool->hasWaiting()) { $data ? $pool->run($arg1, $arg2) : $pool->run(); } if ($results = $pool->wait()) { $num -= count($results);
foreach ($failed as $threadId => $err) { list($errorCode, $errorMessage) = $err; echo "error (thread {$threadId}): #{$errorCode} - {$errorMessage}", PHP_EOL; $left++; } } } while ($num > 0); // After work it's strongly recommended to clean // resources obviously to avoid leaks $pool->cleanup(); // ---------------------------------------------- $threads = 8; $jobs = range(1, 30); $jobs_num = count($jobs); echo PHP_EOL, "Example with pool of threads ({$threads}) and pool of jobs ({$jobs_num})", PHP_EOL; $pool = new ThreadPool('TestThreadReturnFirstArgument', $threads); $num = $jobs_num; // Number of tasks $left = $jobs_num; // Number of remaining tasks $started = array(); do { while ($left > 0 && $pool->hasWaiting()) { $task = array_shift($jobs); if (!($threadId = $pool->run($task))) { throw new Exception('Pool slots error'); } $started[$threadId] = $task; $left--; } if ($results = $pool->wait($failed)) {