misc.python.materialize.optbench.util

 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
10from collections.abc import Callable
11from pathlib import Path
12from re import match
13
14import numpy as np
15
16from . import Scenario
17
18
19def duration_to_timedelta(duration: str) -> np.timedelta64 | None:
20    """Converts a duration like `{time}.{frac}{unit}` to a `np.timedelta64`."""
21
22    frac_to_ns: dict[str, Callable[[str], str]] = {
23        "s": lambda frac: frac.ljust(9, "0")[0:9],
24        "ms": lambda frac: frac.ljust(6, "0")[0:6],
25        "us": lambda frac: frac.ljust(3, "0")[0:3],
26        "ns": lambda frac: "0",  # ns units should not have frac
27    }
28
29    p = r"(?P<time>[0-9]+)(\.(?P<frac>[0-9]+))?\s?(?P<unit>s|ms|µs|ns)"
30    m = match(p, duration)
31
32    if m is None:
33        return None
34    else:
35        unit = "us" if m.group("unit") == "µs" else m.group("unit")
36        time = np.timedelta64(m.group("time"), unit)
37        frac = np.timedelta64(frac_to_ns[unit](m.group("frac") or "0"), "ns")
38        return time + frac
39
40
41def results_path(repository: Path, scenario: Scenario, version: str) -> Path:
42    # default suffix
43    suffix = "unknown"
44
45    # try to match Postgres version syntax
46    m = match(r"PostgreSQL (?P<version>[0-9\.]+)", version)
47    if m:
48        suffix = f"pg-v{m['version']}"
49
50    # try to match Materialize version syntax
51    m = match(r"v(?P<version>[0-9\.]+(-dev)?) \((?P<commit>[0-9a-f]+)\)", version)
52    if m:
53        suffix = f"mz-v{m['version']}-{m['commit']}"
54
55    file = f"optbench-{scenario}-{suffix}.csv"
56    return repository / file
def duration_to_timedelta(duration: str) -> numpy.timedelta64 | None:
20def duration_to_timedelta(duration: str) -> np.timedelta64 | None:
21    """Converts a duration like `{time}.{frac}{unit}` to a `np.timedelta64`."""
22
23    frac_to_ns: dict[str, Callable[[str], str]] = {
24        "s": lambda frac: frac.ljust(9, "0")[0:9],
25        "ms": lambda frac: frac.ljust(6, "0")[0:6],
26        "us": lambda frac: frac.ljust(3, "0")[0:3],
27        "ns": lambda frac: "0",  # ns units should not have frac
28    }
29
30    p = r"(?P<time>[0-9]+)(\.(?P<frac>[0-9]+))?\s?(?P<unit>s|ms|µs|ns)"
31    m = match(p, duration)
32
33    if m is None:
34        return None
35    else:
36        unit = "us" if m.group("unit") == "µs" else m.group("unit")
37        time = np.timedelta64(m.group("time"), unit)
38        frac = np.timedelta64(frac_to_ns[unit](m.group("frac") or "0"), "ns")
39        return time + frac

Converts a duration like {time}.{frac}{unit} to a np.timedelta64.

def results_path( repository: pathlib.Path, scenario: misc.python.materialize.optbench.Scenario, version: str) -> pathlib.Path:
42def results_path(repository: Path, scenario: Scenario, version: str) -> Path:
43    # default suffix
44    suffix = "unknown"
45
46    # try to match Postgres version syntax
47    m = match(r"PostgreSQL (?P<version>[0-9\.]+)", version)
48    if m:
49        suffix = f"pg-v{m['version']}"
50
51    # try to match Materialize version syntax
52    m = match(r"v(?P<version>[0-9\.]+(-dev)?) \((?P<commit>[0-9a-f]+)\)", version)
53    if m:
54        suffix = f"mz-v{m['version']}-{m['commit']}"
55
56    file = f"optbench-{scenario}-{suffix}.csv"
57    return repository / file