$oneJob = bcdiv($end, $jobsP, 99); $res[] = bcdiv(1, $oneJob, 99); $jps = bcdiv(1, $oneJob); $print("Iteration: " . ($j + 1) . "; Jobs per second: {$jps}"); } $sum = 0; foreach ($res as $r) { $sum = bcadd($sum, $r, 99); } $avJps = bcdiv($sum, $tests); $print("Average jobs per second: {$avJps}", 2); if ($bestJps < $avJps) { $bestJps = $avJps; $bestThreadsNum = $threads; } if ($avJps < $bestJps || $avJps < $lastJps) { $regression++; } if ($regression >= 3) { break; } $lastJps = $avJps; // Increase number of threads $threads++; $pool->setMaxThreads($threads); } while (true); $pool->cleanup(); $print('', 3); $print("Best number of threads for your system: {$bestThreadsNum} ({$bestJps} jobs per second)"); $boost = bcdiv($bestJps, bcdiv($averageOneThreadJps, 100, 99), 2); $print("Performance boost in relation to a single thread: {$boost}%");
/** * 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()); }