Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,23 @@ Runner class

Return the :attr:`args` attribute.

.. method:: is_worker()

Return ``True`` in a benchmark worker process, or ``False`` in the
main process. Parse command line arguments if they have not been parsed
yet.

Worker processes re-execute the benchmark script and may skip benchmarks,
returning ``None``. Run all benchmarks in both the main process and
workers, and use this method to guard code that processes their results::

runner = pyperf.Runner()
benchmarks = [runner.bench_func("first", first_func),
runner.bench_func("second", second_func)]
if not runner.is_worker():
suite = pyperf.BenchmarkSuite(benchmarks)
suite.dump("benchmarks.json")

Attributes:

.. attribute:: args
Expand Down
4 changes: 4 additions & 0 deletions pyperf/_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ def parse_args(self, args=None):
self._set_args(args)
return args

def is_worker(self):
"""Return whether this process is a benchmark worker."""
return self.parse_args().worker

def _cpu_affinity(self):
cpus = self.args.affinity
if not cpus:
Expand Down
35 changes: 35 additions & 0 deletions pyperf/tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,41 @@ def test_parse_args_twice_error(self):
with self.assertRaises(RuntimeError):
runner.parse_args(args)

def test_is_worker(self):
for args, expected in (([], False), (['--worker', '-l1', '-w0'], True)):
for parsed in (False, True):
with self.subTest(args=args, parsed=parsed):
pyperf.Runner._created.clear()
runner = pyperf.Runner()
if parsed:
runner.parse_args(args)
with mock.patch.object(sys, 'argv', ['benchmark.py'] + args):
self.assertIs(runner.is_worker(), expected)
namespace = runner.args
self.assertIs(runner.is_worker(), expected)
self.assertIs(runner.args, namespace)

def test_is_worker_guards_benchmark_suite(self):
source = textwrap.dedent('''
import pyperf

def noop():
pass

runner = pyperf.Runner(values=1, processes=1, loops=1, warmups=0)
benchmarks = [runner.bench_func('first', noop),
runner.bench_func('second', noop)]
if not runner.is_worker():
suite = pyperf.BenchmarkSuite(benchmarks)
print('suite: ' + ', '.join(suite.get_benchmark_names()))
''')
with tests.temporary_file(suffix='.py') as filename:
with open(filename, 'w', encoding='utf-8') as script:
script.write(source)
result = tests.get_output([sys.executable, filename, '--quiet'])
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout.count('suite: first, second'), 1)

def test_duplicated_named(self):
def time_func(loops):
return 1.0
Expand Down