psf/pyperf

pyperf does not work in REPL

Marco-Sulla opened this issue · 2 comments

(venv) marco@buzz:~/sources/cpython_mine$ python
Python 3.6.9 (default, Jul 17 2020, 12:50:27) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyperf
>>> 
>>> runner = pyperf.Runner()
>>> runner.timeit(name="sort a sorted list",
...               stmt="sorted(s, key=f)",
...               setup="f = lambda x: x; s = list(range(1000))")
/home/marco/sources/cpython_mine/venv/bin/python: can't find '__main__' module in ''

I'm using pyperf 2.0.0.

Different variant of same problem, now on pyperf 2.3.0. Same failure in pyperf 2.2.0.

Python 3.8.10 (default, Sep 28 2021, 16:10:42) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyperf
>>> pyperf.__version__
'2.3.0'
>>> runner = pyperf.Runner()
>>> runner.timeit('sorted(nums)', stmt='sorted(nums)', setup='import random; nums = [i for i in range(0, 100)]; random.shuffle(nums)')
usage: -m pyperf
       [-h]
       {show,hist,compare_to,stats,metadata,check,collect_metadata,timeit,system,convert,dump,slowest,command}
       ...
-m pyperf: error: argument action: invalid choice: '4' (choose from 'show', 'hist', 'compare_to', 'stats', 'metadata', 'check', 'collect_metadata', 'timeit', 'system', 'convert', 'dump', 'slowest', 'command')
Error when running timeit benchmark:

Statement:
'sorted(nums)'

Setup:
'import random; nums = [i for i in range(0, 100)]; random.shuffle(nums)'

Teardown:
'pass'

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/pyperf/_timeit.py", line 230, in bench_timeit
    runner.bench_time_func(name, timer.time_func, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/pyperf/_runner.py", line 462, in bench_time_func
    return self._main(task)
  File "/usr/local/lib/python3.8/dist-packages/pyperf/_runner.py", line 432, in _main
    bench = self._manager()
  File "/usr/local/lib/python3.8/dist-packages/pyperf/_runner.py", line 560, in _manager
    bench = Manager(self).create_bench()
  File "/usr/local/lib/python3.8/dist-packages/pyperf/_manager.py", line 229, in create_bench
    worker_bench, run = self.create_worker_bench()
  File "/usr/local/lib/python3.8/dist-packages/pyperf/_manager.py", line 128, in create_worker_bench
    suite = self.create_suite()
  File "/usr/local/lib/python3.8/dist-packages/pyperf/_manager.py", line 118, in create_suite
    suite = self.spawn_worker(self.calibrate_loops, 0)
  File "/usr/local/lib/python3.8/dist-packages/pyperf/_manager.py", line 104, in spawn_worker
    raise RuntimeError("%s failed with exit code %s"
RuntimeError: /usr/bin/python3 failed with exit code 2

comint-write-history-sentinel: Python exited abnormally with code 1

Oh right, currently, pyperf can only be used by running a script: https://pyperf.readthedocs.io/en/latest/examples.html

To support REPL, Manager.worker_cmd() should be enhanced. Currently, there is a special Runner class for the python3 -m pyperf timeit command: it sets program_args to ('-m', 'pyperf', 'timeit').