Hanna Arhipova | 55cc129 | 2019-01-08 14:22:18 +0200 | [diff] [blame] | 1 | import texttable as tt |
| 2 | |
| 3 | class helpers(object): |
| 4 | def __init__ (self, local_salt_client): |
| 5 | self.local_salt_client = local_salt_client |
| 6 | |
| 7 | def start_iperf_between_hosts(self, global_results, node_i, node_j, ip_i, ip_j, net_name): |
| 8 | result = [] |
| 9 | direct_raw_results = self.start_iperf_client(node_i, ip_j) |
| 10 | result.append(direct_raw_results) |
| 11 | print "1 forward" |
| 12 | forward = "1 thread:\n" |
| 13 | forward += direct_raw_results + " Gbits/sec" |
| 14 | |
| 15 | direct_raw_results = self.start_iperf_client(node_i, ip_j, 10) |
| 16 | result.append(direct_raw_results) |
| 17 | print "10 forward" |
| 18 | forward += "\n\n10 thread:\n" |
| 19 | forward += direct_raw_results + " Gbits/sec" |
| 20 | |
| 21 | reverse_raw_results = self.start_iperf_client(node_j, ip_i) |
| 22 | result.append(reverse_raw_results) |
| 23 | print "1 backward" |
| 24 | backward = "1 thread:\n" |
| 25 | backward += reverse_raw_results + " Gbits/sec" |
| 26 | |
| 27 | reverse_raw_results = self.start_iperf_client(node_j, ip_i, 10) |
| 28 | result.append(reverse_raw_results) |
| 29 | print "10 backward" |
| 30 | backward += "\n\n10 thread:\n" |
| 31 | backward += reverse_raw_results + " Gbits/sec" |
| 32 | global_results.append([node_i, node_j, |
| 33 | net_name, forward, backward]) |
| 34 | |
| 35 | self.kill_iperf_processes(node_i) |
| 36 | self.kill_iperf_processes(node_j) |
| 37 | return result |
| 38 | |
| 39 | def draw_table_with_results(self, global_results): |
| 40 | tab = tt.Texttable() |
| 41 | header = [ |
| 42 | 'node name 1', |
| 43 | 'node name 2', |
| 44 | 'network', |
| 45 | 'bandwidth >', |
| 46 | 'bandwidth <', |
| 47 | ] |
| 48 | tab.set_cols_align(['l', 'l', 'l', 'l', 'l']) |
| 49 | tab.set_cols_width([27, 27, 15, 20, '20']) |
| 50 | tab.header(header) |
| 51 | for row in global_results: |
| 52 | tab.add_row(row) |
| 53 | s = tab.draw() |
| 54 | print s |
| 55 | |
| 56 | def start_iperf_client(self, minion_name, target_ip, thread_count=None): |
| 57 | iperf_command = 'timeout --kill-after=20 19 iperf -c {0}'.format(target_ip) |
| 58 | if thread_count: |
| 59 | iperf_command += ' -P {0}'.format(thread_count) |
| 60 | output = self.local_salt_client.cmd(tgt=minion_name, |
| 61 | fun='cmd.run', |
| 62 | param=[iperf_command]) |
| 63 | # self.kill_iperf_processes(minion_name) |
| 64 | try: |
| 65 | result = output.values()[0].split('\n')[-1].split(' ')[-2:] |
| 66 | if result[1] == 'Mbits/sec': |
| 67 | return str(float(result[0])*0.001) |
| 68 | if result[1] != 'Gbits/sec': |
| 69 | return "0" |
| 70 | return result[0] |
| 71 | except: |
| 72 | print "No iperf result between {} and {} (maybe they don't have connectivity)".format(minion_name, target_ip) |
| 73 | |
| 74 | |
| 75 | def kill_iperf_processes(self, minion_name): |
| 76 | kill_command = "for pid in $(pgrep iperf); do kill $pid; done" |
| 77 | output = self.local_salt_client.cmd(tgt=minion_name, |
| 78 | fun='cmd.run', |
| 79 | param=[kill_command]) |