Skip to content

Commit

Permalink
[HF]TimeProfiler
Browse files Browse the repository at this point in the history
  • Loading branch information
SparSio committed Oct 28, 2015
1 parent 59fd104 commit f866ba2
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/TimeProfiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace ETNA\FeatureContext;

use Behat\Behat\Context\BehatContext,
Behat\Behat\Exception\PendingException;

trait TimeProfiler
{
static private $max_time = 100;
static private $last_time = null;

/**
* @BeforeScenario
*/
public function beginTimeProfiler()
{
if (true === isset(self::$_parameters['maxTime']) && self::$_parameters['maxTime']) {
self::$max_time = self::$_parameters['maxTime'];
}
self::$last_time = round(microtime(true) * 1000);
}

/**
* @AfterScenario
*/
public function stopTimeProfiler($event)
{
$now = round(microtime(true) * 1000);
$diff = $now - self::$last_time;
if ( $diff > self::$max_time) {
echo "{$event->getScenario()->getFile()}:{$event->getScenario()->getLine()}\n";
throw new PendingException("Request too long {$diff}ms > " . self::$max_time . "ms \n");
}
}
}

0 comments on commit f866ba2

Please sign in to comment.