Jul
14

php extension xend

php       Share This    Trackback
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4 out of 5)
Loading ... Loading ...

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

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists

13 Comments

Make A Comment

Comments RSS Feed   TrackBack URL

Leave a comment

top
Close
E-mail It