PHP archive phar performance test

Source: Internet
Author: User
Tags apc intel core i7
PHP archive phar performance test PHP has added PHAR archive since 5.3. The concept of Phar archive comes from Java? Technical JAR archive, which allows you to use a single file to package an application. This file contains everything required to run the application. This file is different from a single executable file, which is usually generated by a programming language, such as C, because the file is actually an archive file rather than a compiled application. Therefore, the JAR file actually contains the application file PHP archive phar performance test
PHP has added PHAR archive since 5.3. The concept of Phar archive comes from Java? Technical JAR archive, which allows you to use a single file to package an application. This file contains everything required to run the application. This file is different from a single executable file, which is usually generated by a programming language, such as C, because the file is actually an archive file rather than a compiled application. Therefore, the JAR file actually contains the files that constitute the application, but these files are not carefully distinguished for security purposes. Phar extensions are based on similar concepts, but are designed primarily for PHP Web environments. Similarly, unlike JAR archiving, Phar archiving can be processed by PHP itself, so no additional tools are required for creation or use. Phar extension is not a new concept for PHP. It was originally written in PHP and named PHP_Archive. it was added to the PEAR Library in 2005. However, in practice, the pure PHP solution to this problem is very slow. Therefore, in 2007, it was rewritten as a pure C language extension, added support for traversing Phar archives using ArrayAccess objects of SPL. Since then, people have done a lot of work to improve the performance of Phar archiving. At present, Phar is used very limited, and there are few performance tests on Phar. what is the performance of Phar, through a simple experiment.

Test environment:
PHP: 5.5.10
CPU: 2 GHz intel core i7
Mem: 8 GB
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 the Phar Stub (Stub) content?
2: Phar code execution speed
2.1 Comparison of Global functions
2.2 Class objects
2.3 Methods
To ensure test accuracy as much as possible, each method runs three times and average values of three times are eliminated. At the same time, we will directly use the code to obtain the benchmark data.
Phar files mainly contain files


The phar-builder.php is used to generate a phar file that generates a phar-test.phar file before executing the test command.
Test_load.php: test the speed of loading phar files
The src directory contains the file index. the PHP file is a stub file that contains dates. php,. php, functions. php, dates test File Class method,. php testing object method, functions. php function testing method.
The attachment code.
First, the phar loading speed is tested using the include and require methods. we found that there is little difference. only the 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.0044760704040527slocalhost:phar ugg$ php test_phar_load.php phar total:0.0051448345184326slocalhost:phar ugg$ php test_phar_load.php phar total:0.0043849945068359slocalhost:phar ugg$ vim test_phar_load.php

Average loading: 4.7 milliseconds

Compare the direct source code reference method.

$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.0026230812072754slocalhost:phar ugg$ php test_src_load.phpsrc total:0.0026969909667969slocalhost:phar ugg$ php test_src_load.phpsrc total:0.0025439262390137s

Average loading: 2.6 milliseconds
Conclusion: compared with the loading speed, the phar loading method is much slower than the direct file loading method, and it takes almost twice the time to directly reference the file. At the same time, I added some interference files in the phar file to make the phar file larger. I found that the load time was not changed much within 10 kB. Of course, I didn't place the newly added files in the stub. in this way, for directories with more than 10 kB, the file organization method is autoload, instead of using a file to include all files. The phar loading time is about 1.8 times that of src direct loading.

Second, perform a speed test.
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.0047600269317627sphar execution 10000 total:0.00017499923706055slocalhost:phar ugg$ php test_phar_functions.php phar load total:0.004863977432251sphar execution 10000 total:0.00017404556274414slocalhost:phar ugg$ php test_phar_functions.php phar load total:0.004680871963501sphar execution 10000 total:0.00016689300537109s
The total time consumed for executing 10000 class methods, object instances, object methods, and function methods is 0.17 milliseconds.
Src execution efficiency
localhost:phar ugg$ php test_src_functions.php phar load total:0.0029089450836182sphar execution 10000 total:0.00019693374633789slocalhost:phar ugg$ php test_src_functions.php phar load total:0.0028579235076904sphar execution 10000 total:0.0002140998840332slocalhost:phar ugg$ php test_src_functions.php phar load total:0.0029168128967285sphar execution 10000 total:0.00019478797912598s
The total time consumed for executing 10000 class methods, object instances, object methods, and function methods is 0.20 milliseconds.
Conclusion: through the comparison of execution speed, it is found that the execution speed is faster than the direct file include method (0.20-0.17)/0.20*100 = 15%, the specific reason why the phar method is fast is not found. I have some information on the internet. apc + shortde_path sets the phar execution speed very quickly. Https://github.com/ralphschindler/test-phar-performance-apc /.

Summary: The PHP archive phar method is slower to load than the normal file inclusion method, but the execution speed is higher than the file inclusion method. if you use the include_path method and APC or OP method, optimizing the loading speed of phar archives can improve the php execution speed. In the next step, we will try to build a large phar file, experiment loading speed, and execution speed. 2: Understand the phar loading principle and Execution Principle. 3. Manage the package concept and dependencies.


Other references
Create and use Phar archive for new features in PHP V5.3. Http://www.ibm.com/developerworks/cn/opensource/os-php-5.3new4/
Test-phar-performance-apc https://github.com/ralphschindler/test-phar-performance-apc/

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.