_php Tutorials for improving PHP program performance and load testing

Source: Internet
Author: User
Tags benchmark php debugger
An example of how to improve the performance of PHP program and load test code, the need for friends can see how to improve the performance of their programs oh.

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

The code is as follows Copy Code

$start _time = Microtime (true);

Some code that needs to calculate time
... code here ...

Print (' Code run Time is: '. Getexectime ($start _time));

function Getexectime ($start _time)
{
Return Microtime (True)-$start _time;
}pear's benchmark module provides a more detailed time-counting function

Require_once ' benchmark/timer.php ';
$timer =& New Benchmark_timer (true);
$timer->start ();
Set 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 the code here
$timer->stop ();
$timer->display (); Declare structure and ticks directives enable automatic recording of the time each line of PHP code executes

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

Return The Times stored in profiles, 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 < + + $x) {
Echo Similar_text (MD5 ($x), MD5 ($x * $x)), ";";
}
}

Display the data stored in the profiler
Print_r (TRUE); Note: The ticks Directive is obsolete in PHP 5.3.0 and will be removed from the PHP 6.0.0.

Code troubleshooting
The main introduction is Advanced PHP Debugger (APD), through the setup can generate trace files, the file analysis can get the details of the script

Website Stress test
Stress tests and benchmark tests are often confused. Benchmarking is a temporary activity done by a single developer, commonly used by the Apache HTTP Test tool--ab, which tests the number of requests per second that an HTTP server can have. Stress testing is a test technique that interrupts your Web application by testing breakpoints to identify and fix weaknesses in your application and provide a basis for when to acquire new hardware. The common open Source tool is siege.

Speed up skills
By installing the PHP accelerator, you can effectively provide PHP execution speed, the three most common accelerators are alternative PHP Cache (APC), eaccelerator, and Ioncube php Accelerator (PHPA). It is also important to note that the compatibility of accelerators usually lags behind the newly released PHP version.

In addition, the speed-up technique is not used when you can not use the regular, usually alternative solutions are more efficient than the use of regular.


http://www.bkjia.com/PHPjc/444723.html www.bkjia.com true http://www.bkjia.com/PHPjc/444723.html techarticle An example of how to improve the performance of PHP program and load test code, the need for friends can see how to improve the performance of their programs oh. Calculate the time of execution by following this simple ...

  • 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.