File: bench_sendall.py

package info (click to toggle)
python-gevent 0.13.6-1%2Bnmu3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,324 kB
  • sloc: python: 13,296; makefile: 95; ansic: 37
file content (35 lines) | stat: -rw-r--r-- 742 bytes parent folder | download
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
#! /usr/bin/env python
import time
from gevent import socket
from gevent.server import StreamServer


def recvall(socket, addr):
    while socket.recv(4096):
        pass


def main():
    server = StreamServer(("127.0.0.1", 0), recvall)
    server.start()

    length = 50 * 0x100000
    data = "x" * length

    spent_total = 0
    N = 10

    conn = socket.create_connection((server.server_host, server.server_port))
    for i in range(N):
        start = time.time()
        conn.sendall(data)
        spent = time.time() - start
        print "%.2f MB/s" % (length / spent / 0x100000)
        spent_total += spent

    print "~ %.2f MB/s" % (length * N / spent_total / 0x100000)
    server.stop()


if __name__ == "__main__":
    main()