Home > Backend Development > PHP Tutorial > Learn the essence of PHP and write efficient PHP code performance_PHP Tutorial

Learn the essence of PHP and write efficient PHP code performance_PHP Tutorial

WBOY
Release: 2016-07-13 10:28:53
Original
1068 people have browsed it

1. Use benchmark testing to detect the performance of the code

Benchmarking, when it comes to web applications, often refers to "stress testing," where you load your code with as much traffic as possible and then measure how well it performs.

Two benchmarking tools are recommended: ApacheBench (ab) and JMeter.

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

1. ApacheBench is super simple, usually included with Apache installation, or as part of the Apache development package - a binary file called a simple ab. To use ab, just specify the total number of requests (-n), and the number of concurrent threads (-c), and let it work.

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

$ ab -n <span>1000</span> -c <span>20</span> http:<span>//</span><span>example.org/</span>
Copy after login

ab usage 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 samplers, specify JMeter configuration, add other options such as cookie handlers, and add listeners to process the results.

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

2. Use caching to improve code performance

1. For the Apache server, use apc to implement code caching.

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

$ pecl install apc
Copy after login

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

extension = apc.so
Copy after login

Restart Apache and you should be good to go.

Reference for using apc: http://www.php.net/manual/en/book.apc.php

2. For Windows/IIS servers, use Microsoft's WinCache to implement code caching.

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

3. Use memcached to implement 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 storage sessions.

memcached website: http://memcached.org/

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

Install memcached:

$ pecl install memcache # Install ext/<span>memcache
$ memcached </span>-d -m <span>128</span> # Start memcached
Copy after login

Set php.ini:

session.save_handler = <span>'</span><span>memcache</span><span>'</span><span>
session.save_path </span>= <span>'</span><span>tcp://localhost:11211</span><span>'</span>
Copy after login

3. Conduct a program summary analysis to find where the problems are?

Profiling is the act of using precise time or memory testing to run each action in your code. Through analysis, find the location of the problem and then optimize it.

We have two commonly used profiling tools:

1. Reliable Xdebug tool written by Derick Rethans, with results 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, written by Paul Reinheimer as the XHGui Web front-end part.

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

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

Summary:

First of all, we need to solve the biggest problem of performance degradation, so that we can achieve better improvement in overall performance. If a SQL query takes 10 seconds and you increase its execution speed by 50%, you have saved yourself 5 seconds; however, if a PHP function takes 5 seconds, you have also increased its execution speed by 50%. %, you actually only save half a second. At some point you will be absolutely limited by the performance of your hardware, and in our experience you are more likely to be limited by disk or network I/O than CPU or RAM. This is when you need to start scaling your application across multiple computers.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/780576.htmlTechArticle1. Use benchmark testing to detect the performance of the code. When benchmark testing involves web applications, it usually refers to stress testing, that is, Load as much traffic as possible into your code and then measure its execution...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template