/** * @param IpnEntity $ipn * @return bool */ public function forwardIpn(IpnEntity $ipn) { $urls = $ipn->getForwardUrls(); if (!empty($urls)) { $requests = []; foreach ($urls as $url) { $request = $this->guzzle->createRequest('post', $url); $request->setHeader($this->customHeader, $this->getKey()); if (in_array($url, $this->disabledJsonFormatting)) { $request->getQuery()->merge($ipn->toArray()); } else { $request->setHeader('content-type', 'application/json'); if ($this->formatter) { $response = $this->formatter->formatJsonResponse($ipn); } else { $response = ['ipn' => $ipn->toArray()]; } $request->setBody(Stream::factory(json_encode($response))); } $requests[] = $request; } $this->guzzle->sendAll($requests, ['parallel' => $this->maxRequests]); return true; } return false; }
public function testCanSetCustomParallelAdapter() { $called = false; $pa = new FakeParallelAdapter(new MockAdapter(function () use(&$called) { $called = true; return new Response(203); })); $client = new Client(['parallel_adapter' => $pa]); $client->sendAll([$client->createRequest('GET', 'http://www.foo.com')]); $this->assertTrue($called); }
public function testSendsAllInParallel() { $client = new Client(); $client->getEmitter()->attach(new Mock([new Response(200), new Response(201), new Response(202)])); $history = new History(); $client->getEmitter()->attach($history); $requests = [$client->createRequest('GET', 'http://test.com'), $client->createRequest('POST', 'http://test.com'), $client->createRequest('PUT', 'http://test.com')]; $client->sendAll($requests); $requests = array_map(function ($r) { return $r->getMethod(); }, $history->getRequests()); $this->assertContains('GET', $requests); $this->assertContains('POST', $requests); $this->assertContains('PUT', $requests); }
* make perf * # With custom options * REQUESTS=100 PARALLEL=5000 make perf */ require __DIR__ . '/bootstrap.php'; use GuzzleHttp\Client; use GuzzleHttp\Tests\Server; // Wait until the server is responding Server::wait(); // Get custom make variables $total = isset($_SERVER['REQUESTS']) ? $_SERVER['REQUESTS'] : 1000; $parallel = isset($_SERVER['PARALLEL']) ? $_SERVER['PARALLEL'] : 25; $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 to sendAll $reqs = function () use($client, $total) { for ($i = 0; $i < $total; $i++) { (yield $client->createRequest('GET', '/guzzle-server/perf')); } }; $t = microtime(true); $client->sendAll($reqs(), ['parallel' => $parallel]); $totalTime = microtime(true) - $t; $perRequest = $totalTime / $total * 1000; printf("Parallel: %f (%f ms / request) %d total with %d in parallel\n", $totalTime, $perRequest, $total, $parallel);
<?php require __DIR__ . '/../vendor/autoload.php'; use GuzzleHttp\Client; use GuzzleHttp\Subscriber\Log\LogSubscriber; use GuzzleHttp\Subscriber\Log\Formatter; $httpClient = new Client(['base_url' => 'https://httpbin.org']); // Attach a subscriber for logging the request and response. $logger = new LogSubscriber(STDOUT, new Formatter(Formatter::DEBUG)); $httpClient->getEmitter()->attach($logger); // Create a generator that emits PUT requests. $createPutRequests = function ($limit, $start = 1) use($httpClient) { for ($i = $start; $i <= $limit; $i++) { (yield $httpClient->createRequest('PUT', 'put', ['json' => ['first_name' => 'John' . str_pad($i, 4, '0', STR_PAD_LEFT), 'last_name' => 'Doe', 'age' => rand(18, 60)]])); } }; // Execute a batch of requests. $httpClient->sendAll($createPutRequests(5));