This repository has been archived by the owner on Nov 29, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from tyx/feature/add-benchmark-context
Add benchmark context
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Rezzza\Tk1\Benchmark; | ||
|
||
use mageekguy\atoum\asserter; | ||
use Behat\Behat\Context\BehatContext; | ||
use Guzzle\Http\Client as HttpClient; | ||
|
||
class BenchmarkContext extends BehatContext | ||
{ | ||
private $httpClient; | ||
|
||
private $benchmark; | ||
|
||
private $asserter; | ||
|
||
private $baseUrl; | ||
|
||
public function __construct($baseUrl) | ||
{ | ||
$this->baseUrl = $baseUrl; | ||
$this->asserter = new asserter\generator; | ||
$this->httpClient = new HttpClient; | ||
} | ||
|
||
/** | ||
* @When /^I send (?P<nbRequest>\d+) (?P<method>[A-Z]+) request to "(?P<url>[^"]*)" with (?P<percentile>[\d\.]+) percentile$/ | ||
*/ | ||
public function iSendGetRequestToWithPercentile($nbRequest, $method, $url, $percentile) | ||
{ | ||
$url = $this->baseUrl.'/'.ltrim($url, '/'); | ||
$request = $this->httpClient->createRequest($method, $url); | ||
|
||
$this->benchmark = new Benchmark\HttpResponseTimeBenchmark($request, $nbRequest); | ||
$this->benchmark->start($this->httpClient, new Benchmark\HttpTimeDataCollector($percentile)); | ||
} | ||
|
||
/** | ||
* @Then /^print benchmark result$/ | ||
*/ | ||
public function printBenchmarkResult() | ||
{ | ||
$this->guardBenchmarkStarted(); | ||
$this->benchmark->printResult(); | ||
} | ||
|
||
/** | ||
* @Then /^response average time should be inferior to (?P<averageTimeRequired>\d+) ms with (?P<burstTolerancePerent>\d+)% burst tolerance$/ | ||
*/ | ||
public function responseAverageTimeShouldBeInferiorToMsWithBurstTolerance($averageTimeRequired, $burstTolerancePerent) | ||
{ | ||
$this->guardBenchmarkStarted(); | ||
$maxAverageTime = $averageTimeRequired * (1 + ($burstTolerancePerent / 100)); | ||
|
||
$this->asserter | ||
->boolean($this->benchmark->isAverageTimeLessThan($maxAverageTime)) | ||
->isTrue() | ||
; | ||
} | ||
|
||
private function guardBenchmarkStarted() | ||
{ | ||
if (null === $this->benchmark) { | ||
throw new \LogicException('You should start a benchmark to print its results'); | ||
} | ||
} | ||
} |