util.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from collections.abc import Callable
  10. from pathlib import Path
  11. from re import match
  12. import numpy as np
  13. from . import Scenario
  14. def duration_to_timedelta(duration: str) -> np.timedelta64 | None:
  15. """Converts a duration like `{time}.{frac}{unit}` to a `np.timedelta64`."""
  16. frac_to_ns: dict[str, Callable[[str], str]] = {
  17. "s": lambda frac: frac.ljust(9, "0")[0:9],
  18. "ms": lambda frac: frac.ljust(6, "0")[0:6],
  19. "us": lambda frac: frac.ljust(3, "0")[0:3],
  20. "ns": lambda frac: "0", # ns units should not have frac
  21. }
  22. p = r"(?P<time>[0-9]+)(\.(?P<frac>[0-9]+))?\s?(?P<unit>s|ms|µs|ns)"
  23. m = match(p, duration)
  24. if m is None:
  25. return None
  26. else:
  27. unit = "us" if m.group("unit") == "µs" else m.group("unit")
  28. time = np.timedelta64(m.group("time"), unit)
  29. frac = np.timedelta64(frac_to_ns[unit](m.group("frac") or "0"), "ns")
  30. return time + frac
  31. def results_path(repository: Path, scenario: Scenario, version: str) -> Path:
  32. # default suffix
  33. suffix = "unknown"
  34. # try to match Postgres version syntax
  35. m = match(r"PostgreSQL (?P<version>[0-9\.]+)", version)
  36. if m:
  37. suffix = f"pg-v{m['version']}"
  38. # try to match Materialize version syntax
  39. m = match(r"v(?P<version>[0-9\.]+(-dev)?) \((?P<commit>[0-9a-f]+)\)", version)
  40. if m:
  41. suffix = f"mz-v{m['version']}-{m['commit']}"
  42. file = f"optbench-{scenario}-{suffix}.csv"
  43. return repository / file