Skip to content

Instantly share code, notes, and snippets.

@gcatlin
Created January 21, 2013 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcatlin/4588170 to your computer and use it in GitHub Desktop.
Save gcatlin/4588170 to your computer and use it in GitHub Desktop.
Print slowest PHPUnit tests
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
printerClass="ProfilerPrinter"
printerFile="profiler.php"
>
</phpunit>
<?php
class ProfilerPrinter extends PHPUnit_TextUI_ResultPrinter
{
private $current_suite;
private $test_times = array();
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
{
parent::startTestSuite($suite);
$this->current_suite = $suite->getName();
}
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
{
parent::endTestSuite($suite);
$this->current_suite = null;
}
public function endTest(PHPUnit_Framework_Test $test, $time)
{
parent::endTest($test, $time);
$this->test_times[$this->current_suite . '::' . $test->getName()] = $time;
}
protected function printFooter(PHPUnit_Framework_TestResult $result)
{
parent::printFooter($result);
arsort($this->test_times);
$top_five = array_slice($this->test_times, 0, 5);
echo "\n";
echo "5 Slowest Tests:\n";
foreach ($top_five as $test => $time) {
printf("%0.3f\t%s\n", $time, $test);
}
}
}
@sebastianbergmann
Copy link

I'd rather write a script that parses that information from the XML logfile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment