forked from gevent/gevent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_sendall.py
54 lines (37 loc) · 1.1 KB
/
bench_sendall.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
#! /usr/bin/env python
from __future__ import print_function, division, absolute_import
import perf
from gevent import socket
from gevent.server import StreamServer
def recvall(sock, _):
while sock.recv(4096):
pass
N = 10
runs = []
def benchmark(conn, data):
spent_total = 0
for _ in range(N):
start = perf.perf_counter()
conn.sendall(data)
spent = perf.perf_counter() - start
spent_total += spent
runs.append(spent_total)
return spent_total
def main():
runner = perf.Runner()
server = StreamServer(("127.0.0.1", 0), recvall)
server.start()
MB = 1024 * 1024
length = 50 * MB
data = b"x" * length
conn = socket.create_connection((server.server_host, server.server_port))
runner.bench_func('sendall', benchmark, conn, data, inner_loops=N)
conn.close()
server.stop()
if runs:
total = sum(runs)
avg = total / len(runs)
# This is really only true if the perf_counter counts in seconds time
print("~ %.2f MB/s" % (length * N / avg / MB))
if __name__ == "__main__":
main()