php extension xend
I am proud to say, that I wrote my first php extension in c :). What it does it’s pretty simple. For example, take a look at the Zend framework, the first lines of code would properly look like this:
// import zend mvc require_once( 'Zend/Controller/Front.php' );
Lets also take a look at the first lines of ‘Zend/Controller/Front.php’:
<?php /** * Zend Framework * […] */ /** Zend_Loader */ require_once 'Zend/Loader.php'; /** Zend_Controller_Action_HelperBroker */ require_once 'Zend/Controller/Action/HelperBroker.php'; /** Zend_Controller_Action_Helper_ViewRenderer */ require_once 'Zend/Controller/Action/Helper/ViewRenderer.php'; require_once 'Zend/Controller/Exception.php'; require_once 'Zend/Controller/Plugin/Broker.php'; require_once 'Zend/Controller/Request/Abstract.php'; require_once 'Zend/Controller/Router/Interface.php'; require_once 'Zend/Controller/Dispatcher/Interface.php'; require_once 'Zend/Controller/Plugin/ErrorHandler.php'; require_once 'Zend/Controller/Response/Abstract.php'; //...
So you see they include another 10 files, and those files may include others too. The whole process of including files creates some overhead though. For small systems on big machines is not a deal. But for big systems on small machines it could be quite a drawback. Well anyways, you can already imagine what I did.
// import zend mvc require xend( 'Zend/Controller/Front.php' );
Not much different to the previous code, just change require_once(…) to require xend(…) that’s all you need to do. The next time the scripts run, Xend will go thought the whole scripts and bundles them to one big file and saves it under a predefined cache directory (xend.cache_path). Xend will only bundle files together witch are included like
require_once ‘myfile.php’// (no brackets)
and are inside the main scope, so not inside any “{}“ – brackets.
e.g.
<?php // I am getting included by Xend… require_once “some_file.php”; // gets bundled by Xend require_once ‘some_file.php’; // gets bundled by Xend require_once “some_file”.”.php’; // gets bundled by Xend, but an error occurs :( require_once (‘some_file.php’); // gets NOT bundled by Xend include_once (‘some_file.php’); // gets NOT bundled by Xend function hello_world() { require_once “some_file.php”; // gets NOT bundled by Xend } if(true) { require_once “some_file.php”; // gets NOT bundled by Xend } // but... if(true) require_once “some_file.php”; // ...does get bundled by Xend… // …but execution will be wrong since only the first command will be inside the if statement. ?>
Xend takes also care of all the require_once files, so they don’t get included twice even so they were loaded from the cache.
The following functions exist
/** * deletes all files created by xend */ xend_clear_cache(); /** * enables/disables xend, (default enabled, so no need to call that function unless * you want do disable it.) * if you disable it, and call xend(‘myfile.php’), the function will just * return ‘myfile.php’ and requires works as usually * @param bool $enable wether to enable or disable xend */ xend_enable($enable); /** * bundles the given file to one big file, and returns the path to the new file. * if xend is disabled it will return the given file. * @param string $file php file to bundle * @return string path to the file to include */ require xend( $file );
php.ini settings
[xend] xend.cache_path = "/path/to/my/cache"
Some benchmark for the script
<?php header('Content-Type: text/plain; charset=utf-8'); //require_once( 'Zend/Controller/Front.php' ); require xend( 'Zend/Controller/Front.php' ); print_r(get_included_files()); echo "\n\n\n"; echo "total time.......: ", round(xdebug_time_index()*1000000), "\n"; echo "total memory peak: ", memory_get_peak_usage(true), "\n"; echo "total memory.....: ", memory_get_usage(), "\n"; $test = new Zend_View; /************************** // output with Xend total time.......: 1957 total memory peak: 262144 total memory.....: 237796 // output without Xend functions total time.......: 6054 total memory peak: 262144 total memory.....: 240136 */
Apache Benchmark without Xend
Server Software: Apache
Server Hostname: 192.168.2.42
Server Port: 80
Document Path: /
Document Length: 1578 bytes
Concurrency Level: 10
Time taken for tests: 8.438610 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 1733732 bytes
HTML transferred: 1579578 bytes
Requests per second: 118.50 [#/sec] (mean)
Time per request: 84.386 [ms] (mean)
Time per request: 8.439 [ms] (mean, across all concurrent requests)
Transfer rate: 200.63 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 34 23.7 33 107
Processing: 9 48 37.7 50 896
Waiting: 0 37 37.3 34 855
Total: 67 83 29.4 82 912
Percentage of the requests served within a certain time (ms)
50% 82
66% 83
75% 84
80% 84
90% 85
95% 87
98% 115
99% 118
100% 912 (longest request)
Apache Benchmark with Xend
Server Software: Apache
Server Hostname: 192.168.2.42
Server Port: 80
Document Path: /
Document Length: 1672 bytes
Concurrency Level: 10
Time taken for tests: 4.478998 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 1826000 bytes
HTML transferred: 1672000 bytes
Requests per second: 223.26 [#/sec] (mean)
Time per request: 44.790 [ms] (mean)
Time per request: 4.479 [ms] (mean, across all concurrent requests)
Transfer rate: 398.08 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 19 12.7 21 43
Processing: 5 24 12.1 26 48
Waiting: 0 19 12.4 21 43
Total: 42 44 1.1 44 49
Percentage of the requests served within a certain time (ms)
50% 44
66% 44
75% 45
80% 45
90% 45
95% 46
98% 48
99% 48
100% 49 (longest request)
Well I just tested the extension on my system (Suse Linux 10.1 apache2 php 5.2.3). I have no idea if it works on other systems, but since I only used standard functions I don’t think it would cause any troubles.
download xend source file v0.2
13 Comments
Make A CommentComments RSS Feed TrackBack URL

(1 votes, average: 4 out of 5)











September 16th, 2007 at 10:14 am
Sounds great… we need a debian package !
November 28th, 2007 at 2:14 am
PHP5 only I guess ? (ext/hash/php_hash.h is not present on my PHP4 source).
December 8th, 2007 at 12:04 am
great idea! nice job!
December 8th, 2007 at 5:31 am
Excellent work! I second the Debian package request.
January 4th, 2008 at 5:28 pm
great job
thank you, we improved our performance by about 100% with just 3 code lines changed.
January 8th, 2008 at 5:22 pm
just a idea: could it be possible to give xend() more than one file, and xend adds to file1 the second file and if given the third file and so on… so it would be possible to include a list of files with just one include and xend makes one file out of the given ones
ng
ps.: sorry for my bad english, i hope u understand me
May 7th, 2008 at 3:58 am
Hey, sorry I am traveling around right now which makes it hart for me to change anything, but yea ur idea is good and ill can give it a try oneday. cheers 4 that and for the credits
Jacob
May 10th, 2008 at 10:32 pm
hoe can I install and enable yr module
thx
Jari
August 5th, 2008 at 2:52 pm
The same, how to install your module?
When I copy the code in php-5.2.6/ext/xend/ and configure with –enable-xend as param in configure command line:
./configure [other modules…] –enable-xend
it is not workign for me
November 5th, 2008 at 9:06 am
I am trying to get your module to load on Ubuntu but can’t get it to work, can you help me?
January 3rd, 2009 at 4:23 pm
This is probably a weird sounding request, but has anybody compiled this extension as a (Windows-)dll yet?
If so, could somebody post it?
May 25th, 2009 at 9:06 am
sefggdgdfg
August 4th, 2009 at 7:14 pm
before putting this into any distro please check the source code - I think it has a security problem