"Editor 's note" Previously, read a lot about PHP performance analysis of the article, but it is written in a single rule, and the rules are not context, there is no clear experiment to reflect the advantages of these rules, while the discussion also focuses on some grammatical points. This article changes the PHP performance analysis angle, and through the example to analyze the PHP performance aspect needs to pay attention and the improvement point.
The analysis of PHP performance, we start from two levels, this article also divided into two parts, one is the macro level, the so-called macro level, is the PHP language itself and the environmental level, one is the application level, is the grammar and the use of the rules of the level, but not only to explore the rules, more aid to the analysis of examples.
Macro-level, that is, the PHP language itself performance analysis is divided into three aspects:
PHP is a natural flaw in interpretative language performance
PHP as a dynamic type language also has a space for improvement in performance
The current mainstream PHP version itself language engine performance
The performance analysis and promotion of PHP as an explanatory language
PHP, as a scripting language and an interpretive language, is the reason for its natural performance limitations, because unlike compiled languages, which are compiled into binary code before running, the explanatory language faces the input, parsing, compiling, and execution of the original script for each run. The following is the execution process of PHP as an explanatory language.
As shown above, you can see from the above figure that every time you run, you need to go through three parsing, compiling, and running three processes.
So where is the point of optimization? It can be expected that as long as the code file is determined, parsing to the compile step is OK, because the file is no longer changing and execution is different depending on the input parameters. In the performance optimization of the world, the first trick is to get the same results, reduce the operation, this is the famous cache. Caching is ubiquitous, and caching is the killer of performance optimizations. As a result OpCode cache this trick appears, only the first need to parse and compile, and in the subsequent implementation, directly from the script to OpCode, so as to achieve the performance speed. The execution process is shown in the following illustration:
Relative to each parse, compile, read the script, directly from the cache to read bytecode efficiency will have a significant increase in the extent of the increase in the end how much?
Let's do an experiment without Opcode caching. 20 concurrent, with a total of 10,000 requests without a opcode cache, the following results are obtained:
Second, we open the Opcode cache on the server. To implement opcode caching, you only need to install APC, Zend Opcache, eaccelerator extensions, and only one of them is enabled, even if multiple installations are installed. Note that after you modify the php.ini configuration, you need to reload the PHP-FPM configuration.
Here we enable APC and Zend Opcache to do experiments separately. Enable the version of APC.
You can see that the speed has a larger increase, the original 110ms per request, processing requests per second 182, enabled the APC 68ms, processing requests per second 294, lifting speed of nearly 40%.
In the version with Zend Opcache enabled, the results are roughly equivalent to the APC. 291 processing Requests per second, 68.5ms per request.
From the above experiment, you can see that the test pages used have more than 40ms of time spent on parsing and compiling these two items. By caching These two operations, you can greatly increase the speed of this process.
Here to add, OpCode in the end is what Dongdong, OpCode compiled bytecode, we can use the tools such as Bytekit, or use the VLD PHP extension to achieve PHP code compilation. The following is the result of the VLD plug-in parsing code.
You can see that each line of code is compiled into the corresponding OpCode output.