PHP Performance Exploration and testing _php tutorials

Source: Internet
Author: User
Tags benchmark php language php framework
   1. Origins

With regard to PHP, many people's intuitive feeling is that PHP is a flexible scripting language, library class rich, easy to use, secure, very suitable for web development, but poor performance. is PHP's performance really as bad as everyone's feeling? This article is about such a topic to discuss. From the source code, application scenarios, benchmark performance, comparative analysis and other aspects of the performance of PHP in-depth analysis, and through real data to speak.

  2. Analyzing PHP performance from the principle

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

  2.1 Memory Management

Like Nginx memory management, PHP is internally based on a memory pool and introduces the life cycle concept of a memory pool. In the memory pool, PHP hosts all the memory-related operations of PHP scripts and extensions. The management of large memory and small memory adopts different implementation methods and optimizations, and can refer to the following documents: HTTPS://WIKI.PHP.NET/INTERNALS/ZEND_MM. During the memory allocation and recycling lifecycle, PHP takes a single initialization request + dynamic expansion + memory identity recovery mechanism and masks the memory pool directly at the end of each request.

  2.2 Variables

As is known to all, PHP is a weakly variable type of language, so inside PHP, all PHP variables correspond to a type of zval, which is specifically 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 terms of arrays, PHP uses an efficient hashtable to implement it internally.

  2.3 Functions

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

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

After the internal expansion, it will be a function

void Zif_my_function (internal_function_parameters);

void Zif_my_function (

int HT,

Zval * Return_value,

Zval * This_ptr,

int return_value_used,

Zend_executor_globals * executor_globals

);

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

  2.4 Operating mechanism

When it comes to PHP performance, a lot of people will say, "C + + is compiled, Java is semi-compiled, PHP is interpreted." In other words, PHP is the first dynamic parsing and then run the code, so from this point of view, PHP performance is necessarily very poor.

Indeed, the output from a PHP script is really a process of dynamically parsing and re-running the code. Specifically, the PHP script works as shown in the following:

Figure II PHP operating mechanism

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

      Parse. Syntax Analysis phase.

      Compile. Compile output opcode middle code.

      Execute. Run, and the output is run dynamically.

So, within PHP itself, there is the process of compiling. A number of opcode cache tools have been generated, such as APC, EACC, XCache, and so on. These opcode caches are basically standard in production environments. Based on the opcode cache, it is possible to achieve the "PHP script compile once, run multiple times" effect. From this point on, PHP is very similar to Java's semi-compiling mechanism.

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

  2.5 Dynamic operation

From the above analysis, PHP in memory management, variables, functions, operating mechanism, and so on a lot of work, so from the principle of view,PHP should not have performance problems, performance should be at least as close as Java .

At this point, we have to talk about the performance of the PHP Dynamic language features, because PHP is a dynamic runtime, so all variables, functions, object invocation, scope implementation, etc. are determined during the execution phase. This fundamentally determines the PHP performance is very difficult to change something: in C + + etc can be determined in the static compilation of variables, functions, in PHP needs to be determined in the dynamic operation, it is decided that the PHP intermediate 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 in one of the PHP souls, widely used within PHP, including the variable symbol stack, function symbol stack and so on are based on Hashtable.

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

  

$var = "Hello, blog.xiuwz.com";

?>

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

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

Similarly for function calls there is basically a function symbol stack (hashtable).

In fact, on the dynamic operation of variable search features, in the PHP operating mechanism can also be seen some. The PHP code is interpreted and compiled by the following process:

Figure 3 PHP Running Instance

As can be seen, PHP code after compile, the output of the class symbol table, function symbol table, and opcode. In the real execution, Zend engine will be based on the OP code to the corresponding symbol table to find, processing.

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. But at home and abroad there are many people looking for a solution. 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 basic memory management, variables, functions, operating mechanisms, PHP itself does not have significant performance differences, but due to the dynamic nature of PHP, PHP and other compiled language, compared to all the variables to find, Functions run and so on are more hash lookup CPU overhead and additional memory overhead, as to how much of this overhead can be derived from subsequent benchmark performance and comparative analysis.

As a result, there are some scenarios in which PHP is not suitable: a large number of computational tasks, the operation of large data volumes, and the application scenarios where memory requirements are very stringent. If you want to implement these functions, it is also recommended to extend the implementation, and then provide the hook function to the PHP call. This reduces the series overhead of internal computing variables, functions, and so on.

  3. Benchmark Performance

For PHP benchmark performance, there is currently a lack of standard data. Most of the students have perceptual understanding, some people think that 800QPS is the limit of PHP. In addition, there are authoritative numbers that are not responsive 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.

The specific benchmark performance has the following aspects:

1. Bare PHP performance. Complete the basic functionality.

2. Performance of bare frame. Only the simplest route distribution, only the core function.

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

  3.1 Environmental Description

Test environment:

Uname-apnux db-forum-test17.db01.baidu.com 2.6.9_5-7-0-0 #1 SMP Wed 17:35:51 CST x86_64 x86_64 x86_64 GNU/ Pnux

Red Hat Enterprise Pnux 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: (with PHP-FPM)

PHP 5.2.8 (CP) (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, Copyright (c) 2004-2006 eaccelerator, by Eaccelerator

Bingo2:

PHP Framework.

Other Notes:

How the target machine is deployed: nginx->php-fpm->php script.

Test the pressure machine and target machine to deploy independently.

  3.2 Bare PHP Performance

The simplest PHP script.

  

Require_once './actions/indexaction.php ';

$objAction = new Indexaction ();

$objAction->init ();

$objAction->execute ();

?>

The code inside the acitons/indexaction.php is as follows

  

Class Indexaction

{

PUBPC function Execute ()

{

Echo ' Hello, world! ';

}

}

?>

The test results through the pressure tool are as follows:

  3.3 Bare PHP Framework Performance

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

  

Require_once ' bingo/controller/front.php ';

$objFrontController = bingo_controller_front::getinstance (Array (

' Actiondir ' = './actions ',

));

$objFrontController->dispatch ();

The results of the stress test are as follows:

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

  Benchmark performance of 3.4 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 time-consuming overhead for each stage.

      Code Identification & encoding conversion.

      Parsing and invocation of standard configuration files

Use the Bingo2 code generation tool to produce the standard test PHP module: 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. The benchmark performance is fully capable of achieving a thousands of or even upper w of QPS. As for why in most of the PHP module performance is poor, in fact, this time more should be to find the bottleneck point of the system, but simply say ok,php no, then we change C to do it. (The next section will be compared by some examples, using C to deal with not necessarily having a particular advantage.)

The following concrete conclusions can be drawn from the benchmark data:

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

The 2.PHP framework itself has very limited impact on performance. Especially in the case of some business logic and data interaction, it can be almost ignored.

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

  4. Comparative analysis

A lot of times, we found that the PHP module performance is not good, come to a "OK, we use C rewrite it." In the company, a C/C + + to write business logic module phenomenon is everywhere, in previous years and even almost all of them are written in a. It was a pain in the back of the time: Debug hard, Agile don't talk.

http://www.bkjia.com/PHPjc/371766.html www.bkjia.com true http://www.bkjia.com/PHPjc/371766.html techarticle 1. Genesis about PHP, a lot of people's intuitive feeling is that PHP is a flexible scripting language, library classes are rich, easy to use, secure, very suitable for web development, but poor performance. PHP Performance is ...

  • Related Article

    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.