File: speed_test.py

package info (click to toggle)
python-bsblan 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,196 kB
  • sloc: python: 6,864; makefile: 3
file content (382 lines) | stat: -rw-r--r-- 11,553 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
"""Test speed comparison for BSB-LAN API calls.

Compares different approaches:
1. Multiple parallel calls (current approach)
2. Combined read_parameters call
3. With/without parameter filtering

Usage:
    # Set environment variables (optional - will use mDNS discovery if not set)
    export BSBLAN_HOST=10.0.2.60
    export BSBLAN_PORT=80
    export BSBLAN_PASSKEY=your_passkey  # if needed

    # Run the test
    python examples/speed_test.py
"""

from __future__ import annotations

import asyncio
import statistics
import time
from dataclasses import dataclass
from typing import TYPE_CHECKING

from bsblan import BSBLAN, BSBLANConfig
from discovery import get_bsblan_host, get_config_from_env

if TYPE_CHECKING:
    from collections.abc import Awaitable, Callable

# Test configuration
NUM_WARMUP_RUNS = 2
NUM_TEST_RUNS = 10

# Parameters used in different tests
INFO_PARAMS = ["6224"]  # Current firmware version
STATIC_PARAMS = ["714", "716"]  # Min/max temp setpoints
ALL_PARAMS = INFO_PARAMS + STATIC_PARAMS

# Large parameter sets for scalability testing
# Heating parameters
HEATING_PARAMS = ["700", "710", "900", "8000", "8740", "8749"]

# Sensor parameters
SENSOR_PARAMS = ["8700", "8740"]

# Hot water parameters (a good mix of config and state)
HOT_WATER_PARAMS = [
    "1600",  # operating_mode
    "1601",  # eco_mode_selection
    "1610",  # nominal_setpoint
    "1614",  # nominal_setpoint_max
    "1612",  # reduced_setpoint
    "1620",  # release
    "1630",  # dhw_charging_priority
    "1640",  # legionella_function
    "1641",  # legionella_function_periodicity
    "1642",  # legionella_function_day
    "1644",  # legionella_function_time
    "1645",  # legionella_function_setpoint
    "8830",  # dhw_actual_value_top_temperature
    "8820",  # state_dhw_pump
]

# Combined large set (~20 params)
LARGE_PARAM_SET = HEATING_PARAMS + SENSOR_PARAMS + HOT_WATER_PARAMS[:8]  # ~16 params

# Extra large set (~22 params)
XLARGE_PARAM_SET = HEATING_PARAMS + SENSOR_PARAMS + HOT_WATER_PARAMS  # ~22 params


@dataclass
class BenchmarkResult:
    """Results from a speed benchmark."""

    name: str
    times: list[float]

    @property
    def avg(self) -> float:
        """Average time."""
        return statistics.mean(self.times)

    @property
    def min(self) -> float:
        """Minimum time."""
        return min(self.times)

    @property
    def max(self) -> float:
        """Maximum time."""
        return max(self.times)

    @property
    def median(self) -> float:
        """Median time."""
        return statistics.median(self.times)

    @property
    def stdev(self) -> float:
        """Standard deviation."""
        return statistics.stdev(self.times) if len(self.times) > 1 else 0.0

    def __str__(self) -> str:
        """Format results as string."""
        return (
            f"{self.name}:\n"
            f"  avg={self.avg:.3f}s, median={self.median:.3f}s, "
            f"min={self.min:.3f}s, max={self.max:.3f}s, stdev={self.stdev:.3f}s"
        )


async def run_test(
    name: str,
    test_fn: Callable[[], Awaitable[object]],
    num_runs: int = NUM_TEST_RUNS,
    warmup_runs: int = NUM_WARMUP_RUNS,
) -> BenchmarkResult:
    """Run a test function multiple times and collect timing stats.

    Args:
        name: Test name for display.
        test_fn: Async function to test.
        num_runs: Number of timed test runs.
        warmup_runs: Number of warmup runs (not timed).

    Returns:
        BenchmarkResult with timing statistics.

    """
    # Warmup runs (not counted)
    for _ in range(warmup_runs):
        await test_fn()

    # Timed runs
    times: list[float] = []
    for i in range(num_runs):
        start = time.perf_counter()
        await test_fn()
        elapsed = time.perf_counter() - start
        times.append(elapsed)
        print(f"  Run {i + 1}/{num_runs}: {elapsed:.3f}s")

    return BenchmarkResult(name=name, times=times)


async def bench_3_parallel_calls(bsblan: BSBLAN) -> None:
    """Benchmark 3 parallel calls (current initialization approach)."""
    await asyncio.gather(
        bsblan.device(),
        bsblan.info(),
        bsblan.static_values(),
    )


async def bench_2_parallel_calls(bsblan: BSBLAN) -> None:
    """Benchmark 2 calls: device + combined read_parameters."""
    await asyncio.gather(
        bsblan.device(),
        bsblan.read_parameters(ALL_PARAMS),
    )


async def bench_1_read_params(bsblan: BSBLAN) -> None:
    """Benchmark single read_parameters call with all params."""
    await bsblan.read_parameters(ALL_PARAMS)


async def bench_static_values_filtered(bsblan: BSBLAN) -> None:
    """Benchmark static_values with include filter."""
    await bsblan.static_values(include=["min_temp"])


async def bench_info_filtered(bsblan: BSBLAN) -> None:
    """Benchmark info() with include filter."""
    await bsblan.info(include=["device_identification"])


async def bench_large_params_single_call(bsblan: BSBLAN) -> None:
    """Benchmark single call with ~16 parameters."""
    await bsblan.read_parameters(LARGE_PARAM_SET)


async def bench_xlarge_params_single_call(bsblan: BSBLAN) -> None:
    """Benchmark single call with ~22 parameters."""
    await bsblan.read_parameters(XLARGE_PARAM_SET)


async def bench_large_params_4_parallel_calls(bsblan: BSBLAN) -> None:
    """Benchmark 4 parallel calls, each with ~4 parameters."""
    chunk_size = len(LARGE_PARAM_SET) // 4
    chunks = [
        LARGE_PARAM_SET[i : i + chunk_size]
        for i in range(0, len(LARGE_PARAM_SET), chunk_size)
    ]
    await asyncio.gather(*[bsblan.read_parameters(chunk) for chunk in chunks])


async def bench_xlarge_params_4_parallel_calls(bsblan: BSBLAN) -> None:
    """Benchmark 4 parallel calls splitting ~22 parameters."""
    chunk_size = len(XLARGE_PARAM_SET) // 4
    chunks = [
        XLARGE_PARAM_SET[i : i + chunk_size]
        for i in range(0, len(XLARGE_PARAM_SET), chunk_size)
    ]
    await asyncio.gather(*[bsblan.read_parameters(chunk) for chunk in chunks])


async def bench_xlarge_params_2_parallel_calls(bsblan: BSBLAN) -> None:
    """Benchmark 2 parallel calls splitting ~22 parameters."""
    mid = len(XLARGE_PARAM_SET) // 2
    await asyncio.gather(
        bsblan.read_parameters(XLARGE_PARAM_SET[:mid]),
        bsblan.read_parameters(XLARGE_PARAM_SET[mid:]),
    )


async def bench_info_only(bsblan: BSBLAN) -> None:
    """Benchmark info() call only."""
    await bsblan.info()


async def bench_static_values_only(bsblan: BSBLAN) -> None:
    """Benchmark static_values() call only."""
    await bsblan.static_values()


async def run_all_benchmarks(bsblan: BSBLAN) -> list[BenchmarkResult]:
    """Run all speed benchmarks and return results."""
    results: list[BenchmarkResult] = []

    # Define basic tests
    basic_tests = [
        (
            "Test 1: 3 parallel calls (device + info + static_values)",
            "3 parallel calls",
            lambda: bench_3_parallel_calls(bsblan),
        ),
        (
            "Test 2: 2 parallel calls (device + read_parameters)",
            "2 parallel calls",
            lambda: bench_2_parallel_calls(bsblan),
        ),
        (
            "Test 3: Single read_parameters call",
            "1 read_parameters",
            lambda: bench_1_read_params(bsblan),
        ),
        (
            "Test 4: static_values with include filter (min_temp only)",
            "static_values (filtered)",
            lambda: bench_static_values_filtered(bsblan),
        ),
        (
            "Test 5: info with include filter (device_identification only)",
            "info (filtered)",
            lambda: bench_info_filtered(bsblan),
        ),
        (
            "Test 6: static_values without filter (all params)",
            "static_values (all)",
            lambda: bench_static_values_only(bsblan),
        ),
    ]

    for desc, name, bench_fn in basic_tests:
        print(f"\n{desc}")
        result = await run_test(name, bench_fn)
        results.append(result)

    # Scalability tests
    print("\n" + "=" * 60)
    print("SCALABILITY TESTS - Many Parameters")
    print("=" * 60)

    scalability_tests = [
        (
            f"Test 7: Single call with {len(LARGE_PARAM_SET)} params",
            f"1 call ({len(LARGE_PARAM_SET)} params)",
            lambda: bench_large_params_single_call(bsblan),
        ),
        (
            f"Test 8: 4 parallel calls ({len(LARGE_PARAM_SET)} params split)",
            f"4 calls ({len(LARGE_PARAM_SET)} params)",
            lambda: bench_large_params_4_parallel_calls(bsblan),
        ),
        (
            f"Test 9: Single call with {len(XLARGE_PARAM_SET)} params",
            f"1 call ({len(XLARGE_PARAM_SET)} params)",
            lambda: bench_xlarge_params_single_call(bsblan),
        ),
        (
            f"Test 10: 2 parallel calls ({len(XLARGE_PARAM_SET)} params split)",
            f"2 calls ({len(XLARGE_PARAM_SET)} params)",
            lambda: bench_xlarge_params_2_parallel_calls(bsblan),
        ),
        (
            f"Test 11: 4 parallel calls ({len(XLARGE_PARAM_SET)} params split)",
            f"4 calls ({len(XLARGE_PARAM_SET)} params)",
            lambda: bench_xlarge_params_4_parallel_calls(bsblan),
        ),
    ]

    for desc, name, bench_fn in scalability_tests:
        print(f"\n{desc}")
        result = await run_test(name, bench_fn)
        results.append(result)

    return results


def print_results(results: list[BenchmarkResult]) -> None:
    """Print test results summary."""
    print("\n" + "=" * 60)
    print("RESULTS SUMMARY")
    print("=" * 60)

    for r in results:
        print(f"\n{r}")

    # Compare approaches
    baseline = results[0].avg  # 3 parallel calls
    print("\n" + "-" * 60)
    print("COMPARISON (vs 3 parallel calls baseline)")
    print("-" * 60)

    for r in results[1:]:
        diff = baseline - r.avg
        pct = (diff / baseline) * 100 if baseline > 0 else 0
        faster_slower = "faster" if diff > 0 else "slower"
        print(f"{r.name}: {abs(diff):.3f}s {faster_slower} ({abs(pct):.1f}%)")

    # Best approach
    best = min(results, key=lambda x: x.avg)
    print(f"\nāœ“ Best approach: {best.name} (avg: {best.avg:.3f}s)")


async def main() -> None:
    """Run speed comparison tests."""
    print("=" * 60)
    print("BSB-LAN API Speed Comparison Test")
    print("=" * 60)

    # Get configuration from environment or discovery
    env_config = get_config_from_env()

    try:
        host, port = await get_bsblan_host(discovery_seconds=3.0)
    except RuntimeError:
        print("\nNo BSB-LAN device found!")
        print("Set BSBLAN_HOST environment variable or enable mDNS on your device.")
        return

    print(f"\nConnecting to: {host}:{port}")

    # Build config - cast values to str for type safety
    passkey = env_config.get("passkey")
    username = env_config.get("username")
    password = env_config.get("password")

    config = BSBLANConfig(
        host=host,
        port=port,
        passkey=str(passkey) if passkey else None,
        username=str(username) if username else None,
        password=str(password) if password else None,
    )

    async with BSBLAN(config) as bsblan:
        await bsblan.initialize()
        print("āœ“ BSB-LAN client initialized\n")

        print(f"Running {NUM_TEST_RUNS} test runs with {NUM_WARMUP_RUNS} warmup runs\n")
        print("-" * 60)

        results = await run_all_benchmarks(bsblan)
        print_results(results)


if __name__ == "__main__":
    asyncio.run(main())