import_benchmark.py 779 B

12345678910111213141516171819202122232425262728293031
  1. """
  2. A benchmark which measures the import time of jsonschema
  3. """
  4. import subprocess
  5. import sys
  6. def import_time(loops):
  7. total_us = 0
  8. for _ in range(loops):
  9. p = subprocess.run( # noqa: S603 (arguments are static)
  10. [sys.executable, "-X", "importtime", "-c", "import jsonschema"],
  11. stderr=subprocess.PIPE,
  12. stdout=subprocess.DEVNULL,
  13. check=True,
  14. )
  15. line = p.stderr.splitlines()[-1]
  16. field = line.split(b"|")[-2].strip()
  17. us = int(field) # microseconds
  18. total_us += us
  19. # pyperf expects seconds
  20. return total_us / 1_000_000.0
  21. if __name__ == "__main__":
  22. from pyperf import Runner
  23. runner = Runner()
  24. runner.bench_time_func("Import time (cumulative)", import_time)