Learn the essence of PHP and write the performance of efficient PHP code

Source: Internet
Author: User
Tags apc benchmark

First, the use of benchmark testing, testing the performance of the Code

When a benchmark involves a web application, it usually refers to a "stress test" that loads as much traffic as possible in your code and then measures its ability to execute.

Two benchmark tools are recommended: Apachebench (AB) and JMeter.

To perform stress tests, we need two things: both online users and a large number of requests. With these tools, many application threads running concurrently represent the user. So we just need to remember that concurrent threads = concurrent users.

1, Apachebench is super simple, usually contains Apache installation, or as part of the Apache development Package--a binary file called Simple AB. To use AB, you simply specify the total number of requests (-N), and the number of concurrent threads (-c), and then let it start working.

For example: Here we use the-n 1000-c 20 to generate 20 concurrent threads to execute 1000 requests.

 +  http://example.org/

AB Use reference: http://httpd.apache.org/docs/2.0/programs/ab.html

2, JMeter is another GUI-equipped Apache project, and has more features. To use JMeter, you need to create a test plan, add a thread group, add a sampler, specify the configuration of the JMeter, add additional options such as a cookie processor, and increase the listener processing results.

JMeter Website: http://jmeter.apache.org/

Ii. using caching to improve code performance

1. For Apache servers, use APC to implement code caching.

Get APC to compile from PECL (PHP Extension Community library,php extended shared class Library), and then install the extension.

$ pecl Install APC

After that, depending on the settings, you need to edit the php.ini file and add it:

Extension = apc.so

Restart Apache, and then you are ready.

APC Use reference: http://www.php.net/manual/en/book.apc.php

2, for Windows/iis server, use Microsoft's Wincache to implement code cache.

Wincache Website: http://www.iis.net/downloads/microsoft/wincache-extension

3, using memcached to achieve session data caching, Memcached is a memory-based, cluster-friendly key/value pair storage. If you enable the memcached extension, you can automatically use memcached instead of disk to store the reply.

Memcached Website: http://memcached.org/

memcached Use reference: http://www.php.net/manual/zh/book.memcached.php

Install memcached:

$ pecl Install memcache # install ext/ # Start memcached

Set PHP.ini:

' memcache '  'tcp://localhost:11211'

Third, conduct a summary analysis of the procedure, find out where the problem?

Program profiling (profiling) is the act of running each action with precise time or memory-detection code. Through analysis, find the location of the problem and then optimize it.

We have two commonly used profiling tools:

1. Reliable Xdebug tools written by Derick Rethans, and audited results by kcachegrind or qcachegrind.

Xdebug Website: http://xdebug.org/

Kcachegrind Website: http://sourceforge.jp/projects/freshmeat_kcachegrind/releases/

Qcachegrind Website: http://sourceforge.jp/projects/freshmeat_kcachegrind/releases/

2. The newly developed Xhprof tool is an app from Facebook that was written by Paul Reinheimer in the front section of the Xhgui Web.

XHPROF Website: http://pecl.php.net/package/xhprof

Xhgui Website: Https://github.com/perftools/xhgui

Summarize:

First of all, we need to solve the biggest problem of performance degradation, so that we can achieve a better overall performance improvement. If an SQL query takes 10 seconds and you increase it by 50%, you save yourself 5 seconds, however, if you spend 5 seconds executing a PHP function, you also increase its execution speed by 50%, and you actually save only half a second. At some point, you will be subject to the absolute limitations of hardware performance, and in our experience you are more likely to be constrained by disk or network I/O rather than CPU or RAM. At this point you need to start scaling the application on more than one computer.

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.