Added example for performing a performance analysis of VNFFGs
[osm/vim-emu.git] / examples / performance_measurements / vnffg.py
1 #!/usr/bin/env python2
2 # Copyright (c) 2019 Erik Schilling
3 # ALL RIGHTS RESERVED.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import csv
18 import json
19 import os
20
21 import time
22 from jinja2 import Environment, FileSystemLoader
23
24 from emuvim.api.osm.pre_configured_osm import PreConfiguredOSM
25 from emuvim.api.util.docker_utils import build_dockerfile_dir
26
27
28 # sys.path.append('/vim-emu/pycharm-debug.egg')
29 # import pydevd
30 # pydevd.settrace('10.0.2.2', port=21001, stdoutToServer=True, stderrToServer=True, suspend=False)
31
32 prefix = os.path.dirname(os.path.abspath(__file__))
33
34 build_dockerfile_dir('../images/trafficdebug/', 'trafficdebug')
35
36 env = Environment(
37 loader=FileSystemLoader(os.path.join(prefix, '../templates/'))
38 )
39 template = env.get_template('vnffg-nsd.yaml')
40
41 IPERF_TIME = 30
42
43
44 def run_iperf(node, target_ip, target_port):
45 vnffg_iperf_out = node.cmd('iperf3 -J -t %d -c %s -p %d' % (IPERF_TIME, target_ip, target_port))
46 print(vnffg_iperf_out)
47 vnffg_iperf = json.loads(vnffg_iperf_out)
48 vnffg_rtt = vnffg_iperf['end']['streams'][0]['sender']['mean_rtt']
49 vnffg_bits_per_second = vnffg_iperf['end']['sum_received']['bits_per_second']
50 return vnffg_rtt, vnffg_bits_per_second
51
52
53 def main():
54 with open('vnffg-%d.csv' % time.time(), 'w') as csvfile:
55 fieldnames = ['n', 'deploy', 'vnffg_iperf_rtt', 'vnffg_iperf_bps',
56 'normal_iperf_rtt', 'normal_iperf_bps', 'delete']
57 writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
58
59 writer.writeheader()
60
61 # vim-emu increases the subnet ip part by 1 for each run
62 subnet = 0
63
64 for n in range(1, 15 + 1):
65 # render NSD template for current iteration
66 with open(os.path.join(prefix, '../autogenerated/vnffg-nsd/vnffg-nsd.yml'), 'w') as f:
67 f.write(template.render(n=n, subnet=subnet))
68
69 with PreConfiguredOSM(osm_version='master') as osm:
70 osm.onboard_vnfd('../vnfs/vnffg')
71 nsd_id = osm.onboard_nsd('../autogenerated/vnffg-nsd')
72 ns_create = time.time()
73 osm.ns_create('vnffg-%d' % n, nsd_id)
74
75 osm.ns_wait_until_all_in_status('running')
76
77 ns_deploy_done = time.time()
78
79 # ensure that port chains are created, they are not reported by the API.
80 time.sleep(60)
81
82 first_name = 'dc1_vnffg-%d-1--1' % n
83 last_name = 'dc1_vnffg-%d-%d--1' % (n, n + 1)
84 first_node = osm.api.compute.find_server_by_name_or_id(first_name).emulator_compute
85 last_node = osm.api.compute.find_server_by_name_or_id(last_name).emulator_compute
86
87 last_node.cmd('iperf3 -s -p 80 > /dev/null &')
88 last_node.cmd('iperf3 -s -p 81 > /dev/null &')
89
90 # give iperf3 servers time to start up
91 time.sleep(1)
92
93 target_ip = '10.0.%d.%d' % (subnet, n + 2)
94 vnffg_rtt, vnffg_bits_per_second = run_iperf(first_node, target_ip, 80)
95 normal_rtt, normal_bits_per_second = run_iperf(first_node, target_ip, 81)
96
97 ns_delete = time.time()
98 for ns in osm.ns_list():
99 osm.ns_delete(ns['id'])
100
101 osm.ns_wait_until_all_in_status('terminated')
102
103 ns_delete_done = time.time()
104 measurement = {
105 'n': n,
106 'deploy': ns_deploy_done - ns_create,
107 'vnffg_iperf_rtt': vnffg_rtt,
108 'vnffg_iperf_bps': vnffg_bits_per_second,
109 'normal_iperf_rtt': normal_rtt,
110 'normal_iperf_bps': normal_bits_per_second,
111 'delete': ns_delete_done - ns_delete
112 }
113 writer.writerow(measurement)
114 csvfile.flush()
115
116 subnet += 1
117
118
119 if __name__ == '__main__':
120 main()