PHP Archive Phar Performance test

Source: Internet
Author: User
Tags apc execution file size new features php file php and stub intel core i7
PHP since the 5.3 New Phar Archive, Phar archive concept from Java? Technology's JAR archive, which allows you to package your application with a single file containing everything you need to run your application. This file differs from a single executable file, which is typically generated by the 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, the 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 a JAR archive, the Phar archive can be handled by PHP itself, so there is no need to use additional tools to create or use it. Phar extensions are not a new concept for PHP. It was originally written in PHP and named Php_archive, and then added to the PEAR Library in 2005. In practice, however, the pure PHP solution to this problem is very slow, so it was rewritten as a pure C language extension in 2007, while adding support for traversing the Phar archive with Arrayaccess objects using SPL. Since then, a great deal of work has been done to improve the performance of the Phar Archive, which is currently very limited to Phar, and Phar performance tests are rare, in the end Phar performance, through a simple 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 effect of 1.2:include/require?
Effect of 1.3:phar stub content?
2:phar Code Execution Speed
2.1 Global Function comparison
2.2 Class Objects
2.3 Class methods
To ensure that the test is as accurate as possible, run each way 3 times, to 3 times the average. As a contrast, we'll get the benchmark data directly using the code.
Phar files primarily include files


Phar-builder.php is used to generate the Phar file, which is the first to execute the Phar-test.phar file before executing the test command.
test_load.php Test load Phar File speed
The SRC directory contains the file index.php file is a stub file, including Dates.php,fz yo? http://www.2cto.com/kf/ware/vc/"target=" _blank "" class= "> vci5wahasznvuy3rpb25zlnbockoszgf0zxoy4sruzss8/sdgt723qkoszm9ylnboclliyts21m/ zt723qkosznvuy3rpb25zlnboclliyts6r8r9t723qkgjpgjyic8+vt/m5bi9vp60+ slroam8yniglz612tk7o7pwagfyvnpu2mvztsijrllj08npbmnsdwrlus1yzxf1axjlt73kvbliyts3os/ Wsu7s7lk7tpojrna7ssntw3jlcxvpcmw3vcq9oam8yniglz48l3a+phbyzsbjbgfzcz0= "Brush:java;" > $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$ test_phar_load.php php Phar 
. 0043849945068359s
Localhost:phar ugg$ vim test_phar_load.php

Load average 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.0026230812072754s
localhost:phar ugg$ php test_src_ LOAD.PHPSRC 
total:0.0026969909667969s
localhost:phar ugg$ php test_src_load.phpsrc 
total:0. 0025439262390137s

Load average 2.6 milliseconds
Conclusion: By loading speed comparison, the Phar Loading mode is slower than the direct file loading mode, and almost twice times the time consumed by referencing files directly. At the same time I also loaded some interference files in the Phar file, so that Phar file, found within 10k, this load time changes little. Of course, I did not put the new files in the stub, for the purpose, for more than 10K of directories, file organization, such as the AutoLoad way, and not through a file contains all the files. Phar Loading time is about 1.8 times times the direct loading of SRC.

Second: Execution Speed test
Phar Way, 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.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 
Load total:0.004680871963501s
Phar Execution 10000 total:0.00016689300537109s
The class method that executes 10,000 times, object instance and object method, and function method, total time consumes 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 
Load total:0.0029168128967285s
Phar Execution 10000 total:0.00019478797912598s
The class method that executes 10,000 times, object instance and object method, and function method, total time consumes 0.20 milliseconds.
Summary: Through the execution speed contrast, discovers is the Phar way, executes the speed, to be quicker than the direct file include way, fast (0.20-0.17)/0.20*100=15%,phar way execution Speed specific reason did not find, on the net has the information, Apc+include_ The path setting Phar Fast execution. https://github.com/ralphschindler/test-phar-performance-apc/.

Summary: PHP archive Phar Way, loading speed is slower than the normal file contains way, but the execution speed is higher than the file contains, if with include_path settings and APC or OP way, optimize Phar file loading speed, can improve PHP execution speed. The next step will be to try, 1: Build large Phar File, experiment load 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 Archiving. 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.