c55c67f15220bee47c4cf5e067cab662dbbe6b02
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / rapid_corestatstest.py
1 #!/usr/bin/python
2
3 ##
4 ## Copyright (c) 2010-2020 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 ##
11 ##     http://www.apache.org/licenses/LICENSE-2.0
12 ##
13 ## Unless required by applicable law or agreed to in writing, software
14 ## distributed under the License is distributed on an "AS IS" BASIS,
15 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ## See the License for the specific language governing permissions and
17 ## limitations under the License.
18 ##
19
20 import sys
21 import time
22 from rapid_log import RapidLog
23 from rapid_test import RapidTest
24
25 class CoreStatsTest(RapidTest):
26     """
27     Class to manage the corestatstesting
28     """
29     def __init__(self, runtime, pushgateway, environment_file, machines):
30         self.runtime = runtime
31         self.pushgateway = pushgateway
32         self.environment_file = environment_file
33         self.machines = machines 
34
35     def run(self):
36     #    fieldnames = ['PROXID','Time','Received','Sent','NonDPReceived','NonDPSent','Delta','NonDPDelta','Dropped']
37     #    writer = csv.DictWriter(data_csv_file, fieldnames=fieldnames)
38     #    writer.writeheader()
39         RapidLog.info("+------------------------------------------------------------------------------------------------------------------+")
40         RapidLog.info("| Measuring core statistics on 1 or more PROX instances                                                            |")
41         RapidLog.info("+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+")
42         RapidLog.info("| PROX ID   |    Time   |    RX      |     TX     | non DP RX  | non DP TX  |   TX - RX  | nonDP TX-RX|  DROP TOT  |")
43         RapidLog.info("+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+")
44         duration = self.runtime
45         tot_drop = []
46         old_rx = []; old_non_dp_rx = []; old_tx = []; old_non_dp_tx = []; old_drop = []; old_tx_fail = []; old_tsc = []
47         new_rx = []; new_non_dp_rx = []; new_tx = []; new_non_dp_tx = []; new_drop = []; new_tx_fail = []; new_tsc = []
48         machines_to_go = len (self.machines)
49         for machine in self.machines:
50             machine.reset_stats()
51             tot_drop.append(0)
52             old_rx.append(0); old_non_dp_rx.append(0); old_tx.append(0); old_non_dp_tx.append(0); old_drop.append(0); old_tx_fail.append(0); old_tsc.append(0)
53             old_rx[-1], old_non_dp_rx[-1], old_tx[-1], old_non_dp_tx[-1], old_drop[-1], old_tx_fail[-1], old_tsc[-1], tsc_hz = machine.core_stats()
54             new_rx.append(0); new_non_dp_rx.append(0); new_tx.append(0); new_non_dp_tx.append(0); new_drop.append(0); new_tx_fail.append(0); new_tsc.append(0)
55         while (duration > 0):
56             time.sleep(0.5)
57             # Get statistics after some execution time
58             for i, machine in enumerate(self.machines, start=0):
59                 new_rx[i], new_non_dp_rx[i], new_tx[i], new_non_dp_tx[i], new_drop[i], new_tx_fail[i], new_tsc[i], tsc_hz = machine.core_stats()
60                 drop = new_drop[i]-old_drop[i]
61                 rx = new_rx[i] - old_rx[i]
62                 tx = new_tx[i] - old_tx[i]
63                 non_dp_rx = new_non_dp_rx[i] - old_non_dp_rx[i]
64                 non_dp_tx = new_non_dp_tx[i] - old_non_dp_tx[i]
65                 tsc = new_tsc[i] - old_tsc[i]
66                 if tsc == 0 :
67                     continue
68                 machines_to_go -= 1
69                 old_drop[i] = new_drop[i]
70                 old_rx[i] = new_rx[i]
71                 old_tx[i] = new_tx[i]
72                 old_non_dp_rx[i] = new_non_dp_rx[i]
73                 old_non_dp_tx[i] = new_non_dp_tx[i]
74                 old_tsc[i] = new_tsc[i]
75                 tot_drop[i] = tot_drop[i] + tx - rx
76                 RapidLog.info('|{:>10.0f}'.format(i)+ ' |{:>10.0f}'.format(duration)+' | ' + '{:>10.0f}'.format(rx) + ' | ' +'{:>10.0f}'.format(tx) + ' | '+'{:>10.0f}'.format(non_dp_rx)+' | '+'{:>10.0f}'.format(non_dp_tx)+' | ' + '{:>10.0f}'.format(tx-rx) + ' | '+ '{:>10.0f}'.format(non_dp_tx-non_dp_rx) + ' | '+'{:>10.0f}'.format(tot_drop[i]) +' |')
77     #            writer.writerow({'PROXID':i,'Time':duration,'Received':rx,'Sent':tx,'NonDPReceived':non_dp_rx,'NonDPSent':non_dp_tx,'Delta':tx-rx,'NonDPDelta':non_dp_tx-non_dp_rx,'Dropped':tot_drop[i]})
78                 if self.pushgateway:
79                     URL = self.pushgateway+ '/metrics/job/' + TestName + '/instance/' + self.environment_file + str(i)
80                     DATA = 'PROXID {}\nTime {}\n Received {}\nSent {}\nNonDPReceived {}\nNonDPSent {}\nDelta {}\nNonDPDelta {}\nDropped {}\n'.format(i,duration,rx,tx,non_dp_rx,non_dp_tx,tx-rx,non_dp_tx-non_dp_rx,tot_drop[i])
81                     HEADERS = {'X-Requested-With': 'Python requests', 'Content-type': 'text/xml'}
82                     response = requests.post(url=URL, data=DATA,headers=HEADERS)
83                     if (response.status_code != 202) and (response.status_code != 200):
84                         RapidLog.info('Cannot send metrics to {}'.format(URL))
85                         RapidLog.info(DATA)
86                 if machines_to_go == 0:
87                     duration = duration - 1
88                     machines_to_go = len (self.machines)
89         RapidLog.info("+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+")
90         return (True)
91