Home > Article > Backend Development > Performance testing of PHP archive phar
This article mainly introduces the performance test of PHP archive phar, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
PHP has added PHAR archives since 5.3. The concept of Phar archives comes from Java™ technology's JAR archive, which allows applications to be packaged with a single file that contains everything needed to run the application. This file differs from a single executable file, which is typically generated by a programming language such as C, because the file is actually an archive file rather than a compiled application. So the JAR file actually contains the files that make up the application, but these files are not carefully differentiated for security reasons. The Phar extension is based on a similar concept, but is designed primarily for PHP's web environment. Also, unlike JAR archives, Phar archives can be processed by PHP itself, so no additional tools are required to create or use them. Phar extensions are not a new concept to PHP. It was originally written in PHP and named PHP_Archive before being added to the PEAR library in 2005. In practice, however, pure PHP solutions to this problem were very slow, so in 2007 it was rewritten as a pure C language extension and support for traversing Phar archives using SPL's ArrayAccess objects was added. Since then, people have done a lot of work to improve the performance of Phar archiving. Currently, the use of Phar is very limited, and there are very few performance tests on Phar. How good is the performance of Phar? Let's test it through a simple experiment.
Test environment:
PHP: 5.5.10
CPU: 2GHz intel core i7
Mem: 8GB
System: Darwin 13.1.0
Main test points:
1: Phar loading speed
1.1: What is the impact of file size?
1.2: Impact of include/require?
1.3: What is the impact of Phar stub content?
2: Phar code execution speed
2.1 Global function comparison
2.2 Class object
2.3 Class method
In order to ensure that the test is as accurate as possible, each method is run 3 times, and the average of the 3 times is value. At the same time, for comparison, we will directly use code to obtain benchmark data.
The Phar file mainly contains the file
test_load.php Tests the speed of loading phar files
The index.php file contained in the src directory is a stub file, including dates.php, for.php, functions.php, dates test file class method, for.php test Object methods, functions.php test function methods.
Specific attachment code.
First: phar loading speed, test using include and require methods and found that there is not much difference, only require method is used.
$stime = microtime(true); require './phar-test.phar'; $etime = microtime(true); $total = $etime - $stime; echo "phar total:".$total."s";After execution, the efficiency is as follows
localhost:phar ugg$ php test_phar_load.php phar total:0.0044760704040527s localhost:phar ugg$ php test_phar_load.php phar total:0.0051448345184326s localhost:phar ugg$ php test_phar_load.php phar total:0.0043849945068359s localhost:phar ugg$ vim test_phar_load.php
The average loading is 4.7 milliseconds
$stime = microtime(true); require './src/index.php'; $etime = microtime(true); $total = $etime - $stime; echo "src total:".$total."s\n";After execution, the efficiency is as follows
localhost:phar ugg$ php test_src_load.phpsrc total:0.0026230812072754s localhost:phar ugg$ php test_src_load.phpsrc total:0.0026969909667969s localhost:phar ugg$ php test_src_load.phpsrc total:0.0025439262390137s
The average loading is 2.6 millisecondsConclusion: Through the comparison of loading speed, the phar loading method is better than the direct file loading method It's a lot slower, almost twice as long as directly referencing the file. At the same time, I loaded some interference files into the phar file to make the phar file larger. I found that within 10k, the load time did not change much. Of course, I did not put the newly added files into the stub. The purpose of this is that for directories exceeding 10K, the file organization method is such as autoload, and not all files are included in one file. The phar loading time is about 1.8 times that of src direct loading.
Second: Execution speed testphar method, the code is as follows
$stime = microtime(true); //require 'phar://phar-test.phar'; require 'phar-test.phar'; $sstime = microtime(true); for($i = 0; $i<10000; ++$i){ $date = dates::next_week(); $for = new fortest(); $i = $for->for1to10000(); $number = number2Chinese('12345'); } $eetime = microtime(true); $etime = microtime(true); $total = $etime - $stime; $total2 = $eetime - $sstime; echo "phar load total:".$total."s\n"; echo "phar execution 10000 total:".$total2."s";The execution efficiency is as follows
localhost:phar ugg$ php test_phar_functions.php phar load total:0.0047600269317627s phar execution 10000 total:0.00017499923706055s localhost:phar ugg$ php test_phar_functions.php phar load total:0.004863977432251s phar execution 10000 total:0.00017404556274414s localhost:phar ugg$ php test_phar_functions.php phar load total:0.004680871963501s phar execution 10000 total:0.00016689300537109sThe total time consumed for executing class methods, object instances, object methods, and function methods 10,000 times is 0.17 milliseconds.
src execution efficiency
localhost:phar ugg$ php test_src_functions.php phar load total:0.0029089450836182s phar execution 10000 total:0.00019693374633789s localhost:phar ugg$ php test_src_functions.php phar load total:0.0028579235076904s phar execution 10000 total:0.0002140998840332s localhost:phar ugg$ php test_src_functions.php phar load total:0.0029168128967285s phar execution 10000 total:0.00019478797912598s
The total time consumption of executing 10,000 class methods, object instances, object methods, and function methods is 0.20 milliseconds. Summary: Through the comparison of execution speed, it is found that the execution speed of phar method is faster than the direct file include method (0.20-0.17)/0.20*100=15%. The specific execution speed of phar method is faster. The reason has not been found. There is some information on the Internet. The execution speed of phar is very fast when apc include_path is set. https://github.com/ralphschindler/test-phar-performance-apc/.
Summary: PHP archiving phar method, the loading speed is slower than the normal file inclusion method, but the execution speed is higher than the file inclusion method. If you cooperate with the include_path setting and APC or OP method to optimize the loading speed of phar archive, it can be improved. php execution speed. The next step will be to make some attempts, 1: Build a large phar file, and experiment with loading speed and execution speed. 2: Understand the phar loading principle and execution principle, 3: Package concept management and dependencies.
The above is the detailed content of Performance testing of PHP archive phar. For more information, please follow other related articles on the PHP Chinese website!