Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed Dec 17, 2024
1 parent 0e3dbc1 commit 7c47af1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
5 changes: 4 additions & 1 deletion pyperf/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,13 @@ def display_benchmarks(args, show_metadata=False, hist=False, stats=False,
empty_line(output)
output.extend(lines)

contains_warning = False
for line in output:
if line.startswith("WARNING:"):
contains_warning = True
print(line)

if not output and only_checks:
if not contains_warning and only_checks:
if len(data) == 1:
print("The benchmark seems to be stable")
else:
Expand Down
6 changes: 5 additions & 1 deletion pyperf/_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,11 @@ def required_nsamples(self):
# Get the means of the values per run
values = []
for run in self._runs:
values.append(statistics.mean(run.values))
if len(run.values):
values.append(statistics.mean(run.values))

if len(values) < 2:
return None

total = math.fsum(values)
mean = total / len(values)
Expand Down
10 changes: 8 additions & 2 deletions pyperf/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,10 @@ def format_checks(bench, lines=None):
% (bench.format_value(stdev), percent, bench.format_value(mean)))
else:
# display a warning if the number of samples isn't enough to get a stable result
if required_nsamples > len(bench._runs):
if (
required_nsamples is not None and
required_nsamples > len(bench._runs)
):
warn("Not enough samples to get a stable result (95% certainly of less than 1% variation)")

# Minimum and maximum, detect obvious outliers
Expand Down Expand Up @@ -463,7 +466,10 @@ def format_checks(bench, lines=None):
lines.append("Use pyperf stats, pyperf dump and pyperf hist to analyze results.")
lines.append("Use --quiet option to hide these warnings.")

if required_nsamples < len(bench._runs) * 0.75:
if (
required_nsamples is not None and
required_nsamples < len(bench._runs) * 0.75
):
lines.append("Benchmark was run more times than necessary to get a stable result.")
lines.append(
"Consider passing processes=%d to the Runner constructor to save time." %
Expand Down
15 changes: 12 additions & 3 deletions pyperf/tests/test_perf_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,16 @@ def test_hist(self):
22.8 ms: 3 ##############
22.9 ms: 4 ###################
22.9 ms: 4 ###################
Benchmark was run more times than necessary to get a stable result.
Consider passing processes=7 to the Runner constructor to save time.
""")
self.check_command(expected, 'hist', TELCO, env=env)

def test_show(self):
expected = ("""
Benchmark was run more times than necessary to get a stable result.
Consider passing processes=7 to the Runner constructor to save time.
Mean +- std dev: 22.5 ms +- 0.2 ms
""")
self.check_command(expected, 'show', TELCO)
Expand Down Expand Up @@ -518,6 +523,8 @@ def test_stats(self):
100th percentile: 22.9 ms (+2% of the mean) -- maximum
Number of outlier (out of 22.0 ms..23.0 ms): 0
Benchmark was run more times than necessary to get a stable result.
Consider passing processes=7 to the Runner constructor to save time.
""")
self.check_command(expected, 'stats', TELCO)

Expand Down Expand Up @@ -628,8 +635,10 @@ def test_slowest(self):

def test_check_stable(self):
stdout = self.run_command('check', TELCO)
self.assertEqual(stdout.rstrip(),
'The benchmark seems to be stable')
self.assertTrue(
'The benchmark seems to be stable' in
stdout.rstrip()
)

def test_command(self):
command = [sys.executable, '-c', 'pass']
Expand Down Expand Up @@ -689,7 +698,7 @@ def _check_track_memory(self, track_option):
'[1,2]*1000',
'-o', tmp_name)
bench = pyperf.Benchmark.load(tmp_name)

self._check_track_memory_bench(bench, loops=5)

def test_track_memory(self):
Expand Down

0 comments on commit 7c47af1

Please sign in to comment.