Explore PHP's performance in depth

Source: Internet
Author: User
Tags error handling functions hash variables php and php code variable zend

1. Origin

About PHP, a lot of people's intuitive feeling is that PHP is a flexible scripting language, library class rich, easy to use, safe, very suitable for web development, but low performance. Is the performance of PHP really as bad as everyone feels? This article revolves around such a topic to discuss. From the source code, application scenarios, benchmark performance, comparative analysis of several aspects of the performance of PHP in-depth analysis, and through the real data to speak.

2. Analyze PHP performance from the principle

From the principle analysis of PHP performance, mainly from the following aspects: memory management, variables, functions, operating mechanism to carry out the analysis.

2.1 Memory Management

Like Nginx memory management, PHP is also based on the memory pool, and introduces the lifecycle concept of the memory pool. In the memory pool, PHP hosts all of the memory-related operations of PHP scripts and extensions. The management of large memory and small memory adopts different implementation methods and optimization, and can refer to the following documents: HTTPS://WIKI.PHP.NET/INTERNALS/ZEND_MM. During the lifecycle of memory allocation and recycling, PHP uses an initialization application + dynamic expansion + memory identity recovery mechanism, and mask the memory pool directly after each request is completed.

2.2 Variables

As is known to all, PHP is a weak variable type language, so inside PHP, all PHP variables correspond to a type zval, which is defined as follows:

Figure A PHP variable

In terms of variables, PHP does a lot of optimization work, such as reference counting and copy on writer mechanism. This ensures optimal memory usage and reduces the number of memory copies (refer to http://blog.xiuwz.com/2011/11/09/php-using-internal-zval/). In the array aspect, the PHP internal uses the efficient hashtable to realize.

2.3 function

Inside PHP, all PHP functions are converted back into a function pointer inside. For example, the extended function

    1. 		Zend_function (my_function);//similar to FUNCTION my_function () {}

It will be a function when it is expanded inside

    1. 		void Zif_my_function (internal_function_parameters);
    2. void Zif_my_function (
    3. int HT,
    4. Zval * Return_value,
    5. Zval * This_ptr,
    6. int return_value_used,
    7. Zend_executor_globals * executor_globals
    8. );

From this point of view, the PHP function also corresponds to a function pointer inside.

2.4 Operating mechanism

In the words of PHP performance, many people will say "c/s + + is compiled, Java is a precompiled, PHP is interpreted." That is, PHP is the first dynamic parsing code run, so from this point of view, PHP performance must be very poor.

Indeed, running from PHP script to output is really a process of dynamic parsing and code running. Specifically, the operating mechanism of the PHP script is shown in the following illustration:

Figure II PHP operating mechanism

The running phase of PHP is also divided into three phases:

    • Parse. Grammar analysis phase.
    • Compile. Compile output opcode intermediate code.
    • Execute. Run, dynamically run for output.

So, inside the PHP itself, there is the process of compiling. and accordingly generated a large number of opcode cache tools, such as APC, EACC, XCache and so on. These opcode cache are basically standard in the production environment. Based on opcode cache, to do "PHP script compile once, run more than once" effect. From this point, PHP is very similar to the Java half-compilation mechanism.

So, from the operating mechanism, PHP's operating mode and Java are very similar, are first generated in the middle code, and then run on different virtual machines.

2.5 Dynamic operation

From the above several analysis, PHP in memory management, variables, functions, operating mechanism, and so on several aspects have done a lot of work, so from the principle, PHP should not exist performance problems, performance should at least and Java closer.

This time we have to talk about the characteristics of PHP Dynamic language performance problems, because PHP is dynamic runtime, so all variables, functions, object calls, scope implementation, etc. are in the implementation phase is determined. This fundamentally determines the PHP performance is difficult to change some things: in the static compile phase of C + + and other variables, functions, in PHP need to be in dynamic operation to determine, also decided that the PHP middle code can not run directly and need to run on the Zend engine.

Speaking of the specific implementation of PHP variables, but also have to say something: Hashtable. Hashtable can be said to be one of the PHP soul, widely used within PHP, including variable symbol stack, function symbol stack, etc. are based on Hashtable.

Take PHP variables as an example to illustrate the dynamic operating characteristics of PHP, such as code:

    1. 		<?php
    2. $var = "Hello, blog.xiuwz.com";
    3. ?>

The result of this code is to add an item to the variable symbol stack (which is a hashtable)

When you want to use the variable, go to the variable conformance stack to find (that is, the variable call to the process of a hash lookup).

Also for function calls are basically similar to having a function symbol stack (hashtable).

In fact, the dynamic operation of the variable lookup characteristics, in the operating mechanism of PHP can also be seen some. The PHP code is explained and compiled in the following diagram:

Figure 3 PHP Run instance

As can be seen from the above diagram, the PHP code after compile, the output of the class symbol table, function symbol table, and opcode. In real execution, Zend engine will look up and handle the corresponding symbol table according to the OP code.

In a way, it is difficult to find a solution to this problem. Because this is due to the dynamic nature of the PHP language is determined. But at home and abroad there are a lot of people looking for solutions. Because of this, you can fundamentally and completely optimize PHP. The typical example is Facebook's hiphop (https://github.com/facebook/hiphop-php).

2.6 Conclusion

From the above analysis, in the basis of memory management, variables, functions, operating mechanism, PHP itself will not have obvious performance differences, but because of PHP dynamic operating characteristics, the decision of PHP and other compiled language, all the variables to find, function run, and so on. The CPU overhead and additional memory overhead of the hash lookup, as to how much of this overhead, can be obtained through subsequent benchmarking performance and comparative analysis.

As a result, you can generally see that PHP is not suitable for some of the scenarios: a large number of computational tasks, large data operations, memory requirements are very strict application scenarios. If you want to implement these features, it is also recommended that you implement them in an extended manner, and then provide a hook function for PHP invocation. This reduces the cost of the internal calculation of variables, functions, and other series.

3. Benchmark Performance

For PHP benchmark performance, there is currently a lack of standard data. Most of the students have a perceptual understanding, some people think that 800QPS is the limit of PHP. In addition, there is no authoritative number that responds to the performance of the framework and the impact of the framework on performance.

The purpose of this chapter is to give a reference to the benchmark performance indicators, through the data to give you an intuitive understanding.

There are several aspects to the specific benchmark performance:

1. Bare PHP performance. Complete the basic functions.

2. The performance of the bare frame. Only the simplest route distribution, only pass through the core functions.

3. Benchmark performance of standard modules. The benchmark performance of a standard module refers to a benchmark performance with a full service module function.

3.1 Environment Description

Test environment:

Uname-a

Linux db-forum-test17.db01.baidu.com 2.6.9_5-7-0-0 #1 SMP Wed Aug 17:35:51 CST 2009 x86_64 x86_64 x86_64 gnu/linux

Red Hat Enterprise Linux as Release 4 (Nahant Update 3)

8 Intel (R) Xeon (r) CPU E5520 @ 2.27GHz

Software Related:

Nginx:

Nginx version:nginx/0.8.54 built by GCC 3.4.5 20051201 (Red Hat 3.4.5-2)

PHP5: (using PHP-FPM)

PHP 5.2.8 (CLI) (Built:mar 6 2011 17:16:18)

Copyright (c) 1997-2008 the PHP Group

Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies

With Eaccelerator v0.9.5.3, the Copyright (c) 2004-2006 eaccelerator, by Eaccelerator

Bingo2:

PHP Framework.

Other Notes:

How the target machine is deployed: script.

Test the pressure machine and target machine to deploy independently.

3.2 Bare PHP Performance

The simplest PHP script.

    1. 		<?php
    2. Require_once './actions/indexaction.php ';
    3. $objAction = new Indexaction ();
    4. $objAction->init ();
    5. $objAction->execute ();
    6. ?>
    7. The code inside the acitons/indexaction.php is as follows
    8. <?php
    9. Class Indexaction
    10. {
    11. Public Function Execute ()
    12. {
    13. Echo ' Hello, world! ';
    14. }
    15. }
    16. ?>

The results of the test by the pressure tool are as follows:

3.3 Bare PHP Frame performance

For comparison with 3.2, a similar function is implemented based on the BINGO2 framework. The code is as follows

    1. 		<?php
    2. Require_once ' bingo/controller/front.php ';
    3. $objFrontController = bingo_controller_front::getinstance (Array (
    4. ' Actiondir ' => './actions ',
    5. ));
    6. $objFrontController->dispatch ();

The results of the stress test are as follows:

From this test results can be seen: Although the framework has a certain consumption, but the overall performance of the impact is very small.

3.4 Benchmark performance for standard PHP modules

The so-called standard PHP module, refers to a PHP module must be specific to the basic functions:

Route distribution.

Automatically loaded.

Log initialization &notice Journal printing. So the UI request is a standard log.

    • Error handling.
    • Time correction.
    • Automatically calculates the time-consuming cost of each phase.
    • Coding Recognition & Coding transformation.
    • Parsing and invocation of standard configuration files

Generate standard test PHP modules using Bingo2 's code generation tool: Test.

The test results are as follows:

3.5 Conclusion

From the conclusion of the test data, the performance of PHP itself is still possible. Benchmark performance is fully capable of reaching thousands of or even upper W QPS. As for why in most of the PHP module performance is not good, in fact this time should go to find the bottleneck of the system, but simply say ok,php not, then we change C to do it. (In the next section, there are some examples to compare, and using C to deal with it doesn't necessarily have a particular advantage.)

Through the benchmark data, we can draw the following concrete conclusions:

1.PHP itself is also very good performance. Simple function can reach 5000QPS, the limit can also be over W.

The 2.PHP framework itself has very limited performance impact. In particular, there is a certain business logic and data interaction in the case, can almost ignore.

3. A standard PHP module, the benchmark performance can reach 2000QPS (CPU idle).

4. Comparative analysis

Many times, we found that the PHP module performance is not good time, just to a "OK, we use C rewrite it." Within the company, the use of C/C + + to write business logic module is everywhere, in the last few years or even almost all of them are written with the use of At that time, everyone wrote really a pain: debugging difficult, agile do not talk.

Article from: baidu-tech.com



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.