protected function execute(InputInterface $input, OutputInterface $output) { $config = $this->setConfiguraton($input); $client = new \GuzzleHttp\Client(['defaults' => ['allow_redirects' => false, 'timeout' => 5, 'connect_timeout' => 5]]); /** @var Instance $instance */ foreach ($config->getInstances() as $instance) { $requests[] = $client->createRequest('HEAD', $instance->getUrl()); } $options = []; Pool::send($client, $requests, ['complete' => function (CompleteEvent $event) { }, 'error' => function (ErrorEvent $event) use($config) { $instance = $config->findInstanceByUrl($event->getRequest()->getUrl()); if ($instance == null) { throw new \RuntimeException('Instance not found'); } if (!$event->getException()->hasResponse()) { $raven = new \Raven_Client($instance->getSentryDsn()); $event_id = $raven->getIdent($raven->captureMessage(sprintf('The website %s with url -> %s is down or has a problem', $instance->getName(), $event->getRequest()->getUrl()), [], \Raven_Client::FATAL)); if ($raven->getLastError() !== null) { printf('There was an error sending the event to Sentry: %s', $raven->getLastError()); } $error_handler = new \Raven_ErrorHandler($raven); $error_handler->registerExceptionHandler(); $error_handler->registerErrorHandler(); $error_handler->registerShutdownFunction(); } }]); }
$total = isset($_SERVER['REQUESTS']) ? $_SERVER['REQUESTS'] : 1000; $parallel = isset($_SERVER['PARALLEL']) ? $_SERVER['PARALLEL'] : 100; $client = new Client(['base_url' => Server::$url]); $t = microtime(true); for ($i = 0; $i < $total; $i++) { $client->get('/guzzle-server/perf'); } $totalTime = microtime(true) - $t; $perRequest = $totalTime / $total * 1000; printf("Serial: %f (%f ms / request) %d total\n", $totalTime, $perRequest, $total); // Create a generator used to yield batches of requests $reqs = function () use($client, $total) { for ($i = 0; $i < $total; $i++) { (yield $client->createRequest('GET', '/guzzle-server/perf')); } }; $t = microtime(true); Pool::send($client, $reqs(), ['parallel' => $parallel]); $totalTime = microtime(true) - $t; $perRequest = $totalTime / $total * 1000; printf("Batch: %f (%f ms / request) %d total with %d in parallel\n", $totalTime, $perRequest, $total, $parallel); $handler = new CurlMultiHandler(['max_handles' => $parallel]); $client = new Client(['handler' => $handler, 'base_url' => Server::$url]); $t = microtime(true); for ($i = 0; $i < $total; $i++) { $client->get('/guzzle-server/perf'); } unset($client); $totalTime = microtime(true) - $t; $perRequest = $totalTime / $total * 1000; printf("Future: %f (%f ms / request) %d total\n", $totalTime, $perRequest, $total);
/** * @deprecated Use {@see GuzzleHttp\Pool} instead. * @see GuzzleHttp\Pool */ public function sendAll($requests, array $options = array()) { Pool::send($this, $requests, $options); }
/** * Make concurrent requests using guzzle * * @todo What about responses???????? * * @param array $requests * @return */ protected function makeConcurrentRequests(array $requests, Closure $complete = null) { Pool::send($this->client, $requests, ['complete' => $complete]); }
public function testHasSendMethod() { $client = new Client(); $responses = [new Response(404)]; $history = new History(); $client->getEmitter()->attach($history); $client->getEmitter()->attach(new Mock($responses)); $requests = [$client->createRequest('GET', 'http://foo.com/baz')]; Pool::send($client, $requests); $this->assertCount(1, $history); }
$parser = new Crawler($event->getResponse()->getBody()->getContents()); $canto = $parser->filter("body > div.container-fluid > div:nth-child(2) > div.span3 > h2")->first(); $canto = strtolower(str_replace("Canto ", "", $canto->text())); $queue = $parser->filter("body > div.container-fluid > div:nth-child(2) > div.span3 > p:nth-child(3) > a")->each(function (Crawler $node, $i) { return $node->attr('href'); }); foreach ($queue as $estrofe) { $estrofes[] = $client->createRequest("GET", "{$base_url}{$canto}/{$estrofe}"); } }]); Pool::send($client, $estrofes, ['pool_size' => 40, 'complete' => function (CompleteEvent $event) use($base_output) { echo "\n\n"; echo "Acessado: " . $event->getRequest()->getUrl() . "\n"; $parser = new Crawler($event->getResponse()->getBody()->getContents()); $content = $parser->filter("body > div.container-fluid > div:nth-child(2) > div.span8 > div")->first(); echo "Encontrado: " . $content->text() . PHP_EOL; $canto = $parser->filter("body > div.container-fluid > div:nth-child(2) > div.span3 > h2")->first(); $canto = $parser->filter("body > div.container-fluid > div:nth-child(2) > div.span3 > h2")->first(); $estrofe = $parser->filter("body > div.container-fluid > div:nth-child(2) > div.span8 > h3")->first(); $canto = str_replace("Canto ", "", $canto->text()); $estrofe = $estrofe->text(); $output_path = "{$base_output}{$canto}" . "_{$estrofe}.txt"; echo "Salvo em: {$output_path}\n"; file_put_contents($output_path, $content->text()); }, 'error' => function (ErrorEvent $event) { echo "erro"; }]); $end_time = microtime(true); $execution_time = $end_time - $start_time; echo "\n\n\n"; echo "Tempo de execução total: {$execution_time}\n";