5 * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 * @subpackage Util_Log
39 * @author Benjamin Eberlei <kontakt@beberlei.de>
40 * @author Sebastian Bergmann <sebastian@phpunit.de>
41 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @link http://www.phpunit.de/
44 * @since File available since Release 3.5.0
48 * A TestListener that integrates with XHProf.
50 * Here is an example XML configuration for activating this listener:
54 * <listener class="PHPUnit_Util_Log_XHProf" file="PHPUnit/Util/Log/XHProf.php">
57 * <element key="xhprofLibFile">
58 * <string>/var/www/xhprof_lib/utils/xhprof_lib.php</string>
60 * <element key="xhprofRunsFile">
61 * <string>/var/www/xhprof_lib/utils/xhprof_runs.php</string>
63 * <element key="xhprofWeb">
64 * <string>http://localhost/xhprof_html/index.php</string>
66 * <element key="appNamespace">
67 * <string>Doctrine2</string>
69 * <element key="xhprofFlags">
70 * <string>XHPROF_FLAGS_CPU,XHPROF_FLAGS_MEMORY</string>
79 * @subpackage Util_Log
80 * @author Benjamin Eberlei <kontakt@beberlei.de>
81 * @author Sebastian Bergmann <sebastian@phpunit.de>
82 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
83 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
84 * @version Release: 3.5.13
85 * @link http://www.phpunit.de/
86 * @since Class available since Release 3.5.0
88 class PHPUnit_Util_Log_XHProf implements PHPUnit_Framework_TestListener
93 protected $runs = array();
98 protected $options = array();
103 protected $suites = 0;
108 * @param array $options
110 public function __construct(array $options = array())
112 if (!extension_loaded('xhprof')) {
113 throw new RuntimeException(
114 'The XHProf extension is required for this listener to work.'
118 if (!isset($options['appNamespace'])) {
119 throw new InvalidArgumentException(
120 'The "appNamespace" option is not set.'
124 if (!isset($options['xhprofLibFile']) ||
125 !file_exists($options['xhprofLibFile'])) {
126 throw new InvalidArgumentException(
127 'The "xhprofLibFile" option is not set or the configured file does not exist'
131 if (!isset($options['xhprofRunsFile']) ||
132 !file_exists($options['xhprofRunsFile'])) {
133 throw new InvalidArgumentException(
134 'The "xhprofRunsFile" option is not set or the configured file does not exist'
138 require_once $options['xhprofLibFile'];
139 require_once $options['xhprofRunsFile'];
141 $this->options = $options;
147 * @param PHPUnit_Framework_Test $test
148 * @param Exception $e
151 public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
156 * A failure occurred.
158 * @param PHPUnit_Framework_Test $test
159 * @param PHPUnit_Framework_AssertionFailedError $e
162 public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
169 * @param PHPUnit_Framework_Test $test
170 * @param Exception $e
173 public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
180 * @param PHPUnit_Framework_Test $test
181 * @param Exception $e
184 public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
191 * @param PHPUnit_Framework_Test $test
193 public function startTest(PHPUnit_Framework_Test $test)
195 if (!isset($this->options['xhprofFlags'])) {
196 $flags = XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY;
200 foreach (explode(',', $this->options['xhprofFlags']) as $flag) {
201 $flags += constant($flag);
205 xhprof_enable($flags);
211 * @param PHPUnit_Framework_Test $test
214 public function endTest(PHPUnit_Framework_Test $test, $time)
216 $data = xhprof_disable();
217 $runs = new XHProfRuns_Default;
218 $run = $runs->save_run($data, $this->options['appNamespace']);
219 $this->runs[] = $this->options['xhprofWeb'] . '?run=' . $run .
220 '&source=' . $this->options['appNamespace'];
224 * A test suite started.
226 * @param PHPUnit_Framework_TestSuite $suite
228 public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
234 * A test suite ended.
236 * @param PHPUnit_Framework_TestSuite $suite
238 public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
242 if ($this->suites == 0) {
243 print "\n\nXHProf runs: " . count($this->runs) . "\n";
245 foreach ($this->runs as $run) {
246 print ' * ' . $run . "\n";