Skip to content

Commit 42143f5

Browse files
committed
Add a benchmark for the resampler
Signed-off-by: Leandro Lucarella <[email protected]>
1 parent a0f4657 commit 42143f5

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# License: MIT
2+
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Benchmark resampling."""
5+
6+
from datetime import datetime, timedelta, timezone
7+
from timeit import timeit
8+
from typing import Sequence
9+
10+
from frequenz.sdk.timeseries import Sample
11+
from frequenz.sdk.timeseries._resampling import ResamplerConfig, _ResamplingHelper
12+
13+
14+
def nop( # pylint: disable=unused-argument
15+
samples: Sequence[Sample], resampling_period_s: float
16+
) -> float:
17+
"""Return 0.0."""
18+
return 0.0
19+
20+
21+
def _benchmark_resampling_helper(resamples: int, samples: int) -> None:
22+
"""Benchmark the resampling helper."""
23+
helper = _ResamplingHelper(
24+
ResamplerConfig(
25+
resampling_period_s=1.0,
26+
max_data_age_in_periods=3.0,
27+
resampling_function=nop,
28+
)
29+
)
30+
now = datetime.now(timezone.utc)
31+
32+
def _do_work() -> None:
33+
nonlocal now
34+
for _n_resample in range(resamples):
35+
for _n_sample in range(samples):
36+
now = now + timedelta(seconds=1 / samples)
37+
helper.add_sample(Sample(now, 0.0))
38+
helper.resample(now)
39+
40+
print(timeit(_do_work, number=5))
41+
42+
43+
def _benchmark() -> None:
44+
for resamples in [10, 100, 1000]:
45+
for samples in [10, 100, 1000]:
46+
print(f"{resamples=} {samples=}")
47+
_benchmark_resampling_helper(resamples, samples)
48+
49+
50+
if __name__ == "__main__":
51+
_benchmark()

0 commit comments

Comments
 (0)