YII Framework框架教程之日志用法详解(3)

Performance profiling is a special type of message logging. Performance profiling can be used to measure the time needed for the specified code blocks and find out what the performance bottleneck is.

性能分析是一类特殊类型的消息记录。性能分析可用于测量指定代码块所需的时间,并找出性能瓶颈是什么。

To use performance profiling, we need to identify which code blocks need to be profiled. We mark the beginning and the end of each code block by inserting the following methods:

要使用性能分析日志,我们需要确定哪些代码块需要分析。我们要在分析性能的代码短的开始和结尾添加如下方法:

Yii::beginProfile('blockID'); ...code block being profiled... Yii::endProfile('blockID');

where blockID is an ID that uniquely identifies the code block.

其中blockID是一个标识代码块的唯一ID。

Note, code blocks need to be nested properly. That is, a code block cannot intersect with another. It must be either at a parallel level or be completely enclosed by the other code block.

注意,这些方法不能交叉嵌套

To show profiling result, we need to install a CLogRouter application component with a CProfileLogRoute log route. This is the same as we do with normal message routing. The CProfileLogRoute route will display the performance results at the end of the current page.

为了显示分析结果,我们需要为CLogRouter增加CProfileLogRoute路由。然后通过CProfileLogRoute可以把性能测试结果显示在当前页面结束。

6. Profiling SQL Executions 分析SQL执行

Profiling is especially useful when working with database since SQL executions are often the main performance bottleneck of an application. While we can manually insert beginProfile and endProfilestatements at appropriate places to measure the time spent in each SQL execution, starting from version 1.0.6, Yii provides a more systematic approach to solve this problem.

在数据库开发中分析是特别有用的,因为SQL执行往往是应用程序的主要性能瓶颈。尽管我们可以手动在每个SQL执行的适当的地方插入beginProfile和endProfile来衡量花费的时间,但从1.0.6版本开始,Yii提供了更系统的方法来解决这个问题。

By setting CDbConnection::enableProfiling to be true in the application configuration, every SQL statement being executed will be profiled. The results can be readily displayed using the aforementionedCProfileLogRoute, which can show us how much time is spent in executing what SQL statement. We can also call CDbConnection::getStats() to retrieve the total number SQL statements executed and their total execution time.

再实际的应用程序当中通过设置CDbConnection::enableProfiling爱分析每一个正在执行的SQL语句。使用 CProfileLogRoute,结果可以很容易地显示。它可以显示我们是在执行什么SQL语句花费多少时间。我们也可以调用CDbConnection:getStats()来分析检索SQL语句的执行总数和其总的执行时间。

更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/e56c6452f5dda4d33b7377526222f42e.html