diff --git a/benchmarks/conftest.py b/benchmarks/conftest.py index 8b4be8a23..7f280d85b 100644 --- a/benchmarks/conftest.py +++ b/benchmarks/conftest.py @@ -9,6 +9,8 @@ from os.path import abspath from shutil import which, rmtree from tempfile import mkdtemp from pathlib import Path +from contextlib import contextmanager +from time import time import pytest import pytest_twisted @@ -105,3 +107,22 @@ def client_node(request, grid, storage_nodes, number_of_nodes) -> Client: ) print(f"Client node pid: {client_node.process.transport.pid}") return client_node + + +class Benchmarker: + """Keep track of benchmarking results.""" + + @contextmanager + def record(self, name, **parameters): + """Record the timing of running some code, if it succeeds.""" + start = time() + yield + elapsed = time() - start + # For now we just print the outcome: + parameters = " ".join(f"{k}={v}" for (k, v) in parameters.items()) + print(f"BENCHMARK RESULT: {name} {parameters} elapsed {elapsed} secs") + + +@pytest.fixture(scope="session") +def tahoe_benchmarker(): + return Benchmarker() diff --git a/benchmarks/test_cli.py b/benchmarks/test_cli.py index 9f39038c7..94eca4475 100644 --- a/benchmarks/test_cli.py +++ b/benchmarks/test_cli.py @@ -12,7 +12,9 @@ def cli_alias(client_node): cli(client_node.process, "create-alias", "cli") -def test_get_put_one_file(client_node, cli_alias, tmp_path): +def test_get_put_one_file( + client_node, cli_alias, tmp_path, tahoe_benchmarker, number_of_nodes +): """ Upload a file with ``tahoe put`` and then download it with ``tahoe get``, measuring the latency of both operations. @@ -20,21 +22,27 @@ def test_get_put_one_file(client_node, cli_alias, tmp_path): file_size = 1000 # parameterize later on file_path = tmp_path / "file" DATA = b"0123456789" * (file_size // 10) - with file_path.open("wb") as f: f.write(DATA) - cli(client_node.process, "put", str(file_path), "cli:tostdout") - p = Popen( - [ - "tahoe", - "--node-directory", - client_node.process.node_dir, - "get", - "cli:tostdout", - "-", - ], - stdout=PIPE, - ) - assert p.stdout.read() == DATA - assert p.wait() == 0 + with tahoe_benchmarker.record( + "cli-put-file", file_size=file_size, number_of_nodes=number_of_nodes + ): + cli(client_node.process, "put", str(file_path), "cli:tostdout") + + with tahoe_benchmarker.record( + "cli-get-file", file_size=file_size, number_of_nodes=number_of_nodes + ): + p = Popen( + [ + "tahoe", + "--node-directory", + client_node.process.node_dir, + "get", + "cli:tostdout", + "-", + ], + stdout=PIPE, + ) + assert p.stdout.read() == DATA + assert p.wait() == 0