CodeIgniter - 性能压测

CodeIgniter - 性能压测 首页 / Codeigniter入门教程 / CodeIgniter - 性能压测

如果要测量执行一组行或内存使用所花费的时间,则可以使用CodeIgniter中的基准测试点进行计算。为此,在CodeIgniter中有一个单独的"Benchmarking"类。

此类会自动加载;它可以在控制器,视图和模型类中的任何位置使用。您所需要做的就是标签一个起点和终点,然后在这两个标签的点之间执行 elapsed_time()函数,您可以获得执行该代码所花费的时间,如下所示。

<?php 
   $this->benchmark->mark('code_start');
  
   //Some code happens here  

   $this->benchmark->mark('code_end');
  
   echo $this->benchmark->elapsed_time('code_start', 'code_end'); 
?>

要显示内存使用情况,请使用函数 memory_usage(),如以下代码所示。

无涯教程网

<?php 
   echo $this->benchmark->memory_usage(); 
?>

Benchmark示例

创建一个名为 Profiler_controller.php 的控制器,并将其保存在 application/controller/Profiler_controller.php 中

<?php 
   class Profiler_controller extends CI_Controller {
  
      public function index() {
	
         //启用分析器
         $this->output->enable_profiler(TRUE); 
         $this->load->view('test'); 
      } 
  
      public function disable() {
	
         //禁用分析器
         $this->output->enable_profiler(FALSE); 
         $this->load->view('test'); 
      }
		
   } 
?>  

创建一个名为 test.php 的视图文件,并将其保存在 application/views/test.php

<!DOCTYPE html> 
<html lang="en">
 
   <head> 
      <meta charset="utf-8"> 
      <title>CodeIgniter View Example</title> 
   </head>
	
   <body> 
      CodeIgniter View Example 
   </body>
	
</html>

在 application/config/routes.php 处更改route.php文件,以为上述控制器添加路由,并在文件末尾添加以下行。

$route['profiler']="Profiler_controller"; 
$route['profiler/disable']="Profiler_controller/disable"

之后,您可以在浏览器的地址栏中键入以下URL以执行示例。

http://yoursite.com/index.php/profiler

上面的URL将启用事件探查器,并将产生输出,如以下屏幕图所示。

View Example

要禁用分析,请执行以下URL。

链接:https://www.learnfk.comhttps://www.learnfk.com/codeigniter/codeigniter-benchmarking.html

来源:LearnFk无涯教程网

http://yoursite.com/index.php/profiler/disable

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

机器学习40讲 -〔王天一〕

如何做好一场技术演讲 -〔极客时间〕

Web协议详解与抓包实战 -〔陶辉〕

OpenResty从入门到实战 -〔温铭〕

微信小程序全栈开发实战 -〔李艺〕

手机摄影 -〔@随你们去〕

技术面试官识人手册 -〔熊燚(四火)〕

商业思维案例笔记 -〔曹雄峰〕

Go进阶 · 分布式爬虫实战 -〔郑建勋〕

好记忆不如烂笔头。留下您的足迹吧 :)