PHP Archive Phar Performance test

Source: Internet
Author: User
Tags apc intel core i7

PHP since 5.3 New Phar archive, the concept of Phar archive from Java? Technology's JAR archive, which allows you to package your application with a single file that contains everything you need to run your 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 with security in mind, these files are not carefully differentiated. The Phar extension is based on a similar concept, but is designed primarily for PHP's WEB environment. Similarly, unlike JAR Archives, Phar archives can be processed by PHP itself, so there is no need to use additional tools to create or use them. The Phar extension is not a fresh concept for PHP. It was originally written in PHP and named Php_archive, and was added to the PEAR Library in 2005. In practice, however, the pure PHP solution to this problem was very slow, so the 2007 was rewritten as a pure C language extension, with the addition of support for Arrayaccess objects that use SPL to traverse the Phar archive. Since then, a lot of work has been done to improve the performance of Phar archiving, which is currently very limited for Phar, and there is little performance testing on Phar, Phar performance, through a simple experimental test.

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: How much does the file size affect?
The impact of 1.2:include/require?
What is the impact of 1.3:phar stub content?
2:phar Code Execution Speed
2.1 Global Function comparison
Class 2.2 Objects
Class 2.3 Methods
To ensure that the test is as accurate as possible, run 3 times per way, averaging 3 times. At the same time, as a comparison, we will directly use code to obtain the benchmark data.
Phar file mainly contains files


Phar-builder.php is used to generate the Phar file, execute the test command before executing this file to generate the Phar-test.phar file.
test_load.php Test load Phar File speed
The SRC directory contains file index.php files that are stub files that contain Dates.php,for.php,functions.php,dates test file class methods, for.php test object methods, functions.php test function methods.
The specific attachment code.
First: Phar loading speed, using include and require method test found that the difference is not small, only the Require method.

$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 load 4.7 ms

Compare the direct source code reference method.

$stime = Microtime (true); require './src/index.php '; $etime = Microtime (true); $total = $etime-$stime; echo "src total:". $t Otal. " 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 load 2.6 ms
Conclusion: By loading speed comparison, the Phar Loading mode is much slower than the direct file loading mode, and it consumes twice times more time to directly reference the file. At the same time I loaded some interference files in the Phar file, make the Phar file larger, found within 10k, this load time changes little. Of course, I did not put the new files into the stub, for the purpose of more than 10K directory, the way the file is organized, such as the AutoLoad way, and not through a file containing all the files. Phar Load time is about 1.8 times times the direct loading of SRC.

Second: Execution Speed test
Phar Mode, 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;    $total 2 = $eetime-$sstime;    echo "Phar Load Total:". $total. " S\n ";    echo "Phar Execution 10000 total:". $total 2. " S ";
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
Executes 10,000 classes of methods, object instances and object methods, and function methods, with a total time consumption of 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
executes 10,000 classes of methods, object instances and object methods, and function methods, with a total time consumption of 0.20 milliseconds.
Summary: Through the execution speed comparison, found that is the Phar way, execution speed, than the direct file include way, faster (0.20-0.17)/0.20*100=15%,phar way to execute the specific reasons for fast not found, on-line information, Apc+include_ The path setting Phar executes very fast. https://github.com/ralphschindler/test-phar-performance-apc/.

Summary: PHP archive Phar Mode, loading speed is slower than the normal file inclusion method, but the execution speed is higher than the file contains the way, if with include_path settings and APC or OP mode, optimize Phar Archive loading speed, can improve the speed of PHP execution. The next step will be to try, 1: Build a large Phar file, experiment with loading speed, execution speed. 2: Understand Phar Loading principle and implementation principle, 3: Package concept management and dependency.


Some other references
New features in PHP V5.3, creating and using Phar Archive. 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.