I’m running an Amazon EC2 ‘Large’ instance – Ubuntu Natty x64 with PHP5 and MySQL. We execute a PHP script via CRON – this sends an email list (2000-4000 emails) using SMTP/PHPMailer.
The server runs very slowly (several of these CRON jobs run in parallel) and it’s making the CPU go to 100%. Memory usage is low (only ~600mb / 8gigs used) and each CRON job takes a significant chunk of CPU%, for example 20-30% each with 4-5 running in parallel.
Trying to pinpoint the issue, I ran slow query log in MySQL but nothing caught my attention. How should I go about narrowing down the cause of this CPU usage? Is SMTP/email just that CPU intensive or is it a sign that there is a programming or server issue? Thanks!

EDIT: The issue is resolved. There was a trivial (of course) bug that caused emails to ‘grow’ (some of the previous email content was being injected into the next email) – so the email pre-processing got more and more ridiculous with each subscriber. The resulting emails had hundreds/thousands of tracking images which all hit our server simultaneously when opened i.e. ‘display images’ in gmail. After fending off the self-inflicted DDoS attack and two days of no sleep, I am going to enjoy a bottle of Captain Morgan while contemplating various choices I’ve made in life.
Things that can cause this (Non-exhaustive list):
Non block IO with the SMTP server.
Implementation of SMTP library used in php with long strings manipulation/long files encoded every loop (remember: the protocol must be corrected formatted, and this is checked/encoded every time you call the send method by many other methods).
One (or more) query per mail.
Try measuring the time spent on each operation performed inside the loop.
You can use a simple
$start = microtime (true)andprintf (___FILE__.':'.__LINE__.": here after % 0.8f seconds\n", microtime(true) - $start);to a debug file or another profiling tool.Try to reduce protocol formatting/encoding time.
Not allow more than Number of cores in your machine here instances of php scripts running simultaneously.