summaryrefslogtreecommitdiff
path: root/validation/test/P2P_throughput/P2P_throughput_caller.py
blob: 833e1e02c3390d38cf2ff56abfe7ed4e4e208189 (plain)
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
# -*- coding: utf-8 -*-

"""Module allowing to call P2P_throughput, with the adequate configuration"""

import numpy
import P2P_throughput
import sys
import os

class P2P_throughput_caller:

    """P2P throughput caller"""

    def __init__(self):
        from optparse import OptionParser
        dirname = os.path.dirname(__file__)
        self.template_config_file = os.path.join(dirname, "config.py")
        usage = r"""
        %prog report conf_file
            Generate the P2P throughput report based on conf_file

        %prog clear_tokens conf_file
            Clear the tokens on the two iperf hosts specified in conf_file

        %prog set_tokens conf_file
            Set the tokens on the two iperf hosts specified in conf_file

        conf_file must be the path of a file containing exactly the same
        parameters as the template (""" + \
        self.template_config_file + """).\n""" + \
        """
        The tokens must be set on the two iperf hosts before making the
        measurements and cleared when the bench is released."""

        self._commands = dict((n[4:], getattr(self, n))
                              for n in dir(self) if n.startswith('run_'))
        self._parser = OptionParser(usage = usage)

    def run(self, args):
        if len(args) != 2:
            self._parser.error("Bad number of arguments")
        options, args = self._parser.parse_args(args)
        cmd = args[0]
        if cmd not in self._commands:
            self._parser.error("Unknown command \"{0}\".".format(cmd))

        template_config = P2P_throughput.evaluate(self.template_config_file)
        config_file = args[1]

        try:
            config = P2P_throughput.evaluate(config_file)
            expected = sorted(template_config.keys())
            actual = sorted(config.keys())
            assert actual == expected, (actual, expected)
        except IOError:
            self._parser.error(config_file + " cannot be read.")
        except SyntaxError:
            self._parser.error(config_file + " cannot be evaluated.")
        except AttributeError:
            self._parser.error(config_file + " does not contain a dictionnary.")
        except AssertionError as error:
            (actual, expected) = error.args
            message = file_name + " does not contain exactly the same " \
                "parameters as the template (" + \
                self.template_config_file + ").\n" + \
                config_file + \
                " contains the following parameters:\n" + \
                str(actual) + \
                "\n" + \
                 self.template_config_file + \
                 " contains the following parameters:\n" + \
                 str(expected)
            self._parser.error(message)
        except Exception as error:
            self._parser.error("Unknown error.\n" + \
                                   str(type(error)) + "\n" + \
                                   str(error))

        self._commands[cmd](config, config_file)

    def run_report(self, config, config_file):
        P2P_throughput.report(config, config_file)

    def run_set_tokens(self, config, config_file):
        P2P_throughput.set_tokens(config)

    def run_clear_tokens(self, config, config_file):
        P2P_throughput.clear_tokens(config)

if __name__ == "__main__":
    try:
        P2P_throughput_caller().run(sys.argv[1:])
    except RuntimeError, e:
        print >> sys.stderr, "Error:", e
        sys.exit(1)