File: throughput.py

package info (click to toggle)
python-mitogen 0.3.39-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,732 kB
  • sloc: python: 24,733; sh: 198; makefile: 74; perl: 19; ansic: 18
file content (104 lines) | stat: -rw-r--r-- 2,801 bytes parent folder | download | duplicates (2)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'''
Measure file service throughput over local, sudo, and (un)compressed SSH.
'''

import getpass
import os
import tempfile

import mitogen
import mitogen.core
import mitogen.service
import ansible_mitogen.affinity


def prepare():
    pass


def transfer(context, path):
    fp = open('/dev/null', 'wb')
    mitogen.service.FileService.get(context, path, fp)
    fp.close()


def fill_with_random(fp, size):
    n = 0
    s = os.urandom(1048576*16)
    while n < size:
        fp.write(s)
        n += len(s)


def run_test(router, fp, s, context):
    fp.seek(0, 2)
    size = fp.tell()
    print('Testing %s...' % (s,))
    context.call(prepare)
    t0 = mitogen.core.now()
    context.call(transfer, router.myself(), fp.name)
    t1 = mitogen.core.now()
    print('%s took %.2f ms to transfer %.2f MiB, %.2f MiB/s' % (
        s, 1000 * (t1 - t0), size / 1048576.0,
        (size / (t1 - t0) / 1048576.0),
    ))


@mitogen.main()
def main(router):
    import optparse
    parser = optparse.OptionParser(description=__doc__)

    parser.add_option(
        '--ssh-python', metavar='CMD', default='python3',
        help='Remote python path (default %default)')
    parser.add_option(
        '--ssh-user', metavar='S', default=getpass.getuser(),
        help='Remote username (default %default)')

    parser.add_option(
        '--sudo-user', metavar='S', default='root',
        help='Sudo username (default %default)')

    parser.add_option('--debug', action='store_true')
    opts, args = parser.parse_args()

    ansible_mitogen.affinity.policy.assign_muxprocess()

    bigfile = tempfile.NamedTemporaryFile()
    fill_with_random(bigfile, 1048576*512)

    file_service = mitogen.service.FileService(router)
    pool = mitogen.service.Pool(router, ())
    file_service.register(bigfile.name)
    pool.add(file_service)
    try:
        context = router.local(debug=opts.debug)
        run_test(router, bigfile, 'local()', context)
        context.shutdown(wait=True)

        context = router.sudo(username=opts.sudo_user, debug=opts.debug)
        run_test(router, bigfile, 'sudo()', context)
        context.shutdown(wait=True)

        context = router.ssh(
            hostname=args[0],
            python_path=opts.ssh_python,
            username=opts.ssh_user,
            compression=False, debug=opts.debug,
        )
        run_test(router, bigfile, 'ssh(compression=False)', context)
        context.shutdown(wait=True)

        context = router.ssh(
            hostname=args[0],
            python_path=opts.ssh_python,
            username=opts.ssh_user,
            compression=True, debug=opts.debug,
        )
        run_test(router, bigfile, 'ssh(compression=True)', context)
        context.shutdown(wait=True)
    finally:
        pool.stop()
        bigfile.close()