Skip to content

Instantly share code, notes, and snippets.

@baldurrensch
Created December 19, 2012 17:53
Show Gist options
  • Save baldurrensch/4338775 to your computer and use it in GitHub Desktop.
Save baldurrensch/4338775 to your computer and use it in GitHub Desktop.
A PHP Unit Listener that records individual test time runs and can print out warnings if a test is slower then some critical threshold
<?xml version="1.0" encoding="UTF-8"?>
<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "bootstrap.php.cache" >
<!--
To enable logging of test execution times, uncomment the following block in your phpunit.xml file.
The first argument is the number of seconds (0 = off) that a test can run (threshold), before a warning is printed
The second parameter is optional and specifies the path to the log file. Defaults to ./app/logs/test_{timestamp}.csv
-->
<!--
<listeners>
<listener class="Hautelook\ApiBundle\Tests\TimeReportingTestListener" file="TimeReportingTestListener.php">
<arguments>
<integer>60</integer>
<string></string>
</arguments>
</listener>
</listeners>
-->
<groups>
<exclude>
<group>slow</group>
</exclude>
</groups>
</phpunit>
<?php
namespace Hautelook\ApiBundle\Tests;
use PHPUnit_Framework_TestListener;
use PHPUnit_Framework_Test;
use PHPUnit_Framework_TestSuite;
use PHPUnit_Framework_AssertionFailedError;
use Exception;
/**
* @author Baldur Rensch <baldur.rensch@hautelook.com>
*/
class TimeReportingTestListener implements PHPUnit_Framework_TestListener
{
private $logFile;
private $timeThreshold;
public function __construct($timeThreshold, $filename = "")
{
$this->timeThreshold = $timeThreshold;
if (empty($filename)) {
$this->logFile = dirname(__FILE__) . "/../app/logs/test_" . time() . ".csv";
} else {
$this->logFile = $filename;
}
}
public function endTest(PHPUnit_Framework_Test $test, $time)
{
if ($this->timeThreshold && $this->timeThreshold < $time) {
echo "\n !!! Testing time (" . $time . ") exceeded for test: " . $test->getName() . "\n";
}
$data = $test->getName() . ", " . $time . "\n";
file_put_contents($this->logFile, $data, FILE_APPEND);
}
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
{
}
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
public function startTest(PHPUnit_Framework_Test $test)
{
}
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
{
}
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment