Skip to content

Commit 04a2a6e

Browse files
committed
spam_ham
1 parent 62915fe commit 04a2a6e

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

example/spam_ham.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env python3
2+
3+
from collections import OrderedDict
4+
import time
5+
import logging
6+
import gc
7+
8+
from p4p import Value
9+
from p4p.client.thread import Context
10+
11+
class Tracker:
12+
def __init__(self, pv:str, ctxt:Context, n:int, pvReq:str):
13+
self.prev = None
14+
self.nwakes = 0
15+
self.nupdate = 0
16+
self.nskip = 0
17+
self.S = ctxt.monitor(pv, self._update,
18+
request=pvReq,
19+
batch_limit=n,
20+
notify_disconnect=True)
21+
22+
def _update(self, u):
23+
self.nwakes += 1
24+
if isinstance(u, Value):
25+
u = [u]
26+
27+
if isinstance(u, list):
28+
for v in u:
29+
cnt = v.value
30+
if not isinstance(cnt, (int, float)):
31+
cnt = cnt[0]
32+
cnt = int(cnt)
33+
self.nupdate += 1
34+
if self.prev is not None:
35+
diff = (cnt - self.prev)&0xffffffff
36+
if diff!=1:
37+
self.nskip += 1
38+
self.prev = cnt
39+
40+
elif self.S:
41+
print(self.S.name, 'Event', u)
42+
43+
def getargs():
44+
from argparse import ArgumentParser
45+
P = ArgumentParser()
46+
P.add_argument('-w', '--wait', metavar='sec.', type=float, default=10.0)
47+
P.add_argument('-H', '--ham', metavar='PV', action='append', default=[])
48+
P.add_argument('-S', '--spam', metavar='PV', action='append', default=[])
49+
P.add_argument('-P', '--pipeline', dest='pipeline', action='store_true', default=None)
50+
P.add_argument('-p', '--no-pipeline', dest='pipeline', action='store_false')
51+
P.add_argument('-Q', '--queueSize', metavar='CNT', type=int, default=10)
52+
return P
53+
54+
def main(args):
55+
logging.basicConfig(level=logging.INFO)
56+
pvReq = [
57+
'queueSize=%d'%args.queueSize,
58+
]
59+
if args.pipeline is not None:
60+
pvReq.append('pipeline='+('true' if args.pipeline else 'false'))
61+
pvReq = 'record[%s]'%(','.join(pvReq))
62+
print('pvRequest', pvReq)
63+
64+
ctxt = Context(nt=False)
65+
66+
trackers = OrderedDict()
67+
68+
T0 = time.monotonic()
69+
for L in (args.ham, args.spam):
70+
for name in L:
71+
trackers[name] = Tracker(name, ctxt, args.queueSize, pvReq=pvReq)
72+
73+
#gc.disable()
74+
try:
75+
time.sleep(args.wait)
76+
except KeyboardInterrupt:
77+
pass
78+
#gc.enable()
79+
80+
for T in trackers.values():
81+
print(T.S._S.stats())
82+
T.S.close()
83+
T1 = time.monotonic()
84+
85+
dT = T1 - T0
86+
print('run time', dT, 'sec')
87+
88+
for name, T in trackers.items():
89+
print(name, T.nwakes/dT, 'wakes/s', T.nupdate/dT, 'updates/s', T.nskip/dT, 'skips/s')
90+
91+
if __name__=='__main__':
92+
main(getargs().parse_args())

0 commit comments

Comments
 (0)