46bb7670dd5b3f04918cdb753cf7b7cb5d77054c
[nepi.git] / 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    TERMINATE = []
39    def stopme():
40        time.sleep(100)
41        TERMINATE.append(None)
42    t = threading.Thread(target=stopme)
43    t.start()
44    tunchannel.tun_fwd(tun, remote, True, True, passphrase, True, TERMINATE, None, tunkqueue=500,
45         rwrite = rwrite, rread = rread, cipher=cipher, queueclass=queueclass,
46         accept_local = accept, accept_remote = accept)
47
48 # Swallow exceptions on decryption
49 def decrypt(packet, crypter, super=tunchannel.decrypt):
50     try:
51         return super(packet, crypter)
52     except:
53         return packet
54 tunchannel.decrypt = decrypt
55 """
56 for cipher in (None, 'AES', 'Blowfish', 'DES', 'DES3'):
57     if cipher is None:
58         passphrase = None
59     else:
60         passphrase = 'Abracadabra'
61     bytes = 0
62     cProfile.runctx('test(%r,%r)' % (cipher, passphrase),globals(),locals(),'tunchannel.%s.profile' % (cipher,))
63     
64     print "Profile (%s):" % ( cipher, )
65     pstats.Stats('tunchannel.%s.profile' % cipher).strip_dirs().sort_stats('time').print_stats()
66     
67     print "Bandwidth (%s): %.4fMb/s" % ( cipher, bytes / 200.0 * 8 / 2**20, )
68
69 bytes = 0
70 cProfile.runctx('test(None,None,0.5)',globals(),locals(),'tunchannel.plr.profile')
71
72 print "Profile (50% PLR):"
73 pstats.Stats('tunchannel.plr.profile').strip_dirs().sort_stats('time').print_stats()
74
75 print "Bandwidth (50%% PLR): %.4fMb/s" % ( bytes / 200.0 * 8 / 2**20, )
76 """
77
78 bytes = 0
79 cProfile.runctx('test(None,None,None,"tosqueue")',globals(),locals(),'tunchannel.tos.profile')
80
81 print "Profile (TOS):"
82 pstats.Stats('tunchannel.tos.profile').strip_dirs().sort_stats('time').print_stats()
83
84 print "Bandwidth (TOS): %.4fMb/s" % ( bytes / 200.0 * 8 / 2**20, )
85