Skip to content

Commit 148a184

Browse files
authored
Merge pull request #42 from nicholasjng/staging
Make time processing optional, abbreviate home with proper Pathlib APIs
2 parents 466a054 + 597ed41 commit 148a184

File tree

5 files changed

+42
-28
lines changed

5 files changed

+42
-28
lines changed

pybm/commands/run.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class RunCommand(CLICommand):
2222
def __init__(self):
2323
super(RunCommand, self).__init__(name="run")
2424
self.config = PybmConfig.load()
25-
self.use_legacy_checkout: bool = self.config.get_value("git.legacycheckout")
2625

2726
def add_arguments(self):
2827
self.parser.add_argument(
@@ -56,8 +55,8 @@ def add_arguments(self):
5655
action="store_true",
5756
default=False,
5857
help="Run benchmarks in checkout mode in environment 'root'. Here, instead "
59-
"of persisted git worktrees, different refs are benchmarked using "
60-
"`git checkout` commands.",
58+
"of persisted git worktrees, different refs are benchmarked using `git "
59+
"checkout` commands.",
6160
)
6261
self.parser.add_argument(
6362
"--all",
@@ -73,14 +72,14 @@ def add_arguments(self):
7372
default=None,
7473
dest="source_ref",
7574
metavar="<git-ref>",
76-
help="Source benchmark targets from a different git reference.",
75+
help="Source benchmark targets from a different git reference <git-ref>.",
7776
)
7877
self.parser.add_argument(
7978
"--repetitions",
8079
type=int,
8180
default=5,
8281
metavar="<N>",
83-
help="Number of repetitions for the target benchmarks.",
82+
help="Number of times to repeat the target benchmarks.",
8483
)
8584
self.parser.add_argument(
8685
"--filter",
@@ -98,8 +97,8 @@ def add_arguments(self):
9897
dest="benchmark_context",
9998
metavar="<context>",
10099
help="Additional global context, given as strings in the format "
101-
"--context='key'='value'. Keys must be unique. Supplying two or more "
102-
"context values for the same key results in an error.",
100+
"--context='key'='value'. Keys must be unique, supplying more than one "
101+
"value for the same key results in an error.",
103102
)
104103

105104
runner: BaseRunner = get_component_class("runner", config=self.config)
@@ -128,18 +127,21 @@ def run(self, args: List[str]) -> int:
128127
runner: BaseRunner = get_component_class("runner", config=self.config)
129128
reporter: BaseReporter = get_component_class("reporter", config=self.config)
130129

130+
# whether to use legacy checkouts (git < 2.17)
131+
use_legacy_checkout: bool = self.config.get_value("git.legacycheckout")
132+
131133
verbose: bool = runner_options.pop("verbose")
132134

133135
env_ids: List[str] = runner_options.pop("environments") or []
134136
run_all: bool = runner_options.pop("run_all")
135137
checkout_mode: bool = runner_options.pop("checkout")
136138
source_ref: Optional[str] = runner_options.pop("source_ref")
137139
source_path = Path(runner_options.pop("benchmarks"))
138-
run_as_module = runner_options.pop("run_as_module")
140+
run_as_module: bool = runner_options.pop("run_as_module")
139141

140142
# runner dispatch arguments
141-
repetitions = runner_options.pop("repetitions")
142-
benchmark_filter = runner_options.pop("benchmark_filter")
143+
repetitions: int = runner_options.pop("repetitions")
144+
benchmark_filter: Optional[str] = runner_options.pop("benchmark_filter")
143145
benchmark_context = runner_options.pop("benchmark_context")
144146
# at this point, runner_options only include the additional runner kwargs
145147

@@ -198,7 +200,7 @@ def run(self, args: List[str]) -> int:
198200
worktree=worktree,
199201
source_path=source_path,
200202
source_ref=source_ref,
201-
use_legacy_checkout=self.use_legacy_checkout,
203+
use_legacy_checkout=use_legacy_checkout,
202204
) as benchmark_targets:
203205
n = len(benchmark_targets)
204206
if n > 0:
@@ -208,9 +210,9 @@ def run(self, args: List[str]) -> int:
208210
)
209211
else:
210212
msg = (
211-
f"Benchmark selector {str(source_path)!r} did not "
212-
f"match any directory or Python files for {ref_type} "
213-
f"{ref!r} in environment {environment.name!r}."
213+
f"Benchmark selector {str(source_path)!r} did not match any "
214+
f"directory or Python files for {ref_type} {ref!r} in "
215+
f"environment {environment.name!r}."
214216
)
215217

216218
if runner.fail_fast:
@@ -247,8 +249,8 @@ def run(self, args: List[str]) -> int:
247249
"benchmark subprocess. Please check that the configured "
248250
"benchmark runner actually writes the results to stdout."
249251
)
250-
else:
251-
reporter.write(ref, benchmark, data)
252+
253+
reporter.write(ref, benchmark, data)
252254

253255
if checkout_mode:
254256
root_ref, root_type = root_checkout

pybm/git.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
disambiguate_info,
1414
resolve_ref,
1515
is_main_worktree,
16-
checkout,
16+
checkout as git_checkout,
1717
resolve_commit,
1818
)
1919
from pybm.util.path import current_folder
@@ -325,7 +325,7 @@ def switch(self, worktree: Worktree, ref: str, ref_type: Optional[str] = None):
325325
f"Object {ref!r} could not be understood as a valid git reference."
326326
)
327327

328-
checkout(ref=ref, cwd=worktree.root)
328+
git_checkout(ref=ref, cwd=worktree.root)
329329

330330
# null the old reference type if necessary
331331
if ref_type != old_type:

pybm/reporters/console.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,26 @@ def process(bm: Dict[str, Any], target_time_unit: str, shalength: int):
3535
bm["name"] = format_benchmark(bm.pop("name"), bm.pop("executable"))
3636
bm["reference"] = format_ref(bm.pop("ref"), bm.pop("commit"), shalength=shalength)
3737

38-
time_unit: str = bm.pop("time_unit")
39-
time_values: Dict[str, Any] = dfilter_regex(".*time", bm)
38+
time_unit: Optional[str] = bm.pop("time_unit", None)
4039

41-
rescale_fn = partial(rescale, current_unit=time_unit, target_unit=target_time_unit)
40+
if time_unit is not None:
41+
time_values: Dict[str, Any] = dfilter_regex(".*time", bm)
4242

43-
bm.update(dvmap(rescale_fn, time_values))
43+
rescale_fn = partial(
44+
rescale, current_unit=time_unit, target_unit=target_time_unit
45+
)
46+
47+
bm.update(dvmap(rescale_fn, time_values))
4448

4549
return bm
4650

4751

4852
def compare(results: List[Dict[str, Any]]):
4953
"""Compare results between different refs with respect to an anchor ref. Assumes
5054
that the results are sorted in the same order."""
55+
if len(results) == 1:
56+
return results
57+
5158
anchor_result = results[0]
5259

5360
for result in results:
@@ -152,7 +159,7 @@ def compare(
152159

153160
processed_results = lmap(process_fn, reduced)
154161

155-
# group results by again benchmark name
162+
# group results again by benchmark name
156163
grouped_results = groupby("name", processed_results)
157164

158165
if absolute:
@@ -164,6 +171,7 @@ def compare(
164171
formatted_results = lmap(transform_fn, compared_results)
165172

166173
log_to_console(formatted_results, padding=self.padding)
174+
# TODO: Print summary about improvements etc.
167175

168176
def transform_result(self, bm: Dict[str, Any], anchor_ref: str) -> Dict[str, str]:
169177
"""Finalize column header names, cast all values to string and

pybm/reporters/util.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ def log_to_console(results: List[Dict[str, str]], padding: int = 1):
9191
print(make_separator(column_widths, padding=padding))
9292

9393
print(make_line(res.values(), column_widths, padding=padding))
94-
# TODO: Print summary about improvements etc.
9594

9695

9796
def reduce(results: List[Dict[str, Any]]) -> Dict[str, Any]:

pybm/util/print.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55

66

77
def abbrev_home(path: Union[str, Path]) -> str:
8-
homepath = Path(path).resolve().relative_to(Path.home())
9-
return str(Path("~") / homepath)
8+
path = Path(path).resolve()
9+
try:
10+
homepath = path.relative_to(Path.home())
11+
abbrev_path = str(Path("~") / homepath)
12+
except ValueError:
13+
# case where the env path is not a subpath of the home directory
14+
abbrev_path = str(path)
15+
return abbrev_path
1016

1117

1218
def calculate_column_widths(data: Iterable[Iterable[str]]) -> List[int]:
@@ -17,9 +23,8 @@ def calculate_column_widths(data: Iterable[Iterable[str]]) -> List[int]:
1723
def make_line(values: Iterable[str], column_widths: Iterable[int], padding: int) -> str:
1824
pad_char = " " * padding
1925
sep = "|".join([pad_char] * 2)
20-
offset = pad_char
2126

22-
line = offset + sep.join(f"{n:<{w}}" for n, w in zip(values, column_widths))
27+
line = pad_char + sep.join(f"{n:<{w}}" for n, w in zip(values, column_widths))
2328
return line
2429

2530

0 commit comments

Comments
 (0)