Zend Framework – Simple Caching ‘ab’ Test.

As the new framework, Zend Framework, roles out for the PHP language I was curious how the framework would stack up to a non-structured small application. This article will cover the Zend_Cache module and a quick ab test on a small application that will print out 10 lines using a for loop.

The Zend Framework was released as version 1.0 3 weeks ago by the Zend company, IBM partner. The framework is structured into the popular MVC design pattern and provides support for RSS feeds down to memory management. Here’s the link to download it. http://framework.zend.com

To understand what this article is about we need to understand what caching is about. So what is caching? Caching is a method in which a set of data; html, JavaScript, css, chunks of code to process by php, can be stored in a compiled flat file on disk for later use. Why is this important? The reason caching is important is because as a new request comes in the PHP Zend engine must open up the php file the user requested, compile the php source into intermediate code, execute the code, and then display the page. The process usually take a few milli-seconds depending on what’s happening in the code but as more traffic increases to the site the time will increase as requests increase.

To lower the amount of time a user sits and waits for a page to load the architect will incorporate caching. Caching would eliminate the steps of creating intermediate code and executing the code and simple open up the pre-compiled files and present it to the user.

Using the Zend Framework and the class Zend_Cache a developer can create a set of content to cache, place expiration time on the cached content and load cached data based on a set of tags. I wont be going into much detail on the API of the Zend_Cache class here since Zend has put together a great document on how to use it.

Getting down to business. While consulting I began to wonder how the framework caching methods would compare to a non-cached application. I created two sets of code.


/**
* Simple page that contains no caching and displays 10 records
* from n array.
*
* @author Armando Padilla, mando81@prodigy.net
*/
$records = array('record 1',
'record 2',
'record 3',
'record 4',
'record 5',
'record 6',
'record 7',
'record 8',
'record 9',
'record 10');

//Print out all the information from the array.
foreach($records as $record){

echo $record.”
“;

}//End foreach

?>
The code above doesn’t need an explanation since it’s a simple example ill be using and the comments provide you with enough info as to what its doing. The second code below contains the same code but utilizes the caching class that the Zend Framework uses.


/**
* Simple page that contains caching and displays 10 records
* from n array.
*
* @author Armando Padilla, mando81@prodigy.net
*/
require_once('Zend/Cache.php');

//Set settings for caching of front end data
//FrontEnd Data = html, css, javascript
$frontendSettings = array(‘lifetime’ => 7200, //How long we want to cache the data
‘automatic_serialization’ => true);

$backendSettings = array(‘cache_dir’ => “/tmp”);

//Set up a cache object
$cached = Zend_Cache::factory(‘Output’, ‘File’, $frontendSettings, $backendSettings);

/*****
* If the data has not been cached or if the data is no longer
* valid – Read the array and place the info into the cache
* Otherwise – Print from cache
******/
if(!$htmlToPresent = $cached->start(‘records’)){

$records = array(‘record 1’,
‘record 2’,
‘record 3’,
‘record 4’,
‘record 5’,
‘record 6’,
‘record 7’,
‘record 8’,
‘record 9’,
‘record 10’);

//Print out all the information from the array.
foreach($records as $record){

$data .= $record.”
“;

}//End foreach
echo $data;
$cached->end();
}
}
?>
The code above on the first request to the code will create a cached version of the output and then utilize the cached version of the page for any other request that comes in. On to the testing.

Using the built in apache performance testing tool, “ab”, I ran a quick performance test to test a concurrent 100 request per second for 30 seconds the performance print out Is shown below.

Cached:
Server Software: Apache/2.2.4
Server Hostname: localhost
Server Port: 80

Document Path: /ZendCacheExample/nocache.php
Document Length: 137 bytes

Concurrency Level: 100
Time taken for tests: 30.044 seconds
Complete requests: 8600
Failed requests: 0
Broken pipe errors: 0
Keep-Alive requests: 8584
Total transferred: 3078482 bytes
HTML transferred: 1178337 bytes
Requests per second: 286.25 [#/sec] (mean)
Time per request: 349.35 [ms] (mean)
Time per request: 3.49 [ms] (mean, across all concurrent requests)
Transfer rate: 102.47 [Kbytes/sec] received

No Cache
Server Software: Apache/2.2.4
Server Hostname: localhost
Server Port: 80

Document Path: /ZendCacheExample/nocache.php
Document Length: 157 bytes

Concurrency Level: 100
Time taken for tests: 15.412 seconds
Complete requests: 50000
Failed requests: 0
Broken pipe errors: 0
Keep-Alive requests: 49617
Total transferred: 18905608 bytes
HTML transferred: 7860519 bytes
Requests per second: 3244.23 [#/sec] (mean)
Time per request: 30.82 [ms] (mean)
Time per request: 0.31 [ms] (mean, across all concurrent requests)
Transfer rate: 1226.68 [Kbytes/sec] received

The important lines we can look at are the bold items. What these lines indicate are the total number of request per second the system can withstand under such conditions and the amount of time the user will take to see something come up on the screen when there is such a load on the system.

In an ideal situation the Request per second and the Time Per Request will have a lower number as possible. Looking at the numbers the cached version of the code has a higher Rest per Second and Time per request number than the Non-cached version of the code. What does this mean? It means that I either did something wrong or caching in the Zend Framework does not benefit the developer.

Concluding, comparing the two I thought coming into this that the cached version of the code would be much lower than the non-cached version of the code. The reason for this is because the engine does not have to take the time to compile the array and loop through the content. Unfortunately it didn’t turn out that way and im left with more questions than answers. For one, did I do something wrong? Is it possible that caching works best under larger applications?

Well see.

Armando Padilla

Add a Comment

Your email address will not be published. Required fields are marked *