-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_utils.py
More file actions
59 lines (47 loc) · 2.08 KB
/
Copy pathbenchmark_utils.py
File metadata and controls
59 lines (47 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Benchmark script to measure performance improvements in utils.py"""
import time
import pandas as pd
from frosta.utils import as_time_series
from frost_sta_client.model.ext.entity_list import EntityList
from frost_sta_client.model.observation import Observation
from frost_sta_client.model.datastream import Datastream
# Create mock observations for testing
def create_mock_observations(count=1000):
"""Create mock observation data for benchmarking"""
datastream = Datastream()
datastream.id = "test-datastream-001"
observations = []
base_time = pd.Timestamp('2024-01-01', tz='UTC')
for i in range(count):
obs = Observation()
obs.phenomenon_time = (base_time + pd.Timedelta(minutes=i)).isoformat()
obs.result = 20.0 + (i % 10) * 0.1 # Simulated sensor values
obs.datastream = datastream
observations.append(obs)
# Create EntityList with required entity_class parameter
entity_list = EntityList('frost_sta_client.model.observation.Observation')
entity_list.entities = observations
return entity_list
# Benchmark
if __name__ == "__main__":
sizes = [100, 500, 1000, 5000, 10000]
print("Benchmarking as_time_series() performance")
print("=" * 60)
for size in sizes:
entity_list = create_mock_observations(size)
# Warm-up
_ = as_time_series(entity_list)
# Measure
iterations = 10
start = time.perf_counter()
for _ in range(iterations):
result = as_time_series(entity_list)
elapsed = time.perf_counter() - start
avg_time = elapsed / iterations * 1000 # Convert to ms
print(f"{size:6d} observations: {avg_time:7.2f} ms/call ({len(result)} items)")
print("\nOptimizations applied:")
print(" ✓ Single-pass iteration (was: two list comprehensions)")
print(" ✓ Fast datetime parsing with utc=True")
print(" ✓ Skip timezone conversion when tz='UTC'")
print(" ✓ Fixed validation logic bug (or vs and)")
print(" ✓ Early return for empty lists")