63d842f1b7e4ea58b7c5567ed9d5551e9ea95acb
[nepi.git] / util_scripts / tunbench.py
1 import os
2 import sys
3 import threading
4 import time
5 import cProfile
6 import pstats
7
8 from nepi.util import tunchannel
9
10 remote = open("/dev/zero","r+b")
11 tun = open("/dev/zero","r+b")
12
13 def rwrite(remote, packet, remote_fd = remote.fileno(), os_write=os.write, len=len):
14     global bytes
15     bytes += len(packet)
16     return os_write(remote_fd, packet)
17
18 def rread(remote, maxlen, remote_fd = remote.fileno(), os_read=os.read):
19     global bytes
20     rv = os_read(remote_fd, maxlen)
21     bytes += len(rv)
22     return rv
23
24 def test(cipher, passphrase, plr=None, queuemodule=None):
25    if plr:
26         import random
27         def accept(packet, direction, rng=random.random):
28             return rng() > 0.5
29    else:
30         accept = None
31    if queuemodule:
32         import os, os.path
33         sys.path.append(os.path.join(
34             os.path.dirname(__file__), 
35             'src','nepi','testbeds','planetlab','scripts'))
36         queuemodule = __import__(queuemodule)
37         queueclass = queuemodule.queueclass
38    else:
39         queueclass = None
40    TERMINATE = []
41    SUSPEND = []
42
43    def stopme():
44        time.sleep(100)
45        TERMINATE.append(None)
46
47    t = threading.Thread(target=stopme)
48    t.start()
49    tunchannel.tun_fwd(tun, remote, True, True, passphrase, True, TERMINATE,
50             SUSPEND, None, tunkqueue=500, rwrite = rwrite, rread = rread, 
51             cipher=cipher, queueclass=queueclass, accept_local = accept,
52             accept_remote = accept)
53
54 # Swallow exceptions on decryption
55 def decrypt(packet, crypter, super=tunchannel.decrypt):
56     try:
57         return super(packet, crypter)
58     except:
59         return packet
60 tunchannel.decrypt = decrypt
61
62 for cipher in (None, 'AES', 'Blowfish', 'DES', 'DES3'):
63     if cipher is None:
64         passphrase = None
65     else:
66         passphrase = 'Abracadabra'
67     bytes = 0
68     cProfile.runctx('test(%r,%r)' % (cipher, passphrase),globals(),locals(),'tunchannel.%s.profile' % (cipher,))
69     
70     print "Profile (%s):" % ( cipher, )
71     pstats.Stats('tunchannel.%s.profile' % cipher).strip_dirs().sort_stats('time').print_stats()
72     
73     print "Bandwidth (%s): %.4fMb/s" % ( cipher, bytes / 200.0 * 8 / 2**20, )
74
75 bytes = 0
76 cProfile.runctx('test(None,None,0.5)',globals(),locals(),'tunchannel.plr.profile')
77
78 print "Profile (50% PLR):"
79 pstats.Stats('tunchannel.plr.profile').strip_dirs().sort_stats('time').print_stats()
80
81 print "Bandwidth (50%% PLR): %.4fMb/s" % ( bytes / 200.0 * 8 / 2**20, )
82
83 bytes = 0
84 cProfile.runctx('test(None,None,None,"tosqueue")',globals(),locals(),'tunchannel.tos.profile')
85
86 print "Profile (TOS):"
87 pstats.Stats('tunchannel.tos.profile').strip_dirs().sort_stats('time').print_stats()
88
89 print "Bandwidth (TOS): %.4fMb/s" % ( bytes / 200.0 * 8 / 2**20, )
90