]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/Util/Log/XHProf.php
Release 6.2.1
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / Util / Log / XHProf.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
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
18  *     distribution.
19  *
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.
23  *
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.
36  *
37  * @package    PHPUnit
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
45  */
46
47 /**
48  * A TestListener that integrates with XHProf.
49  *
50  * Here is an example XML configuration for activating this listener:
51  *
52  * <code>
53  * <listeners>
54  *  <listener class="PHPUnit_Util_Log_XHProf" file="PHPUnit/Util/Log/XHProf.php">
55  *   <arguments>
56  *    <array>
57  *     <element key="xhprofLibFile">
58  *      <string>/var/www/xhprof_lib/utils/xhprof_lib.php</string>
59  *     </element>
60  *     <element key="xhprofRunsFile">
61  *      <string>/var/www/xhprof_lib/utils/xhprof_runs.php</string>
62  *     </element>
63  *     <element key="xhprofWeb">
64  *      <string>http://localhost/xhprof_html/index.php</string>
65  *     </element>
66  *     <element key="appNamespace">
67  *      <string>Doctrine2</string>
68  *     </element>
69  *     <element key="xhprofFlags">
70  *      <string>XHPROF_FLAGS_CPU,XHPROF_FLAGS_MEMORY</string>
71  *     </element>
72  *    </array>
73  *   </arguments>
74  *  </listener>
75  * </listeners>
76  * </code>
77  *
78  * @package    PHPUnit
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.14
85  * @link       http://www.phpunit.de/
86  * @since      Class available since Release 3.5.0
87  */
88 class PHPUnit_Util_Log_XHProf implements PHPUnit_Framework_TestListener
89 {
90     /**
91      * @var array
92      */
93     protected $runs = array();
94
95     /**
96      * @var array
97      */
98     protected $options = array();
99
100     /**
101      * @var integer
102      */
103     protected $suites = 0;
104
105     /**
106      * Constructor.
107      *
108      * @param array $options
109      */
110     public function __construct(array $options = array())
111     {
112         if (!extension_loaded('xhprof')) {
113             throw new RuntimeException(
114               'The XHProf extension is required for this listener to work.'
115             );
116         }
117
118         if (!isset($options['appNamespace'])) {
119             throw new InvalidArgumentException(
120               'The "appNamespace" option is not set.'
121             );
122         }
123
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'
128             );
129         }
130
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'
135             );
136         }
137
138         require_once $options['xhprofLibFile'];
139         require_once $options['xhprofRunsFile'];
140
141         $this->options = $options;
142     }
143
144     /**
145      * An error occurred.
146      *
147      * @param PHPUnit_Framework_Test $test
148      * @param Exception              $e
149      * @param float                  $time
150      */
151     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
152     {
153     }
154
155     /**
156      * A failure occurred.
157      *
158      * @param PHPUnit_Framework_Test                 $test
159      * @param PHPUnit_Framework_AssertionFailedError $e
160      * @param float                                  $time
161      */
162     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
163     {
164     }
165
166     /**
167      * Incomplete test.
168      *
169      * @param PHPUnit_Framework_Test $test
170      * @param Exception              $e
171      * @param float                  $time
172      */
173     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
174     {
175     }
176
177     /**
178      * Skipped test.
179      *
180      * @param PHPUnit_Framework_Test $test
181      * @param Exception              $e
182      * @param float                  $time
183      */
184     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
185     {
186     }
187
188     /**
189      * A test started.
190      *
191      * @param PHPUnit_Framework_Test $test
192      */
193     public function startTest(PHPUnit_Framework_Test $test)
194     {
195         if (!isset($this->options['xhprofFlags'])) {
196             $flags = XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY;
197         } else {
198             $flags = 0;
199
200             foreach (explode(',', $this->options['xhprofFlags']) as $flag) {
201                 $flags += constant($flag);
202             }
203         }
204
205         xhprof_enable($flags);
206     }
207
208     /**
209      * A test ended.
210      *
211      * @param PHPUnit_Framework_Test $test
212      * @param float                  $time
213      */
214     public function endTest(PHPUnit_Framework_Test $test, $time)
215     {
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'];
221     }
222
223     /**
224      * A test suite started.
225      *
226      * @param PHPUnit_Framework_TestSuite $suite
227      */
228     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
229     {
230         $this->suites++;
231     }
232
233     /**
234      * A test suite ended.
235      *
236      * @param PHPUnit_Framework_TestSuite $suite
237      */
238     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
239     {
240         $this->suites--;
241
242         if ($this->suites == 0) {
243             print "\n\nXHProf runs: " . count($this->runs) . "\n";
244
245             foreach ($this->runs as $run) {
246                 print ' * ' . $run . "\n";
247             }
248
249             print "\n";
250         }
251     }
252 }