-
Notifications
You must be signed in to change notification settings - Fork 5
/
spamsignals.py
executable file
·113 lines (83 loc) · 2.51 KB
/
spamsignals.py
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
#!/bin/env python3
import dbus
import dbus.service
import dbus.mainloop.glib
from gi.repository import GLib
import sys
import time
import random
import string
OBJ_PATH = "/org/fubar/signal1"
INTERFACE = "org.fubar.signal1"
def _rs(l):
return ''.join(random.choice(string.ascii_lowercase) for _ in range(l))
count = 0
prev_size = 0
diff_sum = 0.0
start = -1.0
def _spam_handler(ts, size, msg):
global count
global prev_size
global diff_sum
global start
now = time.time()
if size > 2:
count += 1
diff_sum += (now - ts)
elif size == 1:
# Starting a new sequence
start = now
diff_sum = 0.0
count = 0
elif size == 2:
if prev_size > 2 and count > 10:
# Output a result
avg = diff_sum / float(count)
msg_sec = float(count) / (now - start)
mib = (prev_size * msg_sec) / float(1024 * 1024)
print("%d,%f,%f,%f,%d" % (prev_size, avg, msg_sec, mib, count))
diff_sum = 0.0
count = 0
start = now
prev_size = size
class SignalSpammer(dbus.service.Object):
def __init__(self, c, path):
dbus.service.Object.__init__(self, c, path)
@dbus.service.signal(dbus_interface=INTERFACE,
signature='dts')
def Spam(self, ts, pl_len, pl):
pass
@dbus.service.method(dbus_interface=INTERFACE,
in_signature='tt', out_signature='t')
def SpamSignal(self, num_signals, payload_size):
sent = 0
print("SpamSignal: %d, %d" % (num_signals, payload_size))
pl = _rs(payload_size)
for i in range(num_signals):
self.Spam(time.time(), payload_size, pl)
sent += 1
return sent
if __name__ == '__main__':
server = None
if len(sys.argv) != 2:
print("syntax: spamsignals.py [client|server]")
sys.exit(1)
mode = sys.argv[1]
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
if mode == 'server':
name = dbus.service.BusName(INTERFACE, bus)
server = SignalSpammer(bus, OBJ_PATH)
elif mode == 'client':
s = dbus.Interface(bus.get_object(INTERFACE, OBJ_PATH), INTERFACE)
s.connect_to_signal('Spam', _spam_handler)
else:
print("Invalid mode %s" % (mode))
sys.exit(1)
try:
loop = GLib.MainLoop()
loop.run()
except KeyboardInterrupt:
if server:
server.remove_from_connection(bus, OBJ_PATH)
pass