action.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. import textwrap
  10. from collections.abc import Callable, Iterator
  11. from materialize.feature_benchmark.executor import Executor
  12. class Action:
  13. def __init__(self) -> None:
  14. self._executor: Executor | None = None
  15. def __iter__(self) -> Iterator[None]:
  16. return self
  17. def __next__(self) -> None:
  18. return self.run()
  19. def __call__(self, executor: Executor) -> "Action":
  20. self._executor = executor
  21. return self
  22. def run(
  23. self,
  24. executor: Executor | None = None,
  25. ) -> None:
  26. raise NotImplementedError
  27. class LambdaAction(Action):
  28. def __init__(self, _lambda: Callable) -> None:
  29. self._lambda = _lambda
  30. def run(
  31. self,
  32. executor: Executor | None = None,
  33. ) -> None:
  34. e = executor or self._executor
  35. assert e is not None
  36. e.Lambda(self._lambda)
  37. return None
  38. class Kgen(Action):
  39. def __init__(self, topic: str, args: list[str]) -> None:
  40. self._topic: str = topic
  41. self._args: list[str] = args
  42. self._executor: Executor | None = None
  43. def run(
  44. self,
  45. executor: Executor | None = None,
  46. ) -> None:
  47. executor = executor or self._executor
  48. assert executor
  49. executor.Kgen(topic=self._topic, args=self._args)
  50. class TdAction(Action):
  51. """Use testdrive to run some queries without measuring"""
  52. def __init__(self, td_str: str, dedent: bool = True) -> None:
  53. self._td_str = textwrap.dedent(td_str) if dedent else td_str
  54. self._executor: Executor | None = None
  55. def run(
  56. self,
  57. executor: Executor | None = None,
  58. ) -> None:
  59. executor = executor or self._executor
  60. assert executor
  61. # Print each query once so that it is easier to reproduce regressions
  62. # based on just the logs from CI
  63. if executor.add_known_fragment(self._td_str):
  64. print(self._td_str)
  65. executor.Td(self._td_str)
  66. class DummyAction(Action):
  67. def run(
  68. self,
  69. executor: Executor | None = None,
  70. ) -> None:
  71. return None