PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Performance optimization

Example 1076. Cache server definitions between requests

The example below illustrates caching server definitions between requests.

<?php
// Specify a cache file
$cacheFile dirname(__FILE__) . '/xmlrpc.cache';

// Allow Services_Exceptions to report as fault responses
Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');

$server = new Zend_XmlRpc_Server();

// Attempt to retrieve server definition from cache
if (!Zend_XmlRpc_Server_Cache::get($cacheFile$server)) {
    
$server->setClass('Services_Comb''comb');   // methods called as comb.*
    
$server->setClass('Services_Brush''brush'); // methods called as brush.*
    
$server->setClass('Services_Pick''pick');   // methods called as pick.*

    // Save cache
    
Zend_XmlRpc_Server_Cache::save($cacheFile$server);
}

// Create a request object
$request = new Services_Request();

// Utilize a custom response
$server->setResponseClass('Services_Response');

echo 
$server->handle($request);

Note

The server cache file should be located outside the document root.

Example 1077. Optimizing XML generation

Zend_XmlRpc_Server uses DOMDocument of PHP extension ext/dom to generate it's XML output. While ext/dom is available on a lot of hosts it is not exactly the fastest. Benchmarks have shown, that XmlWriter from ext/xmlwriter performs better.

If ext/xmlwriter is available on your host, you can select a the XmlWriter-based generator to leaverage the performance differences.

<?php
require_once 'Zend/XmlRpc/Server.php';
require_once 
'Zend/XmlRpc/Generator/XmlWriter.php';

Zend_XmlRpc_Value::setGenerator(new Zend_XmlRpc_Generator_XmlWriter());

$server = new Zend_XmlRpc_Server();
...

Benchmark your application

Performance is determined by a lot of parameters and benchmarks only apply for the specific test case. Differences come from PHP version, installed extensions, webserver and operating system just to name a few. Please make sure to benchmark your application on your own and decide which generator to use based on your numbers.

Benchmark your client

This optimization makes sense for the client side too. Just select the alternate XML generator before doing any work with Zend_XmlRpc_Client.

Zend Framework