|
Discuz!X 内核提供了一种简单的性能测试方法,可以用于测试内核性能或者用户插件运行性能。即使用:- C::analysisStart 和 C::analysisStop
复制代码 两个函数,C::analysisStart() 用于设置检测起点,C::analysisStop() 用于停止检测。下面我们来看一段具体的代码:
- require '../../class/class_core.php';
- C::analysisStart('test');
- $discuz = C::app();
- $discuz->init();
- echo "<pre>".print_r(C::analysisStop('test'),1)."</pre>";
复制代码 此段代码将计算出 Dz 内核的加载时间,以及加载前后的内存占用情况,运行后输出结果如下:
- Array
- (
- [start_memory_get_usage] => 407696
- [start_memory_get_real_usage] => 2097152
- [start_memory_get_peak_usage] => 407696
- [start_memory_get_peak_real_usage] => 2097152
- [time] => 28.76306
- [stop_memory_get_usage] => 1103120
- [stop_memory_get_real_usage] => 2097152
- [stop_memory_get_peak_usage] => 1303416
- [stop_memory_get_peak_real_usage] => 2097152
- )
复制代码 time 的单位是毫秒,也就是本次内核加载用了 28ms,内存相关参数的单位是字节,可以看到 real_usage 等于 2097152,也就是差不多2M字节了。运行此脚本的主机配置为:Intel® Core™ i5-4460 CPU @ 3.20GHz × 4 ,16GB 内存。
下面我们再看看 C::analysisStart 和 C::analysisStop 的源码:
- public static function analysisStart($name){
- $key = 'other';
- if($name[0] === '#') {
- list(, $key, $name) = explode('#', $name);
- }
- if(!isset($_ENV['analysis'])) {
- $_ENV['analysis'] = array();
- }
- if(!isset($_ENV['analysis'][$key])) {
- $_ENV['analysis'][$key] = array();
- $_ENV['analysis'][$key]['sum'] = 0;
- }
- $_ENV['analysis'][$key][$name]['start'] = microtime(TRUE);
- $_ENV['analysis'][$key][$name]['start_memory_get_usage'] = memory_get_usage();
- $_ENV['analysis'][$key][$name]['start_memory_get_real_usage'] = memory_get_usage(true);
- $_ENV['analysis'][$key][$name]['start_memory_get_peak_usage'] = memory_get_peak_usage();
- $_ENV['analysis'][$key][$name]['start_memory_get_peak_real_usage'] = memory_get_peak_usage(true);
- }
- public static function analysisStop($name) {
- $key = 'other';
- if($name[0] === '#') {
- list(, $key, $name) = explode('#', $name);
- }
- if(isset($_ENV['analysis'][$key][$name]['start'])) {
- $diff = round((microtime(TRUE) - $_ENV['analysis'][$key][$name]['start']) * 1000, 5);
- $_ENV['analysis'][$key][$name]['time'] = $diff;
- $_ENV['analysis'][$key]['sum'] = $_ENV['analysis'][$key]['sum'] + $diff;
- unset($_ENV['analysis'][$key][$name]['start']);
- $_ENV['analysis'][$key][$name]['stop_memory_get_usage'] = memory_get_usage();
- $_ENV['analysis'][$key][$name]['stop_memory_get_real_usage'] = memory_get_usage(true);
- $_ENV['analysis'][$key][$name]['stop_memory_get_peak_usage'] = memory_get_peak_usage();
- $_ENV['analysis'][$key][$name]['stop_memory_get_peak_real_usage'] = memory_get_peak_usage(true);
- }
- return $_ENV['analysis'][$key][$name];
- }
复制代码 源码逻辑很简单,这里就不多说了。
|
|