Improve php program performance and load test_php tutorial

Source: Internet
Author: User
Tags php debugger
Improve php program performance and load testing. An example code about improving php program performance and load testing. if you need it, you can see how to improve the performance of your program. The following is a simple example of how to improve the performance of php programs and load testing. if you need it, you can see how to improve the performance of your programs.

Computing execution time
The following simple method can be used to calculate the execution time of a program (subtle)

The code is as follows:

$ Start_time = microtime (true );

// Code that requires time calculation
//... Code here...

Print ('code Running time: '. getExecTime ($ start_time ));

Function getExecTime ($ start_time)
{
Return microtime (true)-$ start_time;
} PEAR's Benchmark module provides more detailed time statistics functions

Require_once 'benchmark/Timer. php ';
$ Timer = & new Benchmark_Timer (true );
$ Timer-> start ();
// Set the function
$ Timer-> setMarker ('setup ');
// Some more code executed here
$ Timer-> setMarker ('middle ');
// Even yet still more code here
$ Timer-> setmarker ('done ');
// And a last bit of code here
$ Timer-> stop ();
$ Timer-> display (); the declare structure and ticks command can be used to automatically record the execution time of each line of PHP code.

// A function that records the time when it is called
Function profile ($ dump = FALSE)
{
Static $ profile;

// Return the times stored in profile, then erase it
If ($ dump ){
$ Temp = $ profile;
Unset ($ profile );
Return ($ temp );
}

$ Profile [] = microtime ();
}

// Set up a tick handler
Register_tick_function ("profile ");

// Initialize the function before the declare block
Profile ();

// Run a block of code, throw a tick every 2nd statement
Declare (ticks = 2 ){
For ($ x = 1; $ x <50; ++ $ x ){
Echo similar_text (md5 ($ x), md5 ($ x * $ x )),";";
}
}

// Display the data stored in the profiler
Print_r (profile (TRUE); Note: the ticks command is outdated in PHP 5.3.0 and will be removed from PHP 6.0.0.

Code troubleshooting
Advanced PHP Debugger (APD) is used to generate a trace file. you can analyze the file to obtain detailed information about the script.

Website stress testing
Stress testing and benchmarking are often obfuscated. Benchmark testing is a temporary activity completed by individual developers. it is commonly used as the Apache HTTP testing tool-AB, which can test the number of requests per second on an HTTP server. Stress testing is a testing technology that can interrupt your WEB applications. through Breakpoint testing, it can identify and fix vulnerabilities in applications and provide a basis for when to purchase new hardware. The commonly used open-source tool is Siege.

Acceleration skills
Installing the PHP Accelerator can effectively provide PHP execution speed. The three common accelerators are Alternative PHP Cache (APC), eAccelerator, and ionCube PHP Accelerator (PHPA ). In addition, it should be noted that the accelerator compatibility usually lags behind the newly released PHP version.

In addition, the speed-up technique is to avoid using regular expressions whenever possible. Generally, the alternative solution is more efficient than using regular expressions.


Bytes. The execution time of the computation is as follows...

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.