Home  >  Article  >  Backend Development  >  A brief discussion on PHP caching technology 3_PHP tutorial

A brief discussion on PHP caching technology 3_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:43:51814browse

php应用程序的性能优化

使用PHP编程的最大好处是学习这种编程语言非常容易以及其丰富的 库。即使对需要使用的函数不是十分了解,我们也能够猜测出如何完成一个特定的任务。

尽管PHP非常简单易学,但我们仍然需要花费一点时 间来学习PHP的一些编程技巧,尤其是与性能和内存占用相关的技巧。在PHP中,有许多小技巧能够使我们减少内存的占用,并提高应用程序的性能。在本篇文 章中,我们将对PHP应用程序的分析、如何改变脚本代码以及比较优化前后的各种参数值进行简要的介绍。

通过在程序中设置计时的程序,并反复执行这些代码,我们可以获得有关程序执行速度的一组数据,这些数据可以可以用来发现程序中的瓶颈,以及如何进行 优化,提高应用程序的性能。

也许读者曾经听说过PEAR库吧。我们将使用PEAR库创建在分析时需要使用的例子,这也是对现有的代码进行分析的最简单的方法,它使我们无需使用 商用产品就能对代码进行分析。

我们要使用的库的名字是PEAR::Benchmark,它对于对代码进行分析和性能测试非常有用。这个 库提供一个名字为Benchmark_Timer()的类,能够记录一个函数调用和下一个函数调用之间的时间。在对代码的性能进行测试时,我们可以得到一 个详细的脚本执行结果,它非常简单,如下所示:

include_once("Benchmark/Timer.php");

$bench = new Benchmark_Timer;

$bench-> start();

$bench-> setMarker(Start of the script);

// 现在处于睡眠状态几分钟

sleep(5);

$bench-> stop();

// 从计时器中获得分析信息

PRint_r($bench-> getProfiling());

?> 

上面代码执行后的输出如下所示:

Array

(

[0] =>    Array

(

[name] =>    Start

[time] =>    1013214253.05751200

[diff] =>    -

[total] =>    0

)

[1] =>    Array

(

[name] =>    Start of the script

[time] =>    1013214253.05761100

[diff] =>    9.8943710327148E-05

[total] =>    9.8943710327148E-05

)

[2] =>    Array

(

[name] =>    Stop

[time] =>    1013214258.04920700

[diff] =>    4.9915959835052

[total] =>    4.9916949272156

)

)

上 面的数字似乎是一组杂乱无章的数字,但如果程序的规模更大,这些数字就十分地有用了。

也许广大读者也能猜测到,数组的第一个表目是实际 调用Benchmark_Timer()类的方法,例如

$bench-> start()、$bench-> setMarker()和$bench-> stop(),与这些表目有关的数字是相当简单的,现在我们来仔细地研究这些数字:

[0] =>    Array

(

[name] =>    Start

[time] =>    1013214253.05751200

[diff] =>    -

[total] =>    0

)

time 表目指的是何时对Benchmark_Timer()的start()方法调用的UNIX的timestamp,diff表目表示这次调用和上次调用之间 的时间间隔,由于这里没有上一次,因此显示出了一个破折号,total表目指的是自测试开始到这一特定的调用之前代码运行的总的时间。下面我们来看看下一 个数组的输出:

[1] =>    Array

(

[name] =>    Start of the script

[time] =>    1013214253.05761100

[diff] =>    9.8943710327148E-05

[total] =>    9.8943710327148E-05

)

从上面的数字我们可以看出,在调用$bench-> start()之后,程序运行了9.8943710327148E-05秒(也就是0.0000989秒)后开始调用$bench-> setMarker(….)。

一次真实的性能测试经历

尽管上面的例子不错,但在对于决定如何优化你的站点代码设计方面,它 真的不能算是一个好例子。下面我将用我自己作为网站技术人员的一段亲身经历来说明如何解决性能方面存在的问题。

I don’t really understand the code used by the website, because it was developed over many years based on special needs - one module includes the website conversion code, another module records the usage of the website, and other modules Each has its own role. The main developer of the website and I both realize that the website's code needs to be optimized, but we don't know where the problem lies.

In order to complete the task as quickly as possible, I started to study the main script code of the website, and added some $bench-> setMarker() commands to all script codes and their included files, and then analyzed $bench-> getProfiling( ) and was surprised by the results. It turned out that the problem lay in a function call related to the conversion code that obtains a specific language name (such as en for english), which was used hundreds of times on each page. Each time this function is called, the script code queries a MySQL database to obtain the actual language name from a database table.

So we create a buffering system for this type of information. After just 2 days of work, we greatly improved the performance of the system, and the number of page views increased by 40% in the first week. Of course, this is just one example of how analyzing code can improve the performance of an Internet application or Internet website.

Performance test function call

Although Benchmark_Timer() is particularly useful when analyzing a script or web page (and its containing files), it is not scientific because we must load the script multiple times to obtain the analyzed data, and it is not specific to a specific Class or function call.

PEAR:: Another class in the Benchmark library called Benchmark_Iterator can solve this problem very well. It can display analysis information for specific functions or class methods. The purpose of it is to be able to get consistent results from testing, because we know that if we run a script once and it runs for 10 seconds, it doesn't mean that it will always run for 10 seconds every time.

In any case, lets see some examples:

// Code to connect to the database

include_once("DB.php");

$dsn = array(

phptype => mysql,

hostspec => localhost,

database => database_name,

username => user_name,

passWord => password

);

$dbh = DB::connect($dsn);

function getCreatedDate($id)

{

global $dbh;

> $stmt = "SELECT created_date FROM users WHERE id=$id";

// Use PEAR::DB here

$created_date = $dbh-> getOne($stmt);

if ((PEAR::isError($created_date)) ||

(empty($created_date))) {

return false;

} else {

return $created_date;

}

}

include_once Benchmark/Iterate.php;

$bench = new Benchmark_Iterate;

//Run the getDate function 10 times

$bench-> run(10, getCreatedDate, 1);

//Print analysis information

print_r($bench-> get());

?>

Running the above code will produce results similar to the following:

Array

(

[1] => 0.055413007736206

[2] => 0.0012860298156738

[3] => 0.0010279417037964

[4] => 0.00093603134155273

[5] => 0.00094103813171387

[6] => 0.00092899799346924

[7] => 0.0010659694671631

[8] => 0.00096404552459717

[9] => 0.0010690689086914

[10] => 0.00093603134155273

[mean] => 0.0064568161964417

[iterations] => 10

)

The above numbers are easy to understand. The mean entry represents the average time of 10 runs of the getCreatedDate() function. In actual testing, you should run it at least 1000 times, but the results from this example are enough to illustrate the problem.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478787.htmlTechArticlePerformance Optimization of PHP Applications The biggest benefit of programming in PHP is how easy it is to learn this programming language and its rich Library. Even if we don’t know much about the functions we need to use, we...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn