# Benchmarking a mailer

**URL:** <https://www.ruby-forum.com/t/benchmarking-a-mailer/181538>\
**Category:** Rails\
**Created:** [January 13, 2010, 1:13pm UTC](https://www.ruby-forum.com/t/benchmarking-a-mailer/181538 "2010-01-13T13:13:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [January 13, 2010, 1:13pm UTC](https://www.ruby-forum.com/t/benchmarking-a-mailer/181538/1 "2010-01-13T13:13:44Z")

</div>

We’d like to see how long it takes for a Mailer object to leave our  
server after the deliver command is sent. How could we do this? We’d  
like to get an accurate value (e.g. in milliseconds).

We were using Sendmail to send e-mail from our app, but Sendmail had  
the problem that it took too long to send some e-mails. So we switched  
to Postfix, which is quite a bit faster and more accurate. However,  
we’re still using :sendmail as our delivery\_method. It works, but  
could SMTP be faster?

Also, is there any way to prioritize Mailer objects? We’d like some e-  
mails to go out immediatly and some can be added to the end of the  
queue.

Thanks.

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [January 13, 2010, 1:51pm UTC](https://www.ruby-forum.com/t/benchmarking-a-mailer/181538/2 "2010-01-13T13:51:48Z")

</div>

jhaagmans wrote:

> We’d like to see how long it takes for a Mailer object to leave our  
> server after the deliver command is sent. How could we do this? We’d  
> like to get an accurate value (e.g. in milliseconds).  
> Wrap the call to your deliver\_xxx method in Benchmark.realtime. This  
> will of course only measure the time it takes to generate the mail  
> object and until it is handed off to the mail server.

Since mails can be temporarily rejected multiple times (e.g.  
greylisting, full mailboxes etc.) you’ll have to analyze your mailserver  
logs if you want to look at those times as well.

> We were using Sendmail to send e-mail from our app, but Sendmail had  
> the problem that it took too long to send some e-mails. So we switched  
> to Postfix, which is quite a bit faster and more accurate. However,  
> we’re still using :sendmail as our delivery\_method. It works, but  
> could SMTP be faster?

Conventional wisdom says that bulk mailings should be done via SMTP  
instead of local delivery, but I’ve had the opposite experience with  
Postfix; here delivery via :sendmail to a mailserver running on the same  
machine was a lot faster than using :smtp. Your mileage may vary  
according to server hardware, mail type, size and the amount of mails  
that are being sent at once.

> Also, is there any way to prioritize Mailer objects? We’d like some e-  
> mails to go out immediatly and some can be added to the end of the  
> queue.

That is something you’ll have to configure in your mail server. AFAIK  
Postfix doesn’t have a prioritization built in. You might want to  
consider using different postfix instances to implement that kind of  
behaviour.

Regs,  
Sven

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [January 13, 2010, 2:04pm UTC](https://www.ruby-forum.com/t/benchmarking-a-mailer/181538/3 "2010-01-13T14:04:05Z")

</div>

Hi Sven,

Thanks! Benchmarking the Mailer.deliver commands returns about 45 ms  
of elapsed time when sending through SMTP and 250 ms when sending  
through sendmail, so that at least reduces the time it takes to get  
the e-mail to Postfix. Now I’ll just have to look at optimizing  
Postfix.

There’s one specific domain we’d like to instantly mail to. Will it  
save DNS-lookup time if I’d add the mailserver at the other end to our  
hosts file? If so, should we use the MX-record ([mail.domain.com](http://mail.domain.com)) or  
just the domain part of the e-mail address ([domain.com](http://domain.com))?

Thanks again!

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [January 16, 2010, 3:21pm UTC](https://www.ruby-forum.com/t/benchmarking-a-mailer/181538/4 "2010-01-16T15:21:58Z")

</div>

Thanks for suggesting BIND, I usually only install BIND on a server  
running a nameserver, but this helps 🙂

I’ve decided to go another way though. We can use the SMTP server on  
the receiving end directly from Rails, because it accepts direct SMTP  
requests from our server. Now I’d like to use two ActionMailer  
configurations: one using our own SMTP server (Postfix) for regular  
outgoing e-mails with low priority. And one using the remote SMTP  
server. We’d like to decide which SMTP server it should go to based on  
the Mailer method used. E.g. one mailed method called “notifier” and  
one called “urgent\_notifier”.

How can I accomplish this?

Thank you!

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [January 13, 2010, 4:12pm UTC](https://www.ruby-forum.com/t/benchmarking-a-mailer/181538/5 "2010-01-13T16:12:52Z")

</div>

jhaagmans wrote:

> Hi Sven,

[…]

> There’s one specific domain we’d like to instantly mail to. Will it  
> save DNS-lookup time if I’d add the mailserver at the other end to our  
> hosts file? If so, should we use the MX-record ([mail.domain.com](http://mail.domain.com)) or  
> just the domain part of the e-mail address ([domain.com](http://domain.com))?

Right idea, but a fragile approach. If upstream ever changes the IP  
address of the mail server you’ll still be trying to deliver to the old  
host, which may cause tons of bounced mail before you catch it.

I’d recommend installing a caching-only DNS server on your mail server.  
If you don’t want to get into the overhead of installing and maintaining  
BIND you can try a something smaller that works just as well, like  
dnsmasq (be sure to turn off dnsmasq’s DHCP capabilities though).

YMMV, depending on the amount of mail you’re generating. For ten mails a  
day you won’t see much of an improvement, but several thousand will at  
the very least help taking load off of your DNS server.

Regs,  
Sven
