File: stresstest.py

package info (click to toggle)
sshuttle 0.54-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 680 kB
  • ctags: 385
  • sloc: python: 3,438; sh: 160; makefile: 20; ansic: 14
file content (86 lines) | stat: -rwxr-xr-x 2,557 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
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
#!/usr/bin/env python
import sys, os, socket, select, struct, time

listener = socket.socket()
listener.bind(('127.0.0.1', 0))
listener.listen(500)

servers = []
clients = []
remain = {}

NUMCLIENTS = 50
count = 0


while 1:
    if len(clients) < NUMCLIENTS:
        c = socket.socket()
        c.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        c.bind(('0.0.0.0', 0))
        c.connect(listener.getsockname())
        count += 1
        if count >= 16384:
            count = 1
        print 'cli CREATING %d' % count
        b = struct.pack('I', count) + 'x'*count
        remain[c] = count
        print 'cli  >> %r' % len(b)
        c.send(b)
        c.shutdown(socket.SHUT_WR)
        clients.append(c)
        r = [listener]
        time.sleep(0.1)
    else:
        r = [listener]+servers+clients
    print 'select(%d)' % len(r)
    r,w,x = select.select(r, [], [], 5)
    assert(r)
    for i in r:
        if i == listener:
            s,addr = listener.accept()
            servers.append(s)
        elif i in servers:
            b = i.recv(4096)
            print 'srv <<  %r' % len(b)
            if not i in remain:
                assert(len(b) >= 4)
                want = struct.unpack('I', b[:4])[0]
                b = b[4:]
                #i.send('y'*want)
            else:
                want = remain[i]
            if want < len(b):
                print 'weird wanted %d bytes, got %d: %r' % (want, len(b), b)
                assert(want >= len(b))
            want -= len(b)
            remain[i] = want
            if not b:  # EOF
                if want:
                    print 'weird: eof but wanted %d more' % want
                    assert(want == 0)
                i.close()
                servers.remove(i)
                del remain[i]
            else:
                print 'srv  >> %r' % len(b)
                i.send('y'*len(b))
                if not want:
                    i.shutdown(socket.SHUT_WR)
        elif i in clients:
            b = i.recv(4096)
            print 'cli <<  %r' % len(b)
            want = remain[i]
            if want < len(b):
                print 'weird wanted %d bytes, got %d: %r' % (want, len(b), b)
                assert(want >= len(b))
            want -= len(b)
            remain[i] = want
            if not b:  # EOF
                if want:
                    print 'weird: eof but wanted %d more' % want
                    assert(want == 0)
                i.close()
                clients.remove(i)
                del remain[i]
listener.accept()