Test Results on Memory Usage of Zend Framework and Doctrine with APC

October 10th, 2009 by rvdavid Leave a reply »

After investigating a recommendation to use Doctrine by a fellow blogger, Brian at Real of Zod, I have decided to run with Doctrine as my Domain Model in Zend Framework projects. The thing is, if I’m going to commit to this, I need to know that applications I build in the future with the Zend Framework while using Doctrine as an integral part of the Model layer will not take performance hits from things like memory usage.

With Doctrine doing a _lot_ of magic, I thought that this would be something that I wanted to see for myself.

4MB Memory to execute a simple Query?!?! Ffffff#$#!!!!

A quick google search took me to a Question posted on StackOverflow about Doctrine Memory Usage. The concerned OP was asking if he had a server misconfiguration or if this was normal for Doctrine to be using so much memory for a simple query. He posted a 4MB difference in Peak Memory Usage between the start of the request before the Doctrine Query was executed and after the Doctrine Query was executed. After reading that, I was a little nervous.

Use Opcode Caching to reduce Memory Usage.

There were a couple of answers and comments one of the answers came from Doctrine’s own romanb (I think?) specifying that you _need_ opcode caching if you’re going to be using something the size of Doctrine (and Zend Framework for that matter).

Similar to what the OP at StackOverflow did, I created a simple test which measures peak memory usage when using Doctrine from within the Zend Framework.

Be warned however, that this is not a real benchmark. All this is testing is what memory usage is using the simplest of simplest operations. In this case, I created a query which pulls one record from the Database.

Here are the details.

The Controller I used for this quick test:

<?php

class IndexController
extends Zend_Controller_Action
{
// with doctrine set up and everything
/**
*
* @return unknown_type
*/
public function memoryUsageTestAction()
{
$this->_echo_memory_usage();
$q = Doctrine_Query::create()
->from(’Default_Model_User u’)
->where(’u.ID = ?’, 1);

$result = $q->fetchOne();
echo $result['FirstName'] . ‘ ‘ .$result['LastName']. ‘<br />’;

$this->_echo_memory_usage();
exit;
}

/**
* Function from: http://www.php.net/manual/en/function.memory-get-usage.php
* @return unknown_type
*/
protected function _echo_memory_usage() {
$mem_usage = memory_get_usage(true);

if ($mem_usage < 1024)
echo $mem_usage.” bytes”;
elseif ($mem_usage < 1048576)
echo round($mem_usage/1024,2).” kilobytes”;
else
echo round($mem_usage/1048576,2).” megabytes”;
echo “<br/>”;
}
}

Result without APC:

10.25 megabytes
RV David
16.5 megabytes

Result with APC:

3 megabytes
RV David
4.25 megabytes

This is with the following APC configuration:

apc.shm_size = 30
apc.ttl = 7200
apc.user_ttl = 7200
apc.mmap_file_mask = /tmp/apc.XXXXXX

apc.shm_segments = 1
apc.optimization = 0
apc.num_files_hint = 1000

As you can see, there’s a big difference – chopped ZF Memory Peak Usage (my configuration anyway) by about 65%-70% and Peak Usage with a Doctrine call to the Database by about 75%.

Advertisement

Leave a Reply