Learning the essence of PHP and compiling the performance of efficient PHP code _ PHP Tutorial

Source: Internet
Author: User
Tags apc
Learn the essence of PHP and write the performance of efficient PHP code. I. benchmark testing: when testing code performance benchmark testing involves Web applications, it usually refers to stress testing, that is, loading as much traffic as possible in your code, then it is measured in compliance 1. the benchmark test is used to detect the code performance.

When Benchmarking involves Web applications, it usually refers to "stress testing", that is, loading as much traffic as possible in your code, and then measuring its execution capability.

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

To perform stress testing, we need two things: simultaneous online users and a large number of requests. After these tools are used, many concurrent application threads represent users. Therefore, we only need to remember: concurrent threads = concurrent users.

1. apacheloud is super simple, usually including Apache installation, or as part of the Apache development kit-a binary file called simple AB. To use AB, you only need to specify the total number of requests (-n) and the number of concurrent threads (-c), and then let it start to work.

For example, here we use-n 1000-c 20 to generate 20 concurrent threads to execute 1000 requests.

$ ab -n 1000 -c 20 http://example.org/

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

2. JMeter is another Apache project with GUI and has more functions. To use JMeter, you need to create a test plan, add thread groups, add samplervers, specify JMeter configurations, add other options such as Cookie processors, and add listener processing results.

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

II. use cache to improve code performance

1. for Apache servers, code caching is implemented using apc.

Obtain APC from PECL (PHP Extension Community Library, PHP Extension shared class Library) for compilation, and then install the Extension.

$ pecl install apc

After that, edit the php. ini file and add it according to the settings:

extension = apc.so

Restart Apache.

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

2. for Windows/IIS servers, use Microsoft WinCache to implement code caching.

WinCache website: http://www.iis.net/downloads/microsoft/wincache-extension

3. use memcached to cache session data. memcached is a memory-based, cluster-friendly key/value pair storage. If you enable the memcached extension, you can use memcached instead of disk storage.

Memcached website: http://memcached.org/

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

Install memcached:

$ pecl install memcache # Install ext/memcache$ memcached -d -m 128 # Start memcached

Set php. ini:

session.save_handler = 'memcache'session.save_path = 'tcp://localhost:11211'

3. conduct a summary analysis of the program to find out where the problem is?

Profiling uses precise time or memory to detect the behavior of code running each action. Through analysis, locate the problem and optimize it.

We have two common profiling tools:

1. reliable Xdebug tools compiled by Derick Rethans and reviewed 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 application from Facebook. it was written in the previous section of XHGui Web by Paul Reinheim.

XHProf website: http://pecl.php.net/package/xhprof

XHGui website: https://github.com/perftools/xhgui

Summary:

First, we need to solve the biggest problem of performance degradation, so that we can achieve better overall performance improvement. If an SQL query takes 10 seconds and you increase the execution speed by 50%, you can save 5 seconds for yourself. However, if it takes 5 seconds to execute a PHP function, you have also improved the execution speed by 50%, but you have actually saved only half a second. In some cases, you will be absolutely limited by hardware performance. In our experience, you are more likely to be limited by disk or network I/O, rather than CPU or RAM. Then you need to scale the application on multiple computers.

When benchmark testing involves Web applications, it usually refers to stress testing, that is, loading as much traffic as possible in your code, and then measuring its execution...

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.