comparison bin/collector_loadtest.py @ 2072:d5f1cc9615af

collector: rewrites for asyncio
author drewp@bigasterisk.com
date Sun, 22 May 2022 03:03:43 -0700
parents 2951a690f1ba
children 2c48e92ad5d3
comparison
equal deleted inserted replaced
2071:56a9eaf5e882 2072:d5f1cc9615af
1 #!bin/python 1 #!bin/python
2 import asyncio
2 import logging 3 import logging
3 import time 4 import time
4 from typing import cast 5 from typing import cast
5 6
6 import twisted.internet.reactor 7 from light9.collector.collector_client_asyncio import sendToCollector
7 from light9.collector.collector_client import sendToCollector
8 from light9.effect.settings import DeviceSettings 8 from light9.effect.settings import DeviceSettings
9 from light9.namespaces import DEV, L9 9 from light9.namespaces import DEV, L9
10 from light9.run_local import log 10 from light9.run_local import log
11 from twisted.internet.interfaces import IReactorCore
12 11
13 reactor = cast(IReactorCore, twisted.internet.reactor)
14 log.setLevel(logging.DEBUG) 12 log.setLevel(logging.DEBUG)
15 13
16 14
17 def loadTest(): 15 async def loadTest():
18 print("scheduling loadtest") 16 print("scheduling loadtest")
19 n = 2500 17 n = 200000
20 times = [None] * n 18 period=1
19 times = []
21 session = "loadtest%s" % time.time() 20 session = "loadtest%s" % time.time()
22 offset = 0
23 for i in range(n): 21 for i in range(n):
22 if i % 100 == 0:
23 log.info('sendToCollector %s', i)
24 start = time.time()
25 await sendToCollector(
26 "http://localhost:8202/",
27 session,
28 DeviceSettings(
29 graph=None,
30 settingsList=[
31 [DEV["backlight1"], L9["color"], "#ffffff"], #
32 [DEV["backlight2"], L9["color"], "#ffffff"],
33 [DEV["backlight3"], L9["color"], "#ffffff"],
34 [DEV["backlight4"], L9["color"], "#ffffff"],
35 [DEV["backlight5"], L9["color"], "#ffffff"],
36 [DEV["down2"], L9["color"], "#ffffff"],
37 [DEV["down3"], L9["color"], "#ffffff"],
38 [DEV["down4"], L9["color"], "#ffffff"],
39 [DEV["houseSide"], L9["level"], .8],
40 [DEV["backlight5"], L9["uv"], 0.011]
41 ]))
42 times.append(time.time() - start)
43 await asyncio.sleep(period)
24 44
25 def send(i): 45 print("loadtest done")
26 if i % 100 == 0: 46 with open('/tmp/times', 'w') as f:
27 log.info('sendToCollector %s', i) 47 f.write(''.join('%s\n' % t for t in times))
28 d = sendToCollector(
29 "http://localhost:8202/",
30 session,
31 DeviceSettings(
32 graph=None,
33 settingsList=[
34 [DEV["backlight1"], L9["color"], "#ffffff"], #
35 [DEV["backlight2"], L9["color"], "#ffffff"],
36 [DEV["backlight3"], L9["color"], "#ffffff"],
37 [DEV["backlight4"], L9["color"], "#ffffff"],
38 [DEV["backlight5"], L9["color"], "#ffffff"],
39 [DEV["down2"], L9["color"], "#ffffff"],
40 [DEV["down3"], L9["color"], "#ffffff"],
41 [DEV["down4"], L9["color"], "#ffffff"],
42 [DEV["houseSide"], L9["level"], .8],
43 [DEV["backlight5"], L9["uv"], 0.011]
44 ]))
45
46 def ontime(dt, i=i):
47 times[i] = dt
48
49 d.addCallback(ontime)
50
51 reactor.callLater(offset, send, i)
52 offset += .002
53
54 def done():
55 print("loadtest done")
56 with open('/tmp/times', 'w') as f:
57 f.write(''.join('%s\n' % t for t in times))
58 reactor.stop()
59
60 reactor.callLater(offset + .5, done)
61 reactor.run()
62 48
63 49
64 if __name__ == '__main__': 50 if __name__ == '__main__':
65 loadTest() 51 asyncio.run(loadTest())