diff --git a/python/.changelog.d/4101.added b/python/.changelog.d/4101.added new file mode 100644 index 0000000000..c15dccb4e5 --- /dev/null +++ b/python/.changelog.d/4101.added @@ -0,0 +1 @@ +Added support for benchmarks. diff --git a/python/src/trezorlib/benchmark.py b/python/src/trezorlib/benchmark.py new file mode 100644 index 0000000000..f96ef7970e --- /dev/null +++ b/python/src/trezorlib/benchmark.py @@ -0,0 +1,36 @@ +# This file is part of the Trezor project. +# +# Copyright (C) 2012-2022 SatoshiLabs and contributors +# +# This library is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the License along with this library. +# If not, see . + +from typing import TYPE_CHECKING + +from . import messages +from .tools import expect + +if TYPE_CHECKING: + from .client import TrezorClient + from .protobuf import MessageType + + +@expect(messages.BenchmarkNames) +def list_names( + client: "TrezorClient", +) -> "MessageType": + return client.call(messages.BenchmarkListNames()) + + +@expect(messages.BenchmarkResult) +def run(client: "TrezorClient", name: str) -> "MessageType": + return client.call(messages.BenchmarkRun(name=name)) diff --git a/python/src/trezorlib/cli/benchmark.py b/python/src/trezorlib/cli/benchmark.py new file mode 100644 index 0000000000..e445089815 --- /dev/null +++ b/python/src/trezorlib/cli/benchmark.py @@ -0,0 +1,68 @@ +# This file is part of the Trezor project. +# +# Copyright (C) 2012-2022 SatoshiLabs and contributors +# +# This library is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the License along with this library. +# If not, see . + +from fnmatch import fnmatch +from typing import TYPE_CHECKING, List, Optional + +import click + +from .. import benchmark +from . import with_client + +if TYPE_CHECKING: + + from ..client import TrezorClient + + +def list_names_patern( + client: "TrezorClient", pattern: Optional[str] = None +) -> List[str]: + names = list(benchmark.list_names(client).names) + if pattern is None: + return names + return [name for name in names if fnmatch(name, pattern)] + + +@click.group(name="benchmark") +def cli() -> None: + """Benchmark commands.""" + + +@cli.command() +@click.argument("pattern", required=False) +@with_client +def list_names(client: "TrezorClient", pattern: Optional[str] = None) -> None: + """List names of all supported benchmarks""" + names = list_names_patern(client, pattern) + if len(names) == 0: + click.echo("No benchmark satisfies the pattern.") + else: + for name in names: + click.echo(name) + + +@cli.command() +@click.argument("pattern", required=False) +@with_client +def run(client: "TrezorClient", pattern: Optional[str]) -> None: + """Run benchmark""" + names = list_names_patern(client, pattern) + if len(names) == 0: + click.echo("No benchmark satisfies the pattern.") + else: + for name in names: + result = benchmark.run(client, name) + click.echo(f"{name}: {result.value} {result.unit}") diff --git a/python/src/trezorlib/cli/trezorctl.py b/python/src/trezorlib/cli/trezorctl.py index 625baab5ee..60f8e8d309 100755 --- a/python/src/trezorlib/cli/trezorctl.py +++ b/python/src/trezorlib/cli/trezorctl.py @@ -31,6 +31,7 @@ from ..transport.udp import UdpTransport from . import ( AliasedGroup, TrezorConnection, + benchmark, binance, btc, cardano, @@ -416,6 +417,7 @@ cli.add_command(tezos.cli) cli.add_command(firmware.cli) cli.add_command(debug.cli) +cli.add_command(benchmark.cli) # # Main