Merge "VNF_Catalogue Codebase"
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / dpi / dpi1.py
1 #!/bin/env python
2
3 ##
4 ## Copyright (c) 2010-2017 Intel Corporation
5 ##
6 ## Licensed under the Apache License, Version 2.0 (the "License");
7 ## you may not use this file except in compliance with the License.
8 ## You may obtain a copy of the License at
9 ##
10 ##     http://www.apache.org/licenses/LICENSE-2.0
11 ##
12 ## Unless required by applicable law or agreed to in writing, software
13 ## distributed under the License is distributed on an "AS IS" BASIS,
14 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ## See the License for the specific language governing permissions and
16 ## limitations under the License.
17 ##
18
19 from testerset import *
20 from time import sleep
21 from time import time
22 from decimal import *
23 import copy
24 from os import system
25 import socket
26 from itertools import chain
27 from math import *
28 from csvwriter import *
29 from config import *
30 from progress import *
31 from proxmaxssprobe import *
32
33 def runTest(minSetupRate, testParam):
34     print "Running test with following parameters:"
35     print testParam.toString();
36
37     testers = testerSet(config._test_systems, config._max_port_rate, testParam);
38
39     thresh = testParam.getConnections();
40     p = Progress(thresh, ["connections", "setup rate", "reTX"], False);
41     loop_count = 0;
42     converged = False;
43
44     testers.startForkJoin();
45     testers.wait_links_up();
46     testers.start_cores();
47
48     print "Running until convergence (%s connections)" % str(thresh)
49     while (not converged):
50         sleep(config._interCheckDuration)
51         testers.update_stats();
52         tot = testers.get_total_connections();
53         tot_retx = testers.get_total_retx();
54         rates = testers.get_rates();
55         curSetupRate = testers.get_setup_rate();
56         ierrors = testers.getIerrors();
57
58         converged = tot >= thresh;
59         if (not converged):
60             if (loop_count > 0 and curSetupRate < minSetupRate):
61                 reason = str(curSetupRate) + " < " + str(minSetupRate);
62                 print "Current setup rate is lower than min setup rate: " +  reason
63                 testers.killProx();
64                 return False, [];
65             if (not testers.conditionsGood()):
66                 print "conditions are bad: " + testers.getReason();
67                 testers.killProx();
68                 return False, [];
69
70         if (config._debug):
71             p.setProgress(tot, [tot, curSetupRate, tot_retx]);
72             print p.toString();
73         loop_count += 1;
74     print "converged"
75
76     skipTime = config._skipTime
77     print "Connection threshold reached, waiting for " + str(skipTime) + "s, conditions checked = " + str(config._checkConditions)
78     while (skipTime > 0):
79         skipTime -= config._interCheckDuration
80         sleep(config._interCheckDuration)
81         testers.update_stats();
82         if (config._checkConditions and not testers.conditionsGood()):
83             print "conditions are bad: " + testers.getReason();
84             testers.killProx();
85             return False, [];
86
87     testers.tx_rate_meassurement();
88
89     testLength = config._testLength
90     print "Waiting final " + str(testLength) + "s"
91     while (testLength > 0):
92         testLength -= config._interCheckDuration
93         sleep(config._interCheckDuration)
94         testers.update_stats();
95         if (not testers.conditionsGood()):
96             print "conditions are bad: " + testers.getReason();
97             testers.killProx();
98             return False, [];
99
100     rates = testers.tx_rate_meassurement();
101
102     testers.killProx();
103     return True, rates;
104
105 def find_ss(tot_conn, maxSetupRate, ss_max):
106     iterationCount = 0;
107     valid_ss = []
108     speed_ss = [];
109
110     # The setup rate must be in [0.2% of total connections, maxSetupRate]
111     # Also, it must not be hihger than 50% of the total connections
112     min_setup_rate = tot_conn / 500;
113
114     if (min_setup_rate > maxSetupRate):
115         print "min setup rate > max setup rate: " + str(min_setup_rate) + " > " + str(maxSetupRate);
116         return valid_ss, speed_ss;
117     if (maxSetupRate > tot_conn / 2):
118         print "maximum setup rate (" + str(maxSetupRate) + ") is more than 50% of " + str(tot_conn)
119         return valid_ss, speed_ss;
120
121     accuracy = 10**config._accuracy
122     ss_lo = 1
123     ss_hi = int(round(ss_max * accuracy,0))
124
125     iterationOverride = [ss_hi, ss_lo];
126     # Binary search for highest speed scaling
127     while (ss_lo <= ss_hi):
128         if (iterationCount < len(iterationOverride)):
129             ss = iterationOverride[iterationCount]
130         else:
131             ss = (ss_lo + ss_hi)/2;
132
133         testParam = TestParameters(maxSetupRate, tot_conn, float(ss)/accuracy);
134
135         success, rates = runTest(min_setup_rate, testParam);
136         print "success = " + str(success) + ", rates = " + str(rates)
137         if (success == True):
138             valid_ss.append(float(ss)/accuracy);
139             speed_ss.append(sum(rates)/len(rates))
140             ss_lo = ss + 1
141         else:
142             ss_hi = ss - 1;
143         iterationCount += 1
144     return valid_ss, speed_ss;
145
146 def get_highest_ss_and_speed(valid_ss, speed_ss):
147     highest_ss = None;
148     highest_speed = None;
149
150     for i in range(len(valid_ss)):
151         if(highest_ss == None or highest_ss < valid_ss[i]):
152             highest_ss = valid_ss[i];
153             highest_speed = speed_ss[i];
154     return highest_ss, highest_speed;
155
156 def get_max_ss():
157     ts = config._test_systems[0];
158     test_system = ProxMaxSSProbe(ts);
159     max_ss = test_system.getMaxSS();
160
161     return floor((max_ss * (10**config._accuracy)))/(10**config._accuracy)
162
163 config = Config();
164 config.parse(sys.argv[0], sys.argv[1:])
165
166 err = config.getErrorTestOne();
167 if (err is not None):
168     print "Invalid configuration: " + err;
169     exit(-1);
170 else:
171     print config.toString()
172
173 if (config._once is not None):
174     maxSetupRate = int(config._once[0])
175     minSetupRate = maxSetupRate/500
176     connections = int(config._once[1])
177     speedScaling = float(config._once[2])
178
179     testParam = TestParameters(maxSetupRate, connections, speedScaling)
180     success, rates = runTest(minSetupRate, testParam)
181     print "success = " + str(success) + ", port rates = " + str(rates)
182     exit(0);
183
184 msr_list = []
185 msr_list += range(4000, 20000, 2000)
186 msr_list += range(20000, 100000, 20000)
187 msr_list += range(100000, 300000, 50000)
188 msr_list += range(300000, 800001, 100000);
189
190 conn_list = [1*10**5, 2*10**5, 4*10**5, 8*10**5, 1*10**6, 2*10**6]
191
192 summary_file = CsvWriter()
193 summary_file.open(config._output_file_name)
194
195 tot_it = 0;
196 for tot_conn in conn_list:
197     for msr in msr_list:
198         if (msr >= tot_conn/2):
199             break;
200         tot_it += 1
201
202 cnt = -1;
203 print "Search will include " + str(tot_it) + " parameter combinations"
204 print "Will search for highest link utilization"
205
206 # If the lowest msr was a for n connections, then the lowest msr
207 # for n + 1 connections can't be lower than a.
208 low_sr = msr_list[0];
209
210 max_ss = get_max_ss()
211
212 high_ss = Decimal(max_ss)
213
214 globalProgress = Progress(tot_it)
215 globalProgress.setProgress(0);
216 for tot_conn in conn_list:
217     had_success = False;
218     all_ss = []
219     for msr in msr_list:
220         globalProgress.incrProgress();
221
222         if (msr < low_sr):
223             print "skipping " + str(msr) + " since it is lower than " + str(low_sr)
224             continue;
225
226         print globalProgress.toString();
227
228         valid_ss, speed_ss = find_ss(tot_conn, msr, high_ss)
229         print "valid ss = " + str(valid_ss)
230         print "valid speeds = " + str(speed_ss)
231
232         if (len(valid_ss) > 0):
233             highest_ss, highest_speed = get_highest_ss_and_speed(valid_ss, speed_ss);
234             summary_file.write([msr, tot_conn, highest_ss, highest_speed]);
235
236             if (not had_success):
237                 low_sr = msr;
238
239             had_success = True;
240         all_ss = all_ss + valid_ss;
241
242     if (len(all_ss) > 0):
243         high_ss = max(all_ss);