PHP application performance optimization

Source: Internet
Author: User
Author: The biggest advantage of Liu Yanqing's compilation and use of PHP programming is that it is very easy to learn this programming language and its rich library. Even if we don't know much about the functions we need, we can guess how to complete a specific task. Although PHP is very easy to learn, it still takes a little time to learn some programming skills of PHP, especially with performance and memory usage.

The biggest advantage of using PHP programming is that it is very easy to learn this programming language and its rich library. Even if we don't know much about the functions we need, we can guess how to complete a specific task.

Although PHP is very easy to learn, it still takes us a little time to learn some PHP programming skills, especially those related to performance and memory usage. In PHP, there are many tips that can help us reduce memory usage and improve application performance. In this article, we will briefly introduce the analysis of PHP applications, how to change script code, and various parameter values before and after optimization.

By setting the timing program in the program and executing the code repeatedly, we can obtain a set of data about the program execution speed, which can be used to find bottlenecks in the program, and how to optimize and improve application performance.

Maybe you have heard of the PEAR Library. We will use the PEAR library to create an example for analysis. this is also the simplest method for analyzing existing code, it allows us to analyze code without using commercial products.

The name of the library we want to use is PEAR: Benchmark, which is very useful for code analysis and performance testing. This library provides a class named Benchmark_Timer (), which records the time between a function call and the next function call. When testing the code performance, we can get a detailed script execution result, which is very simple, as shown below:

Include_once ("Benchmark/Timer. php ");
$ Timer = new Benchmark_Timer;

$ Begin-> start ();
$ Marker-> setMarker ('Start of the script ');

// Sleep for several minutes
Sleep (5 );

$ Response-> stop ();

// Obtain the analysis information from the timer
Print_r ($ response-> getProfiling ());
?>


The output after code execution is as follows:


Array
(
[0] => Array
(
[Name] => Start
[Time] = & gt; 1013214253.05751200
[Diff] =>-
[Total] => 0
)

[1] => Array
(
[Name] => Start of the script
[Time] = & gt; 1013214253.05761100
[Diff] => 9.8943710327148E-05
[Total] => 9.8943710327148E-05
)

[2] => Array
(
[Name] => Stop
[Time] = & gt; 1013214258.04920700
[Diff] = & gt; 4.9915959835052
[Total] = & gt; 4.9916949272156
)

)





The numbers above seem to be a mess of numbers, but they are very useful if the program is larger.

Maybe the readers can guess that the first table in the array is actually calling the Benchmark_Timer () class method, for example

$ Marker-> start (), $ marker-> setMarker (), and $ marker-> stop (). the numbers related to these tables are quite simple, now let's take a closer look at these numbers:

[0] => Array

(

[Name] => Start

[Time] = & gt; 1013214253.05751200

[Diff] =>-

[Total] => 0

)

The time table refers to the UNIX timestamp when the start () method of Benchmark_Timer () is called. the diff table indicates the time interval between this call and the last call, because there is no previous one, a broken number is displayed. The total table refers to the total time of code running from the test to the specific call. Next let's take a look at the output of the next array:

[1] => Array

(

[Name] => Start of the script

[Time] = & gt; 1013214253.05761100

[Diff] => 9.8943710327148E-05

[Total] => 9.8943710327148E-05

)

From the preceding figure, we can see that after calling $ program-> start (), the program runs 9.8943710327148E-05 seconds (that is

0.0000989 seconds) and then start calling $ marker-> setMarker (....).

A real performance test experience

Although the above example is good, it is really not a good example of deciding how to optimize your site code design. Next I will explain how to solve the performance problems with my personal experience as a website technician.

I don't quite understand the code used by the website, because it is developed based on special requirements over the years. one module of the website contains the website conversion code, and the other module records the usage of the website, other modules have their respective functions. I and the main website developers both realized that the website code needs to be optimized, but they do not know where the problem is.

To complete the task as soon as possible, I started to study the main script code of the website and added some

$ Marker-> setMarker () command, analyze the output of $ marker-> getProfiling (), and be surprised by the result, it turns out that in a function call that obtains the conversion code of a specific language name (for example, en stands for english), the function is used hundreds of times on every page. Each time you call this function, the script code queries a MySQL database and obtains the real language name from a database table.

So we created a buffer system for this type of information. After just two days of work, we have greatly improved the system performance. in the first week, the page views increased by 40%. Of course, this is just an example of how analysis code can improve the performance of Internet applications or internet websites.

Performance Test function call

Although Benchmark_Timer () is particularly useful in analyzing a script or webpage (and its contained files), it is not scientific, because we must load the script multiple times to obtain the analyzed data, and it is not called for a class or function.

PEAR: another class in the Benchmark library called Benchmark_Iterator can solve this problem well. it can display the analysis information of a specific function or class method. Its purpose is to get consistent results from the test, because we know that if a script is run once, its running time is 10 seconds, it does not mean that every running time is always 10 seconds.

In any case, let's see some examples:

// Database connection code
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 ";
// Here PEAR: DB is used
$ Created_date = $ dbh-> getOne ($ stmt );
If (PEAR: isError ($ created_date) |
(Empty ($ created_date ))){
Return false;
} Else {
Return $ created_date;
}
}

Include_once 'benchmark/Iterate. php ';
$ Tenant = new Benchmark_Iterate;

// Run the getDate function 10 times
$ Response-> run (10, 'getcreateddate', 1 );

// Print the analysis information
Print_r ($ response-> get ());
?>




Running the above code produces similar results:


Array
(
[1] => 0.055413007736206.
[2] => 0.0012860298156738.
[3] = & gt; 0.0010279417037964
[4] = & gt; 0.00093603134155273
[5] = & gt; 0.00094103813171387
[6] = & gt; 0.00092899799346924
[7] = & gt; 0.0010659694671631
[8] = & gt; 0.00096404552459717
[9] = & gt; 0.0010690689086914
[10] => 0.00093603134155273.
[Mean] = & gt; 0.0064568161964417
[Iterations] => 10
)



The preceding numbers are easy to understand. mean entries indicate the average time of the getCreatedDate () function to run for 10 times. The actual test should be performed at least 1000 times, but the results obtained in this example are sufficient to explain the problem.

Conclusion

It is hoped that the majority of readers will learn how to quickly analyze PHP code through this article. Here, I also want to remind readers that analyzing code is not a simple task, because we must master a lot of features related to this language. The code used to add timing in the code helps to find slow functions. using repeated methods, we can find the correct method to optimize the code.

Reposted in this article: SCID

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.