-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfers.py
355 lines (252 loc) · 12 KB
/
transfers.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
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
from concurrent.futures import ThreadPoolExecutor
import concurrent.futures
import mmap
import socket
import sys
import zerorpc
import time
from typing import List
import numpy
from multiprocessing import shared_memory
import momentumx as mx
class Sender:
def __init__(self, num_payloads: int, payload_size_mb: int, num_workers: int) -> None:
self.num_payloads = num_payloads
self.payload_size_mb = payload_size_mb
self.num_workers = num_workers
print("Creating Payloads")
self.payloads: List[bytes] = [numpy.random.bytes(self.payload_size_mb * 1024 * 1024) for _ in range(self.num_payloads)]
def send(self, worker_idx: int = 0):
pass
def send_parallel(self):
futs = []
with ThreadPoolExecutor(max_workers=self.num_workers) as executor:
for worker_idx in range(self.num_workers):
futs.append(executor.submit(self.send, worker_idx))
def recv(self):
pass
def __str__(self):
return f"<{self.__class__} num_payloads={self.num_payloads} payload_size_mb={self.payload_size_mb}>"
def __repr__(self):
return str(self)
class SocketSender(Sender):
def __init__(self, num_payloads: int, payload_size_mb: int, num_workers: int) -> None:
super().__init__(num_payloads, payload_size_mb, num_workers)
self.host = "127.0.0.1"
self.port = 65432
self.socket_chunk_size = 4096
def send(self, worker_idx: int = 0):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((self.host, self.port + worker_idx))
print("SEND", "connected to port")
for p_idx, payload in enumerate(self.payloads):
if p_idx % self.num_workers != worker_idx:
continue
print("SEND", "payload", p_idx, len(payload))
# Send the payload in chunks of 4096 bytes
n = len(payload) // self.socket_chunk_size
for idx, chunk in enumerate([payload[i : i+self.socket_chunk_size] for i in range(0, len(payload), n)]):
if idx % 2500 == 0:
print(f"Sending chunk {idx} of payload {p_idx} in worker {worker_idx}")
s.sendall(chunk)
class MmapSender(Sender):
def __init__(self, num_payloads: int, payload_size_mb: int, num_workers: int) -> None:
super().__init__(num_payloads, payload_size_mb, num_workers)
def send(self, worker_idx: int = 0):
block_size = self.payload_size_mb * 1024 * 1024
with mmap.mmap(-1, length=block_size * self.num_payloads, access=mmap.ACCESS_WRITE) as mmap_obj:
for idx, payload in enumerate(self.payloads):
if idx % self.num_workers != worker_idx:
continue
print(f"Writing payload {idx} in worker {worker_idx}")
mmap_obj[(block_size * idx): block_size * (idx+ 1)] = payload
class RPCSender(Sender):
def __init__(self, num_payloads: int, payload_size_mb: int, num_workers: int) -> None:
super().__init__(num_payloads, payload_size_mb, num_workers)
def send(self, worker_idx: int = 0):
class RPC(object):
def __init__(self, payloads):
self.payloads = payloads
def get(self, idx):
print(f"Sending payload {idx}")
return self.payloads[idx]
s = zerorpc.Server(RPC(self.payloads))
print(f"Starting server in worker {worker_idx}")
s.bind(f"tcp://0.0.0.0:{4242 + worker_idx}")
s.run()
class SharedMemorySender(Sender):
def __init__(self, num_payloads: int, payload_size_mb: int, num_workers: int) -> None:
self.block_size = payload_size_mb * 1024 * 1024
self.shared_memory = shared_memory.SharedMemory(name="test", create=True, size=num_payloads * self.block_size)
super().__init__(num_payloads, payload_size_mb, num_workers)
def send(self, worker_idx: int = 0):
for idx, payload in enumerate(self.payloads):
if idx % self.num_workers != worker_idx:
continue
print(f"Writing payload {idx} in worker {worker_idx}")
self.shared_memory.buf[(self.block_size * idx): self.block_size * (idx+ 1)] = payload
class MomentumXSender(Sender):
def __init__(self, num_payloads: int, payload_size_mb: int, num_workers: int) -> None:
self.block_size = payload_size_mb * 1024 * 1024
self.num_workers = num_workers
self.num_payloads = num_payloads
super().__init__(num_payloads, payload_size_mb, num_workers)
def send(self, worker_idx: int = 0):
stream = mx.Producer(
f'pycon_test_{worker_idx}',
buffer_size=self.block_size,
buffer_count=self.num_payloads,
sync=self.num_workers == 1
)
min_subscribers = 1
print("waiting for subscriber(s)")
while stream.subscriber_count < min_subscribers:
pass
print("All expected subscribers are ready")
# Write the series 0-999 to a consumer
for idx, payload in enumerate(self.payloads):
# if stream.subscriber_count == 0:
# cancel_event.wait(0.5)
if idx % self.num_workers != worker_idx:
continue
# Note: sending strings directly is possible via the send_string call
# elif stream.send_string(str(n)):
# print(f"Sent: {n}")
print(f"Sending payload {idx}")
buffer = stream.next_to_send()
buffer.write(payload)
buffer.send()
class Receiver:
def __init__(self, num_payloads: int, payload_size_mb: int, num_workers: int) -> None:
self.num_payloads = num_payloads
self.payload_size_mb = payload_size_mb
self.num_workers = num_workers
self.buffer: List[bytes] = []
self.throughputs = []
self.payloads = []
def recv(self, worker_idx: int = 0):
pass
def recv_parallel(self):
futs = []
with ThreadPoolExecutor(max_workers=self.num_workers) as executor:
# start = time.time()
for worker_idx in range(self.num_workers):
futs.append(executor.submit(self.recv, worker_idx))
futs = concurrent.futures.wait(futs, return_when="ALL_COMPLETED")
# end = time.time()
assert len(futs.not_done) == 0
print("Avg. Throughput recv_parallel", sum(self.throughputs) / len(self.throughputs), "GBps")
def __str__(self):
return f"<{self.__class__} num_payloads={self.num_payloads} payload_size_mb={self.payload_size_mb}>"
def __repr__(self):
return str(self)
class SocketReceiver(Receiver):
def __init__(self, num_payloads: int, payload_size_mb: int, num_workers: int) -> None:
super().__init__(num_payloads, payload_size_mb, num_workers)
self.host = "127.0.0.1"
self.port = 65432
self.socket_chunk_size = 4096
def recv(self, worker_idx: int = 0):
start = time.time()
buffer = []
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((self.host, self.port + worker_idx))
s.listen()
conn, addr = s.accept()
print("Connected to", (self.host, self.port + worker_idx))
counter = 0
with conn:
while True:
data = conn.recv(self.socket_chunk_size)
buffer.append(data)
if not data:
break
counter += 1
if counter % 2500 == 0:
print(f"Received chunk: {counter}")
end = time.time()
# self.times.append(end-start)
# if worker_idx == 0:
# total = sum(map(lambda x: len(x), self.buffer))
total = sum(map(lambda x: len(x), buffer))
throughput = (total / (1024 ** 3)) / (end-start)
print("Throughput recv", throughput, "GBps")
self.throughputs.append(throughput)
class MmapReceiver(Receiver):
def __init__(self, num_payloads: int, payload_size_mb: int, num_workers: int) -> None:
super().__init__(num_payloads, payload_size_mb, num_workers)
def recv(self, worker_idx: int = 0):
buffer = None
block_size = self.payload_size_mb * 1024 * 1024
start = time.time()
with mmap.mmap(-1, length=block_size * self.num_payloads, access=mmap.ACCESS_WRITE) as mmap_obj:
for idx in range(self.num_payloads):
if idx % self.num_workers != worker_idx:
continue
print(f"Reading payload {idx} in worker {worker_idx}")
buffer = mmap_obj[(block_size * idx): block_size * (idx+ 1)]
end = time.time()
# if worker_idx == 0:
# total = sum(map(lambda x: len(x), buffer))
total = len(buffer)
throughput = (total / (1024 ** 3)) / (end - start)
print("Throughput recv", throughput, "GBps")
self.throughputs.append(throughput)
class RPCReceiver(Receiver):
def __init__(self, num_payloads: int, payload_size_mb: int, num_workers: int) -> None:
super().__init__(num_payloads, payload_size_mb, num_workers)
def recv(self, worker_idx: int = 0):
buffer = None
c = zerorpc.Client()
c.connect(f"tcp://0.0.0.0:{4242 + worker_idx}")
start = time.time()
for idx in range(self.num_payloads):
if idx % self.num_workers != worker_idx:
continue
print(f"Reading payload {idx}")
buffer = c.get(idx)
end = time.time()
total = len(buffer)
throughput = (total / (1024 ** 3)) / (end - start)
print("Throughput recv", throughput, "GBps")
self.throughputs.append(throughput)
class SharedMemoryReceiver(Receiver):
def __init__(self, num_payloads: int, payload_size_mb: int, num_workers: int) -> None:
self.block_size = payload_size_mb * 1024 * 1024
self.shared_memory = shared_memory.SharedMemory(name="test", size=num_payloads * self.block_size)
super().__init__(num_payloads, payload_size_mb, num_workers)
def recv(self, worker_idx: int = 0):
buffer = None
start = time.time()
for idx in range(self.num_payloads):
if idx % self.num_workers != worker_idx:
continue
print(f"Reading payload {idx} in worker {worker_idx}")
buffer = bytes(self.shared_memory.buf[(self.block_size * idx): self.block_size * (idx+ 1)])
end = time.time()
total = len(buffer)
throughput = (total / (1024 ** 3)) / (end - start)
print("Throughput recv", throughput, "GBps")
self.throughputs.append(throughput)
class MomentumXReceiver(Receiver):
def __init__(self, num_payloads: int, payload_size_mb: int, num_workers: int) -> None:
self.block_size = payload_size_mb * 1024 * 1024
super().__init__(num_payloads, payload_size_mb, num_workers)
def recv(self, worker_idx: int = 0):
print(f"Creating consumer in worker: {worker_idx}")
stream = mx.Consumer(f'pycon_test_{worker_idx}')
payload: bytes = None
start = time.time()
while stream.has_next:
print(f"Readingfrom stream in worker: {worker_idx}")
buffer = stream.receive()
if buffer is not None:
payload = buffer.read(buffer.data_size)
print(f"Recieved Buffer in worker: {worker_idx}")
end = time.time()
print("RECV", f"{sys.getsizeof(payload)}")
print("RECV", f"{end - start} secs")
total = len(payload)
throughput = (total / (1024 ** 3)) / (end - start)
print("Throughput recv", throughput, "GBps")
self.throughputs.append(throughput)