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(),如以下代码所示。

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

来源:LearnFk无涯教程网

<?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。

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

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

技术教程推荐

SQL必知必会 -〔陈旸〕

Linux实战技能100讲 -〔尹会生〕

高并发系统设计40问 -〔唐扬〕

摄影入门课 -〔小麥〕

恋爱必修课 -〔李一帆〕

零基础实战机器学习 -〔黄佳〕

eBPF核心技术与实战 -〔倪朋飞〕

结构会议力 -〔李忠秋〕

互联网人的数字化企业生存指南 -〔沈欣〕

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