better benchmarking and reporting

This commit is contained in:
holger krekel 2023-10-19 02:00:02 +02:00
parent 902f98c9ba
commit 1b347f97a0
3 changed files with 55 additions and 17 deletions

View File

@ -1,23 +1,30 @@
def test_tls_serialized_connect(benchmark, imap_or_smtp):
def connect():
imap_or_smtp.connect()
def test_tls_imap(benchmark, imap):
def imap_connect():
imap.connect()
benchmark(connect)
benchmark(imap_connect, 10)
def test_login(benchmark, imap_or_smtp, gencreds):
cls = imap_or_smtp.__class__
conns = []
for i in range(20):
conn = cls(imap_or_smtp.host)
conn.connect()
conns.append(conn)
def test_login_imap(benchmark, imap, gencreds):
def imap_connect_and_login():
imap.connect()
imap.login(*gencreds())
def login():
conn = conns.pop()
conn.login(*gencreds())
benchmark(imap_connect_and_login, 10)
benchmark(login)
def test_tls_smtp(benchmark, smtp):
def smtp_connect():
smtp.connect()
benchmark(smtp_connect, 10)
def test_login_smtp(benchmark, smtp, gencreds):
def smtp_connect_and_login():
smtp.connect()
smtp.login(*gencreds())
benchmark(smtp_connect_and_login, 10)
def test_send_and_receive_10(benchmark, cmfactory, lp):
@ -31,4 +38,4 @@ def test_send_and_receive_10(benchmark, cmfactory, lp):
for i in range(10):
ac2.wait_next_incoming_message()
benchmark(send_10_receive_all)
benchmark(send_10_receive_all, 1)

View File

@ -1,5 +1,6 @@
import os
import io
import time
import random
import subprocess
import imaplib
@ -14,6 +15,10 @@ def pytest_addoption(parser):
)
def pytest_configure(config):
config._benchresults = {}
def pytest_runtest_setup(item):
markers = list(item.iter_markers(name="slow"))
if markers:
@ -54,6 +59,32 @@ def pytest_report_header():
return ["-" * len(text), text, "-" * len(text)]
@pytest.fixture
def benchmark(request):
def bench(func, num, name=None):
if name is None:
name = func.__name__
durations = []
for i in range(num):
now = time.time()
func()
durations.append(time.time()-now)
durations.sort()
request.config._benchresults[name] = durations
return bench
def pytest_terminal_summary(terminalreporter):
tr = terminalreporter
results = tr.config._benchresults
tr.section("benchmark results")
for name, durations in results.items():
overall = sum(durations)
median = sorted(durations)[len(durations) // 2]
tr.write_line(f"{name: <30} {median:2.4f} seconds")
@pytest.fixture
def imap(maildomain):
return ImapConn(maildomain)

View File

@ -11,4 +11,4 @@ chatmaild/venv/bin/pip install --upgrade pytest build 'setuptools>=68'
chatmaild/venv/bin/pip install -e chatmaild
python3 -m venv online-tests/venv
online-tests/venv/bin/pip install pytest pytest-timeout pdbpp deltachat pytest-benchmark
online-tests/venv/bin/pip install pytest pytest-timeout pdbpp deltachat