File: test_single_11.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 (53 lines) | stat: -rw-r--r-- 1,560 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- Mode: Python -*-

# no-holds barred, test a single channel's pipelining speed

import string
import socket

def build_request_chain (num, host, request_size):
    s = 'GET /test%d.html HTTP/1.1\r\nHost: %s\r\n\r\n' % (request_size, host)
    sl = [s] * (num-1)
    sl.append (
            'GET /test%d.html HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n' % (
                    request_size, host
                    )
            )
    return string.join (sl, '')

import time

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

    def end (self):
        return time.time() - self.start

if __name__ == '__main__':
    import sys
    if len(sys.argv) != 5:
        print 'usage: %s <host> <port> <request-size> <num-requests>' % (sys.argv[0])
    else:
        host = sys.argv[1]
        [port, request_size, num_requests] = map (
                string.atoi,
                sys.argv[2:]
                )
        chain = build_request_chain (num_requests, host, request_size)
        import socket
        s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
        s.connect ((host,port))
        t = timer()
        s.send (chain)
        num_bytes = 0
        while 1:
            data = s.recv(16384)
            if not data:
                break
            else:
                num_bytes = num_bytes + len(data)
        total_time = t.end()
        print 'total bytes received: %d' % num_bytes
        print 'total time: %.2f sec' % (total_time)
        print 'transactions/sec: %.2f' % (num_requests/total_time)