php 效能測試工具

來源:互聯網
上載者:User

<?php<br />/*<br /> * This class enables you to mark points and calculate the time difference<br /> * between them. Memory consumption can also be displayed.<br /> *<br /> * @packageCodeIgniter<br /> * @subpackageLibraries<br /> * @categoryLibraries<br /> * @authorExpressionEngine Dev Team<br /> * @linkhttp://codeigniter.com/user_guide/libraries/benchmark.html<br /> */<br />class CI_Benchmark {</p><p>var $marker = array();</p><p>// --------------------------------------------------------------------</p><p>/**<br /> * Set a benchmark marker<br /> *<br /> * Multiple calls to this function can be made so that several<br /> * execution points can be timed<br /> *<br /> * @accesspublic<br /> * @paramstring$namename of the marker<br /> * @returnvoid<br /> */<br />function mark($name)<br />{<br />$this->marker[$name] = microtime();<br />}</p><p>// --------------------------------------------------------------------</p><p>/**<br /> * Calculates the time difference between two marked points.<br /> *<br /> * If the first parameter is empty this function instead returns the<br /> * {elapsed_time} pseudo-variable. This permits the full system<br /> * execution time to be shown in a template. The output class will<br /> * swap the real value for this variable.<br /> *<br /> * @accesspublic<br /> * @paramstringa particular marked point<br /> * @paramstringa particular marked point<br /> * @paramintegerthe number of decimal places<br /> * @returnmixed<br /> */<br />function elapsed_time($point1 = '', $point2 = '', $decimals = 4)<br />{<br />if ($point1 == '')<br />{<br />return '{elapsed_time}';<br />}</p><p>if ( ! isset($this->marker[$point1]))<br />{<br />return '';<br />}</p><p>if ( ! isset($this->marker[$point2]))<br />{<br />$this->marker[$point2] = microtime();<br />}</p><p>list($sm, $ss) = explode(' ', $this->marker[$point1]);<br />list($em, $es) = explode(' ', $this->marker[$point2]);</p><p>return number_format(($em + $es) - ($sm + $ss), $decimals);<br />}</p><p>// --------------------------------------------------------------------</p><p>/**<br /> * Memory Usage<br /> *<br /> * This function returns the {memory_usage} pseudo-variable.<br /> * This permits it to be put it anywhere in a template<br /> * without the memory being calculated until the end.<br /> * The output class will swap the real value for this variable.<br /> *<br /> * @accesspublic<br /> * @returnstring<br /> */<br />function memory_usage()<br />{<br />return '{memory_usage}';<br />}</p><p>}</p><p>// END CI_Benchmark class</p><p>/* End of file Benchmark.php */<br />/* Location: ./system/libraries/Benchmark.php */<br />

 

一個小例子,如下:

<?php</p><p>header("Content-Type:text/html;charset=UTF-8");<br />if( ! defined('BASEPATH') ) {<br />define ('BASEPATH', $_SERVER['DOCUMENT_ROOT'].'/news/');<br />}<br />require_once BASEPATH.'service/Json_service.php';<br />require_once BASEPATH.'common/Benchmark.php';</p><p>$benchmark = new CI_Benchmark();<br />$benchmark->mark('10');<br />$benchmark->mark('11');</p><p>$benchmark->mark('12');<br />$jsonservice = new JsonService();<br />$benchmark->mark('13');<br />$resultjson = $jsonservice->getNewsTitleJsonArray($categoryid=143746653,$index=1,$max=20,$lastpage,$lastid);<br />$benchmark->mark('14');<br />$jsonservice->clear();<br />//$size = strlen($resultjson);<br />//header("Content-Length: $size");<br />$benchmark->mark('15');<br />echo '-----------------------------------------'.'<br>';<br />echo $benchmark->elapsed_time('10','11',6).'<br>';<br />echo $benchmark->elapsed_time('11','12',6).'<br>';<br />echo $benchmark->elapsed_time('12','13',6).'<br>';<br />echo $benchmark->elapsed_time('13','14',6).'<br>';<br />echo $benchmark->elapsed_time('14','15',6).'<br>';<br />?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.