Skip to content

Commit

Permalink
Merge pull request #42 from gdubost/v3
Browse files Browse the repository at this point in the history
Amélioration de la précision du time profiler
  • Loading branch information
Guillaume DUBOST authored May 15, 2019
2 parents b22fa65 + 759eddc commit 630e0ca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
7 changes: 7 additions & 0 deletions src/ApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,15 @@ public function jeFaisUneRequetteHTTPAvecDuJSON($method, $url, $body)
$request->cookies->add($this->request["cookies"]);
$request->files->add($this->request["files"]);

$time_profiler = self::$contexts['ETNA\FeatureContext\TimeProfilerContext'];

isset($time_profiler) && $time_profiler->start();
$response = $this->getKernel()->handle($request, HttpKernelInterface::MASTER_REQUEST, true);

if (!isset(self::$contexts['ETNA\FeatureContext\CoverageContext']) && isset($time_profiler)) {
$time_profiler->stopTimeProfiler();
}

$result = [
"http_code" => $response->getStatusCode(),
"http_message" => Response::$statusTexts[$response->getStatusCode()],
Expand Down
39 changes: 20 additions & 19 deletions src/TimeProfilerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,38 @@

class TimeProfilerContext implements Context
{
static private $max_time = 100;
static private $last_time = null;
private $max_time = 100;
private $start = 0.0;
private $end = 0.0;
private $failure = null;

public function __construct($max_time)
{
self::$max_time = $max_time;
$this->max_time = $max_time;
}

/**
* @BeforeScenario
*/
public function beginTimeProfiler()
public function start()
{
self::$last_time = round(microtime(true) * 1000);
$this->failure = null;
$this->start = microtime(true);
}

/**
* @AfterScenario
*/
public function stopTimeProfiler(AfterScenarioScope $scope)
public function stopTimeProfiler()
{
$has_coverage = $scope->getEnvironment()->hasContextClass("ETNA\FeatureContext\CoverageContext");
if (true === $has_coverage) {
return;
$diff = round((microtime(true) - $this->start) * 1000);
if ($diff > $this->max_time) {
$this->failure = $diff;
}
}

$now = round(microtime(true) * 1000);
$diff = $now - self::$last_time;
if ($diff > self::$max_time) {
/**
* @afterScenario
*/
public function afterScenarioCheck(AfterScenarioScope $scope)
{
if (null !== $this->failure) {
echo "{$scope->getFeature()->getFile()}:{$scope->getScenario()->getLine()}\n";
throw new PendingException("Request too long {$diff}ms > " . self::$max_time . "ms \n");
throw new PendingException("Request too long {$this->failure}ms > {$this->max_time}ms\n");
}
}
}

0 comments on commit 630e0ca

Please sign in to comment.