performance optimizations for PHP applications
The biggest benefit 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 that need to be used, we can guess how to accomplish a specific task.
Although PHP is easy to learn, it still takes a little time to learn some of the programming skills of PHP, especially those related to performance and memory usage. In PHP, there are a number of tips that can help us reduce memory usage and improve application performance. In this article, we will briefly introduce the analysis of the PHP application, how to change the script code, and the various parameter values before and after the comparison optimization.
By setting the timer program in the program and executing the code over and over again, we can get a set of data about the speed of the program, which can be used to discover bottlenecks in the program and how to optimize and improve the performance of the application.
Perhaps readers have heard of Pear library. We will use the Pear library to create the examples we need to use in our analysis, which is the simplest way to analyze existing code, which allows us to analyze the code without using a commercial product.
The name of the library we want to use is Pear::benchmark, which is useful for parsing and performance testing of code. This library provides a class named Benchmark_timer () that can record the time between a function call and the next function call. When testing the performance of the code, we can get a detailed script execution result, which is very simple as follows:
include_once ("benchmark/timer.php"); $bench = new Benchmark_timer; $bench start (); $bench, Setmarker (' Start of the script '); //Sleep now for a few minutes sleep (5); Stop ($bench); //Get profiling information from the timer Print_r ($bench-getprofiling ()); ? > The output from the above code execution is as follows: Array ( [0] = + Array ( [name] + Start [TIME] = 1013214253.0575 $ [diff] +- [total] = 0 ) [1] = = Array ( [name] + Start of the script [TIME] = > 1013214253.05761100 [diff] = 9.8943710327148E-05 [total] = 9.8943710327148E-05 ) [2] + A Rray ( [Name] = + Stop [TIME] = 1013214258.04920700 [diff] = 4.9915959835052 [Total] and 4. 9916949272156 ) ) |
The numbers above seem to be a messy set of numbers, but if the program is larger, the numbers are very useful.
Perhaps the vast majority of readers can also guess that the first table of an array is actually a method that calls the Benchmark_timer () class, for example
$bench, Start (), $bench, Setmarker () and $bench-> Stop (), the numbers related to these tables are fairly straightforward, so let's look at these numbers carefully:
[0] = =
[Name] + = [TIME] = [diff] = [Total] = ) |
The time table refers to when the Timestamp,diff table of Unix that is called by the start () method of Benchmark_timer () represents the interval between this call and the last call, because there is no previous one, so a dash is shown, The total table of accounts refers to the overall time that the code runs since the start of the test to this particular call. Let's take a look at the output of the next array:
[1] = =
[Name] + = [TIME] = [diff] = [Total] =
|
From the above figure we can see that after calling $bench-> start (), the program runs for 9.8943710327148E-05 seconds (that is, 0.0000989 seconds) after the start of calling $bench-> Setmarker (...).
a real-world performance Test experience
Although the example above is good, it really isn't a good example of how to optimize your site code design. I'll use my own experience as a site technician to illustrate how to solve performance problems.
I do not understand the code used by the site, because it is based on special needs, after years of development of ━━ one of the modules including the website conversion code, another module to record the use of the site, the other modules have their own role. I and the main developers of the site are aware that the site's code needs to be optimized, but it's unclear where the problem is.
to complete the task as quickly as possible, I began to study the main script code of the Web site and added some $bench-> setmarker () commands to all the script code and its include files, and then analyze the output of $bench-> getprofiling (). And surprised by the results, the problem is that in a function call to a conversion code that obtains a specific language name (such as en for English), the function is used hundreds of times on each page. Each time the function is called, the script code queries a MySQL database to obtain a real language name from a database table.
We created a buffer system for this type of information. After just 2 days of work, we have greatly improved the performance of the system, and the number of pages visited in the first week increased by 40%. Of course, this is just an example of how code analysis can improve the performance of Internet applications or Internet sites.
Performance test function call
When parsing a script or Web page (and its containing file), although Benchmark_timer () is particularly useful, it is not scientific, because we have to load the script multiple times to get the parsed data. And it is not called for a class or function. Another class called Benchmark_iterator in the
Pear::benchmark library solves this problem well, and it can display its analytical information for a particular function or class of methods. Its purpose is to be able to get consistent results from testing, because we know that if you run a script once and it runs for 10 seconds, it does not mean that it will run for 10 seconds at a time.
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 here::D b $created _date = $dbh, GetOne ($stmt);
(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 (' Getcreateddate ', 1); Print analysis information Print_r ($bench, Get ());
|
Running the above code can 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 [Ten] + 0.00093603134155273 [Mean] = 0.0064568161964417 [Iterations] = 10 ) |
The above numbers are well understood, and the mean entry represents the average time that the Getcreateddate () function runs 10 times. The actual test should run at least 1000 times, but the result of this example is enough to explain the problem. Editor
http://www.bkjia.com/PHPjc/446852.html www.bkjia.com true http://www.bkjia.com/PHPjc/446852.html techarticle The greatest benefit of PHP application performance optimization using PHP programming is that it is very easy to learn this programming language and its rich library. Even if the function you need to use is not very well understood, we also ...