# Licensed under a 3-clause BSD style license - see LICENSE.rst"""\Usage: python -masv.benchmark COMMAND [...]Manage a single benchmark and, when run from the commandline, reportits runtime to a file.commands: timing [...] Run timing benchmark for given Python statement.internal commands: discover BENCHMARK_DIR RESULT_FILE Discover benchmarks in a given directory and store result to a file. setup_cache BENCHMARK_DIR BENCHMARK_ID Run setup_cache for given benchmark. run BENCHMARK_DIR BENCHMARK_ID QUICK PROFILE_PATH RESULT_FILE Run a given benchmark, and store result in a file. run_server BENCHMARK_DIR SOCKET_FILENAME Run a Unix socket forkserver."""importosimportsysfromasv_runner.checkimport_checkfromasv_runner.discoveryimport_discoverfromasv_runner.runimport_runfromasv_runner.serverimport_run_serverfromasv_runner.setup_cacheimport_setup_cachefromasv_runner.timingimport_timing
[docs]defmain():# Remove asv package directory from `sys.path`. This script file resides# there although it's not part of the package, so Python prepends it to# `sys.path` on start which can shadow other modules. On Python 3.11+ it is# possible to use `PYTHONSAFEPATH` to prevent this, but the script needs to# work for older versions of Python.if(notgetattr(sys.flags,'safe_path',False)# Python 3.11+ only.andsys.path[0]==os.path.dirname(os.path.abspath(__file__))):sys.path.pop(0)iflen(sys.argv)<2:_help([])sys.exit(1)mode=sys.argv[1]args=sys.argv[2:]env=os.environ.copy()# --- Modify sys.path for the current interpreter ---if'ASV_PYTHONPATH'inenv:new_paths=env['ASV_PYTHONPATH'].split(os.pathsep)forpathinreversed(new_paths):# Add to the front to prioritizeifpathnotinsys.path:sys.path.insert(0,path)# Remove ASV_PYTHONPATH from env, as it's no longer needed after sys.path updateenv.pop('ASV_PYTHONPATH')else:# Clean up sys.path if PYTHONPATH was set but ASV_PYTHONPATH is notif'PYTHONPATH'inenv:old_paths=env['PYTHONPATH'].split(os.pathsep)forpathinold_paths:ifpathinsys.path:sys.path.remove(path)env.pop('PYTHONPATH')ifmodeincommands:commands[mode](args)sys.exit(0)else:sys.stderr.write(f"Unknown mode {mode}\n")sys.exit(1)