aboutsummaryrefslogtreecommitdiff
path: root/tools/scriptgenerator.py
blob: cc3e1eafc093f6f95dc1e7166237a93b707554e9 (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
import argparse
import importlib.machinery as mach

from rumba.storyboard import *
import rumba.log as log
from rumba.utils import ExperimentManager, PAUSE_SWAPOUT

client1 = Client(
    "rinaperf",
    options="-t perf -s 1000 -c 0",
    c_id='rinaperf_^C'  # To differentiate
)


client2 = Client(
    "rinaperf",
    options="-t perf -s 1000 -D <duration>",
    shutdown="",
    c_id='rinaperf_-D'  # To differentiate
)


server = Server(
    "rinaperf",
    options="-l",
    arrival_rate=0.5,
    mean_duration=5,
    clients=[client1, client2]
)


def main(duration, exp, run=False, script='generated_script.txt'):
    story = StoryBoard(duration)
    story.set_experiment(exp)
    story.add_server_on_node(server, exp.nodes[0])
    client1.add_node(exp.nodes[1])
    if len(exp.nodes) == 2:
        third_n = exp.nodes[1]
    else:
        third_n = exp.nodes[2]
    client2.add_node(third_n)
    story.generate_script()

    with open(script, 'w') as f:
        f.write('################################################\n')
        f.write('# SCRIPT GENERATED WITH RUMBA SCRIPT GENERATOR #\n')
        f.write('################################################\n')
        story.write_script(f)

    if run:
        with ExperimentManager(exp, swap_out_strategy=PAUSE_SWAPOUT):
            exp.swap_in()
            exp.bootstrap_prototype()
            story.start()


if __name__ == '__main__':

    description = "Storyboard generator for Rumba scripts"
    epilog = "2018 Marco Capitani <m.capitani@nextworks.it>"

    parser = argparse.ArgumentParser(description=description,
                                     epilog=epilog)
    parser.add_argument('exp', metavar='EXPERIMENT', type=str,
                        help='The experiment file.')
    parser.add_argument('-O', '--object', metavar='OBJECT', type=str,
                        default='exp',
                        help='The Python variable name of '
                             'the experiment object defined in the file')
    parser.add_argument('d', metavar='DURATION', type=float,
                        help='The required duration of the storyboard')
    parser.add_argument('-R', '--run', action='store_true',
                        help='Run the storyboard after script generation.')
    parser.add_argument('-S', '--script', type=str,
                        default='generated_script.txt',
                        help='Name of the output script.')

    args = parser.parse_args()

    try:
        exp_module = mach.SourceFileLoader('exp', args.exp).load_module()
        experiment = getattr(exp_module, args.object)
        main(args.d, experiment, args.run, args.script)
    except AttributeError:
        logger.error('Module %s has no attribute %s', args.exp, args.object)
    except Exception as e:
        logger.error('Could not load file %s. %s.', args.exp, str(e))
    log.flush_log()