Step 1: Move vm scripts to the right place
[integration/test.git] / test / tools / odl-mdsal-clustering-tests / clustering-performance-test / flow_stats_stability_monitor.py
1 #!/usr/bin/python
2
3 """
4 The script is based on the flow_add_delete_test.py. The only difference is that
5 it doesn't wait till stats are collected, but it triggers inventory data as
6 long as specified and produces an output file it's name is given.
7 """
8
9 import argparse
10 import time
11 from flow_config_blaster import FlowConfigBlaster, get_json_from_file
12 from inventory_crawler import InventoryCrawler
13 from config_cleanup import cleanup_config_odl
14
15
16 def get_time_delta(actualtime, basetime):
17     return actualtime - basetime
18
19
20 def monitor_stats(crawler, monitortime, period):
21     """
22     Check incentory and yields collected data.
23     """
24     basetime = time.time()
25     while True:
26         lastcrawl = time.time()
27         crawler.nodes = 0
28         crawler.crawl_inventory()
29         actualtime = time.time()
30         yield (actualtime, crawler.nodes, crawler.reported_flows, crawler.found_flows)
31         if actualtime > basetime + monitortime:
32             break
33         time.sleep(period-get_time_delta(actualtime, lastcrawl))
34
35
36 if __name__ == "__main__":
37     ############################################################################
38     # This program executes an ODL performance test. The task is executed in
39     # four steps:
40     #
41     # 1. The specified number of flows is added in the 'add cycle' (uses
42     #    flow_config_blaster to blast flows)
43     # 2. The network is polled for flow statistics from the network (using the
44     #    inventory_crawler.py script) to make sure that all flows have been
45     #    properly programmed into the network and the ODL statistics collector
46     #    can properly read them as long as specified
47     # 3. The flows are deleted in the flow cycle. Deletion happens either in
48     #    'bulk' (using the config_cleanup) script or one by one (using the
49     #    flow_config_blaster 'delete' method)
50     # 4. Same as 2. Monitoring and reporting the state of the inventory data
51     #    for a specified period of time.
52     ############################################################################
53
54     parser = argparse.ArgumentParser(description='Flow programming performance test: First adds and then deletes flows '
55                                                  'into the config tree, as specified by optional parameters.')
56
57     parser.add_argument('--host', default='127.0.0.1',
58                         help='Host where odl controller is running (default is 127.0.0.1)')
59     parser.add_argument('--port', default='8181',
60                         help='Port on which odl\'s RESTCONF is listening (default is 8181)')
61     parser.add_argument('--cycles', type=int, default=1,
62                         help='Number of flow add/delete cycles; default 1. Both Flow Adds and Flow Deletes are '
63                              'performed in cycles. <THREADS> worker threads are started in each cycle and the cycle '
64                              'ends when all threads finish. Another cycle is started when the previous cycle finished.')
65     parser.add_argument('--threads', type=int, default=1,
66                         help='Number of request worker threads to start in each cycle; default=1. '
67                              'Each thread will add/delete <FLOWS> flows.')
68     parser.add_argument('--flows', type=int, default=10,
69                         help='Number of flows that will be added/deleted by each worker thread in each cycle; '
70                              'default 10')
71     parser.add_argument('--fpr', type=int, default=1,
72                         help='Flows-per-Request - number of flows (batch size) sent in each HTTP request; '
73                              'default 1')
74     parser.add_argument('--delay', type=int, default=2,
75                         help='Time (seconds) to between inventory polls when waiting for stats to catch up; default=1')
76     parser.add_argument('--timeout', type=int, default=100,
77                         help='The maximum time (seconds) to wait between the add and delete cycles; default=100')
78     parser.add_argument('--delete', dest='delete', action='store_true', default=True,
79                         help='Delete all added flows one by one, benchmark delete '
80                              'performance.')
81     parser.add_argument('--bulk-delete', dest='bulk_delete', action='store_true', default=False,
82                         help='Delete all flows in bulk; default=False')
83     parser.add_argument('--auth', dest='auth', action='store_true',
84                         help="Use authenticated access to REST (username: 'admin', password: 'admin'); default=False")
85     parser.add_argument('--startflow', type=int, default=0,
86                         help='The starting Flow ID; default=0')
87     parser.add_argument('--file', default='',
88                         help='File from which to read the JSON flow template; default: no file, use a built in '
89                              'template.')
90     parser.add_argument('--config_monitor', type=int, default=60,
91                         help='Time to monotir inventory after flows are configured in seconds; default=60')
92     parser.add_argument('--deconfig_monitor', type=int, default=60,
93                         help='Time to monitor inventory after flows are de configured in seconds; default=60')
94     parser.add_argument('--monitor_period', type=int, default=10,
95                         help='Monitor period of triggering inventory crawler in seconds; default=10')
96     parser.add_argument('--monitor_outfile', default=None, help='Output file(if specified)')
97
98     in_args = parser.parse_args()
99
100     # Initialize
101     if in_args.file != '':
102         flow_template = get_json_from_file(in_args.file)
103     else:
104         flow_template = None
105
106     ic = InventoryCrawler(in_args.host, in_args.port, 0, 'operational', in_args.auth, False)
107
108     fct = FlowConfigBlaster(in_args.host, in_args.port, in_args.cycles, in_args.threads, in_args.fpr,
109                             16, in_args.flows, in_args.startflow, in_args.auth)
110     # Get the baseline stats. Required in Step 3 to validate if the delete
111     # function gets the controller back to the baseline
112     ic.crawl_inventory()
113     reported = ic.reported_flows
114     found = ic.found_flows
115
116     print 'Baseline:'
117     print '   Reported nodes: %d' % reported
118     print '   Found nodes:    %d' % found
119
120     stats = []
121     stats.append((time.time(), ic.nodes, ic.reported_flows, ic.found_flows))
122     # Run through <CYCLES> add cycles, where <THREADS> threads are started in
123     # each cycle and <FLOWS> flows are added from each thread
124     fct.add_blaster()
125
126     print '\n*** Total flows added: %d' % fct.get_ok_flows()
127     print '    HTTP[OK] results:  %d\n' % fct.get_ok_rqsts()
128
129     # monitor stats and save results in the list
130     for stat_item in monitor_stats(ic, in_args.config_monitor, in_args.monitor_period):
131         print stat_item
132         stats.append(stat_item)
133
134     # Run through <CYCLES> delete cycles, where <THREADS> threads  are started
135     # in each cycle and <FLOWS> flows previously added in an add cycle are
136     # deleted in each thread
137     if in_args.bulk_delete:
138         print '\nDeleting all flows in bulk:'
139         sts = cleanup_config_odl(in_args.host, in_args.port, in_args.auth)
140         if sts != 200:
141             print '   Failed to delete flows, code %d' % sts
142         else:
143             print '   All flows deleted.'
144     else:
145         print '\nDeleting flows one by one\n   ',
146         fct.delete_blaster()
147         print '\n*** Total flows deleted: %d' % fct.get_ok_flows()
148         print '    HTTP[OK] results:    %d\n' % fct.get_ok_rqsts()
149
150     # monitor stats and append to the list
151     for stat_item in monitor_stats(ic, in_args.deconfig_monitor, in_args.monitor_period):
152         print stat_item
153         stats.append(stat_item)
154
155     # if requested, write collected data into the file
156     if in_args.monitor_outfile is not None:
157         with open(in_args.monitor_outfile, 'wt') as fd:
158             for e in stats:
159                 fd.write('{0} {1} {2} {3}\n'.format(e[0], e[1], e[2], e[3]))