Skip to content

Commit

Permalink
feat: split build and benchmark run commands
Browse files Browse the repository at this point in the history
  • Loading branch information
enthusiastmartin committed Mar 22, 2021
1 parent 19218b7 commit 2e536ad
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bench_wizard/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.0"
__version__ = "0.4.0"
32 changes: 25 additions & 7 deletions bench_wizard/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import subprocess
from typing import List

from bench_wizard.cargo import Cargo
from bench_wizard.config import Config
from bench_wizard.exceptions import BenchmarkCargoException
from bench_wizard.output import Output
Expand Down Expand Up @@ -34,7 +35,6 @@ class Benchmark:
""" Represents single benchmark"""

def __init__(self, pallet: str, command: [str], ref_value: float, extrinsics: list):

self._pallet = pallet
self._stdout = None
self._command = command
Expand Down Expand Up @@ -120,17 +120,16 @@ def _prepare_benchmarks(config: Config, reference_values: dict) -> List[Benchmar
benchmarks = []

for pallet in config.pallets:
command = COMMAND + [f"--pallet={pallet}"]
cargo = Cargo(pallet=pallet, template=config.template)
if config.output_dir:
output_file = os.path.join(config.output_dir, f"{pallet}.rs")
command += [f"--output={output_file}"]

if config.template:
command += [f"--template={config.template}"]
cargo.output = output_file

ref_data = reference_values[pallet]
ref_value = sum(list(map(lambda x: float(x), ref_data.values())))
benchmarks.append(Benchmark(pallet, command, ref_value, ref_data.keys()))
benchmarks.append(
Benchmark(pallet, cargo.command(), ref_value, ref_data.keys())
)

return benchmarks

Expand All @@ -149,6 +148,21 @@ def _run_benchmarks(benchmarks: List[Benchmark], output: Output, rerun=False) ->
output.update(bench)


def _build(manifest: str) -> None:
command = [
"cargo",
"build",
"--release",
"--features=runtime-benchmarks",
f"--manifest-path={manifest}",
]

result = subprocess.run(command, capture_output=True)

if result.returncode != 0:
raise BenchmarkCargoException(result.stderr.decode("utf-8"))


def run_pallet_benchmarks(config: Config, to_output: Output) -> None:
if not config.do_pallet_bench:
return
Expand All @@ -161,6 +175,10 @@ def run_pallet_benchmarks(config: Config, to_output: Output) -> None:

benchmarks = _prepare_benchmarks(config, s)

to_output.info("Compiling - this may take a while...")

_build("node/Cargo.toml")

to_output.info("Running benchmarks - this may take a while...")

_run_benchmarks(benchmarks, to_output)
Expand Down
43 changes: 43 additions & 0 deletions bench_wizard/cargo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from dataclasses import dataclass
from typing import Optional, List


@dataclass
class Cargo:
pallet: str
manifest: str = "node/Cargo.toml"
chain: str = "dev"
steps: int = 5
repeat: int = 20
extrinsic: str = "*"
execution: str = "wasm"
wasm_execution: str = "compiled"
heap_pages: int = 4096
output: Optional[str] = None
template: Optional[str] = None

def command(self) -> List[str]:
cmd = [
"cargo",
"run",
"--release",
"--features=runtime-benchmarks",
f"--manifest-path={self.manifest}",
"--",
"benchmark",
f"--pallet={self.pallet}",
f"--chain={self.chain}",
f"--steps={self.steps}",
f"--repeat={self.repeat}",
f"--extrinsic={self.extrinsic}",
f"--execution={self.execution}",
f"--wasm-execution={self.wasm_execution}",
f"--heap-pages={self.heap_pages}",
]

if self.output:
cmd.append(f"--output={self.output}")
if self.template:
cmd.append(f"--template={self.template}")

return cmd
5 changes: 4 additions & 1 deletion bench_wizard/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ def track(self, benchmarks: ["Benchmark"]):
self._tracker = len(benchmarks)

def update(self, benchmark: "Benchmark"):
print(f"Running {self._completed}/{self._tracker}", end="\r")
print(
f"Running {self._completed}/{self._tracker} (pallet: {benchmark.pallet})",
end="\r",
)
self._completed += benchmark.completed

def results(self, benchmarks: ["Benchmark"]):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "bench-wizard"
version = "0.3.0"
version = "0.4.0"
description = ""
authors = ["Martin Hloska <[email protected]>"]

Expand Down
2 changes: 1 addition & 1 deletion tests/test_bench_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert __version__ == "0.3.0"
assert __version__ == "0.4.0"

0 comments on commit 2e536ad

Please sign in to comment.