Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

Commit

Permalink
Merge pull request #18 from tyx/feature/add-benchmark-context
Browse files Browse the repository at this point in the history
Add benchmark context
  • Loading branch information
stephpy committed Dec 29, 2014
2 parents 7f3ea9b + ee32562 commit d10abf6
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/Benchmark/BenchmarkContext.php
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');
}
}
}

0 comments on commit d10abf6

Please sign in to comment.