bebleo / smtpdfix

A SMTP server for use as a pytest fixture that implements encryption and authentication.
MIT License
16 stars 5 forks source link

SSL is slow #388

Open azmeuk opened 5 months ago

azmeuk commented 5 months ago

Sending a single mail to smtpdfix with SSL enabled on a very capable computer can take more than 1s.

This can easily be reproduced with this test:

import email.message
import smtplib

def test_send_email_ssl(smtpd):
    smtpd.config.use_ssl = True
    smtpd.config.use_starttls = False

    msg = email.message.EmailMessage()
    msg["To"] = "<foobar@example.org>"

    with smtplib.SMTP_SSL(smtpd.hostname, smtpd.port) as smtp:
        smtp.send_message(msg)

    assert smtpd.messages
$ time pytest --disable-warnings --durations 1 test_ssl.py
=============================================== test session starts ================================================
platform linux -- Python 3.12.2, pytest-8.1.1, pluggy-1.4.0
rootdir: /home/eloi/test
plugins: smtpdfix-0.5.1
collected 1 item

test_ssl.py .                                                                                                [100%]

=============================================== slowest 1 durations ================================================
2.04s call     test_ssl.py::test_send_test_email_ssl
========================================== 1 passed, 2 warnings in 2.09s ===========================================
pytest --disable-warnings --durations 1 test_ssl.py  0,40s user 0,04s system 17% cpu 2,442 total

Here the test takes 2,04s, but without SSL it would take 0.01s. Duplicating the very same ssl test takes twice as long, so whatever is causing this delay is executed on each test.

I suspect being slow is something hard to avoid with SSL, but maybe there are strategies to mitigate this? Whatever takes time, are there things to cache during the test sessions, less secure algorithms to choose, or shorter keys, so everything goes fast?

Any thoughts?

Environment