~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to utilities/local-latency

  • Committer: Danilo Segan
  • Date: 2011-04-22 14:02:29 UTC
  • mto: This revision was merged to the branch mainline in revision 12910.
  • Revision ID: danilo@canonical.com-20110422140229-zhq4d4c2k8jpglhf
Ignore hidden files when building combined JS file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
__metaclass__ = type
4
 
 
5
 
import subprocess
6
 
import sys
7
 
 
8
 
from script_commands import (
9
 
    helps,
10
 
    run_subcommand,
11
 
    UserError,
12
 
    )
13
 
 
14
 
 
15
 
def tc(command):
16
 
    """Run a tc command under sudo.
17
 
 
18
 
    :param tc: The remainder of the command (leaving out tc).
19
 
    """
20
 
    subprocess.call('sudo tc ' + command, shell=True)
21
 
 
22
 
 
23
 
@helps(delay='Length of delay in miliseconds (each way).',
24
 
       port='Port to induce delay on.')
25
 
def start(delay=500, port=443):
26
 
    """Add artificial latency to the lo interface on the specified port."""
27
 
    tc('qdisc add dev lo root handle 1: prio')
28
 
    tc('qdisc add dev lo parent 1:3 handle 30: netem delay %dms' % delay)
29
 
    tc('filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip'
30
 
       ' dport %d 0xffff flowid 1:3' % port)
31
 
    tc('filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip'
32
 
       ' sport %d 0xffff flowid 1:3' % port)
33
 
 
34
 
 
35
 
def stop():
36
 
    """Remove latency from the lo."""
37
 
    tc('qdisc del dev lo root')
38
 
 
39
 
 
40
 
subcommands = {
41
 
    'start': start,
42
 
    'stop': stop,
43
 
    }
44
 
 
45
 
 
46
 
 
47
 
if __name__ == "__main__":
48
 
    try:
49
 
        run_subcommand(subcommands, sys.argv[1:])
50
 
    except UserError as e:
51
 
        sys.stderr.write(str(e)+'\n')