Skip to content

Instantly share code, notes, and snippets.

@bhavishyagopesh
Last active August 19, 2017 08:44
Show Gist options
  • Save bhavishyagopesh/4b90dd8e4907ad247aeb6648ae8a97e1 to your computer and use it in GitHub Desktop.
Save bhavishyagopesh/4b90dd8e4907ad247aeb6648ae8a97e1 to your computer and use it in GitHub Desktop.
Benchmark smtplib module.
"""
Benchmark smtplib module.
"""
# This benchmark requires a running smtp service on port 25
# For Eg: PostFix
import perf
from six.moves import xrange
import smtplib
import email.utils
from email.mime.text import MIMEText
def add_cmdline_args(cmd, args):
if args.benchmark:
cmd.append(args.benchmark)
def bench_smtplib(loops):
range_it = xrange(loops)
t0 = perf.perf_counter()
for _ in range_it:
msg = MIMEText('This is the body of the message.')
msg['To'] = email.utils.formataddr(('Recipient',
'recipient@example.com'))
msg['From'] = email.utils.formataddr(('Author',
'author@example.com'))
msg['Subject'] = 'Simple test message'
server = smtplib.SMTP('localhost', 25)
try:
server.sendmail('author@example.com',
['recipient@example.com'],
msg.as_string())
finally:
server.quit()
dt = perf.perf_counter() - t0
return dt
BENCHMARKS = {
"smtplib": bench_smtplib,
}
if __name__ == "__main__":
runner = perf.Runner(add_cmdline_args=add_cmdline_args)
runner.metadata['description'] = "Performance of the smtplib module"
parser = runner.argparser
parser.add_argument("benchmark", nargs='?', choices=sorted(BENCHMARKS))
options = runner.parse_args()
if options.benchmark:
benchmarks = (options.benchmark,)
else:
benchmarks = sorted(BENCHMARKS)
for bench in benchmarks:
name = '%s' % bench
bench_func = BENCHMARKS[bench]
runner.bench_time_func(name, bench_func, inner_loops=10)
This adds a benchmark for "smtplib". Though I'm not sure of the procedure, presently it depends on a mailservice like 'PostFix' on port 25
and than sends mail to it.And with this procedure *py3* comes out to be slower than *py2*.
here are some stats:
python3 bm_smtplib.py
.....................
smtplib: Mean +- std dev: 13.2 ms +- 22.8 us
python2 bm_smtplib.py
.....................
smtplib: Mean +- std dev: 9.66 ms +- 32.52 us
So thoughts?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment