/** * @test */ public function histogramsShouldIncrementAtomically() { $start = microtime(true); $promises = [$this->client->getAsync('/examples/some_histogram.php?c=0&adapter=' . $this->adapter), $this->client->getAsync('/examples/some_histogram.php?c=1&adapter=' . $this->adapter), $this->client->getAsync('/examples/some_histogram.php?c=2&adapter=' . $this->adapter), $this->client->getAsync('/examples/some_histogram.php?c=3&adapter=' . $this->adapter), $this->client->getAsync('/examples/some_histogram.php?c=4&adapter=' . $this->adapter), $this->client->getAsync('/examples/some_histogram.php?c=5&adapter=' . $this->adapter), $this->client->getAsync('/examples/some_histogram.php?c=6&adapter=' . $this->adapter), $this->client->getAsync('/examples/some_histogram.php?c=7&adapter=' . $this->adapter), $this->client->getAsync('/examples/some_histogram.php?c=8&adapter=' . $this->adapter), $this->client->getAsync('/examples/some_histogram.php?c=9&adapter=' . $this->adapter)]; Promise\settle($promises)->wait(); $end = microtime(true); echo "\ntime: " . ($end - $start) . "\n"; $metricsResult = $this->client->get('/examples/metrics.php?adapter=' . $this->adapter); $body = (string) $metricsResult->getBody(); $this->assertThat($body, $this->stringContains(<<<EOF test_some_histogram_bucket{type="blue",le="0.1"} 1 test_some_histogram_bucket{type="blue",le="1"} 2 test_some_histogram_bucket{type="blue",le="2"} 3 test_some_histogram_bucket{type="blue",le="3.5"} 4 test_some_histogram_bucket{type="blue",le="4"} 5 test_some_histogram_bucket{type="blue",le="5"} 6 test_some_histogram_bucket{type="blue",le="6"} 7 test_some_histogram_bucket{type="blue",le="7"} 8 test_some_histogram_bucket{type="blue",le="8"} 9 test_some_histogram_bucket{type="blue",le="9"} 10 test_some_histogram_bucket{type="blue",le="+Inf"} 10 test_some_histogram_count{type="blue"} 10 test_some_histogram_sum{type="blue"} 45 EOF )); }
function are_available($urls, $timeout = 10) { $client = new Client(['timeout' => $timeout, 'connect_timeout' => $timeout]); $promises = array_map(function ($url) use($client) { return $client->getAsync($url); }, $urls); try { $results = Promise\settle($promises)->wait(); return array_map(function ($result) { return $result['value']->getStatusCode() === 200; }, $results); } catch (Exception $e) { return array_map(function ($url) { return false; }, $urls); } }
/** * Get loading time information for two websites * Info: Requests are concurrent - http://docs.guzzlephp.org/en/latest/quickstart.html#concurrent-requests * * @param $comparingUri * @param $competitorUri * * @return array */ public function watchConcurrent($comparingUri, $competitorUri) { $comparingStats = null; $comparingRequest = $this->client->getAsync($comparingUri, [RequestOptions::TIMEOUT => $this->timeout, RequestOptions::ON_STATS => function (TransferStats $stats) use(&$comparingStats) { $comparingStats = $stats; }]); $competitorStats = null; $competitorRequest = $this->client->getAsync($competitorUri, [RequestOptions::TIMEOUT => $this->timeout, RequestOptions::ON_STATS => function (TransferStats $stats) use(&$competitorStats) { $competitorStats = $stats; }]); Promise\settle([$comparingRequest, $competitorRequest])->wait(); return [$comparingStats, $competitorStats]; }