File: bench.py

package info (click to toggle)
python-medusa 0.5.4%2Bclean-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 540 kB
  • ctags: 1,009
  • sloc: python: 5,489; makefile: 11
file content (35 lines) | stat: -rw-r--r-- 952 bytes parent folder | download | duplicates (9)
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
# -*- Mode: Python -*-

# benchmark a single channel, pipelined

request = 'GET /index.html HTTP/1.0\r\nConnection: Keep-Alive\r\n\r\n'
last_request = 'GET /index.html HTTP/1.0\r\nConnection: close\r\n\r\n'

import socket
import time

class timer:
    def __init__ (self):
        self.start = time.time()
    def end (self):
        return time.time() - self.start

def bench (host, port=80, n=100):
    s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    s.connect ((host, port))
    t = timer()
    s.send ((request * n) + last_request)
    while 1:
        d = s.recv(65536)
        if not d:
            break
    total = t.end()
    print 'time: %.2f seconds  (%.2f hits/sec)' % (total, n/total)

if __name__ == '__main__':
    import sys
    import string
    if len(sys.argv) < 3:
        print 'usage: %s <host> <port> <count>' % (sys.argv[0])
    else:
        bench (sys.argv[1], string.atoi (sys.argv[2]), string.atoi (sys.argv[3]))