Dhawal
Dhawal

Reputation: 23

Symfony2 Templating renderResponse causing CPU usage to shoot up

I have a job that loops through the users and sends a personalized email based on their preferences. I am using the Templating engine in Symfony2 to generate the HTML.

Basically something like this runs in a loop:

public function getHTML($user) 
{
    $templating = $this->getContainer()->get('templating');
    $html = $templating->renderResponse( 
                'recommendation.html.twig',
                 array(
                   'user' => $user
                 )
             )->getContent();
    return $html;
}

The problem is CPU usage shoots up to 100% and it takes longer and longer to process each user. Here is some data:

Took 5.4851469993591 seconds 
Took 12.790720939636 seconds 
Took 23.007503032684 seconds 
Took 34.852293014526 seconds 
Took 43.901736021042 seconds 
Took 66.028288125992 seconds 

Each line represents the time taken to process 100 users. So the first 100 users took 5 seconds, next 12 seconds, and so on.

Any suggestions on what I can do to improve performance?

Upvotes: 2

Views: 129

Answers (1)

Richard
Richard

Reputation: 4129

It'll likely be doctrine accumulating a massive identity map of managed entities as you fetch them.

In each iteration of your email send, look at doing an entity manager clear or detach

Best to use this in combination with an iterated result, there's a good example in the documentation.

It can make a huge difference to speed and memory consumption for jobs that do a large amount of iterations.

Doctrine isn't generally great for long running scripts without a bit of work.

Upvotes: 2

Related Questions