-
-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathperftest.py
More file actions
93 lines (69 loc) · 2.42 KB
/
perftest.py
File metadata and controls
93 lines (69 loc) · 2.42 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
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
"""Script for testing update performance on devices."""
import asyncio
import time
import asyncclick as click
import pandas as pd
from kasa import Discover
async def _update(dev, lock=None):
if lock is not None:
await lock.acquire()
await asyncio.sleep(2)
try:
start_time = time.time()
# print("%s >> Updating" % id(dev))
await dev.update()
# print("%s >> done in %s" % (id(dev), time.time() - start_time))
return {"id": f"{id(dev)}-{dev.model}", "took": (time.time() - start_time)}
finally:
if lock is not None:
lock.release()
async def _update_concurrently(devs):
start_time = time.time()
update_futures = [asyncio.ensure_future(_update(dev)) for dev in devs]
await asyncio.gather(*update_futures)
return {"type": "concurrently", "took": (time.time() - start_time)}
async def _update_sequentially(devs):
start_time = time.time()
for dev in devs:
await _update(dev)
return {"type": "sequential", "took": (time.time() - start_time)}
@click.command()
@click.argument("addrs", nargs=-1)
@click.option("--rounds", default=5)
async def main(addrs, rounds):
"""Test update performance on given devices."""
print(f"Running {rounds} rounds on {addrs}")
devs = []
for addr in addrs:
try:
dev = await Discover.discover_single(addr)
devs.append(dev)
except Exception as ex:
print(f"unable to add {addr}: {ex}")
data = []
test_gathered = True
if test_gathered:
print("=== Testing using gather on all devices ===")
for _i in range(rounds):
data.append(await _update_concurrently(devs))
await asyncio.sleep(2)
await asyncio.sleep(5)
for _i in range(rounds):
data.append(await _update_sequentially(devs))
await asyncio.sleep(2)
df = pd.DataFrame(data)
print(df.groupby("type").describe())
print("=== Testing per-device performance ===")
futs = []
data = []
locks = {dev: asyncio.Lock() for dev in devs}
for _i in range(rounds):
for dev in devs:
futs.append(asyncio.ensure_future(_update(dev, locks[dev])))
for fut in asyncio.as_completed(futs):
res = await fut
data.append(res)
df = pd.DataFrame(data)
print(df.groupby("id").describe())
if __name__ == "__main__":
main(_anyio_backend="asyncio")