measurement.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 __future__ import annotations
  10. from dataclasses import dataclass
  11. from enum import Enum, auto
  12. class MeasurementUnit(Enum):
  13. UNKNOWN = "?"
  14. SECONDS = "s"
  15. NANOSECONDS = "ns"
  16. COUNT = "#"
  17. MEGABYTE = "MB"
  18. def __str__(self):
  19. return str(self.value)
  20. @dataclass
  21. class WallclockDuration:
  22. duration: float
  23. unit: MeasurementUnit
  24. def is_equal_or_after(self, other: WallclockDuration) -> bool:
  25. assert self.unit == other.unit
  26. return self.duration >= other.duration
  27. class MeasurementType(Enum):
  28. WALLCLOCK = auto()
  29. MEMORY_MZ = auto()
  30. MEMORY_CLUSTERD = auto()
  31. def __str__(self) -> str:
  32. return self.name.lower()
  33. def is_amount(self) -> bool:
  34. return self in {
  35. MeasurementType.MEMORY_MZ,
  36. MeasurementType.MEMORY_CLUSTERD,
  37. }
  38. def is_lower_value_better(self) -> bool:
  39. return True
  40. @dataclass
  41. class Measurement:
  42. type: MeasurementType
  43. value: float
  44. unit: MeasurementUnit
  45. notes: str | None = None
  46. def __str__(self) -> str:
  47. return f"{self.value:>11.3f}({self.type})"