def get_info(self): """ Get CPU info :return: """ cmd = Command("lscpu") try: nums = cmd.get_str(r'^CPU\S*:\s+(?P<cpus>\d+)$', 'cpus', False) except Exception as concrete_error: print(concrete_error) return False self.nums = int(nums) self.list = range(self.nums) cmd = Command( "cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq") try: max_freq = cmd.get_str() except Exception as concrete_error: print(concrete_error) return False self.max_freq = int(max_freq) cmd = Command( "cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq") try: min_freq = cmd.get_str() except Exception as concrete_error: print(concrete_error) return False self.min_freq = int(min_freq) return True
def test_tcp_bandwidth(self): """ Test tcp bandwidth :return: """ cmd = "qperf %s tcp_bw" % self.server_ip print(cmd) com = Command(cmd) pattern = r"\s+bw\s+=\s+(?P<bandwidth>[\.0-9]+ [MG]B/sec)" for _ in range(self.retries): try: bandwidth = com.get_str(pattern, 'bandwidth', False) band_width = bandwidth.split() if 'GB' in band_width[1]: bandwidth = float(band_width[0]) * 8 * 1024 else: bandwidth = float(band_width[0]) * 8 target_bandwidth = self.target_bandwidth_percent * self.speed print("Current bandwidth is %.2fMb/s, target is %.2fMb/s" % (bandwidth, target_bandwidth)) if bandwidth > target_bandwidth: return True except Exception as concrete_error: print(concrete_error) return False
def test_bw(self, cmd): """ Test bandwidth :param cmd: :return: """ if self.link_layer == 'Ethernet': cmd = cmd + ' -R' if not self.call_remote_server(cmd, 'start', self.server_ip): print("start %s server failed." % cmd) return False cmd = "%s %s -d %s -i %s" % (cmd, self.server_ip, self.ib_device, self.ib_port) print(cmd) com = Command(cmd) pattern = r"\s+(\d+)\s+(\d+)\s+([\.\d]+)\s+(?P<avg_bw>[\.\d]+)\s+([\.\d]+)" try: avg_bw = com.get_str(pattern, 'avg_bw', False) # MB/sec avg_bw = float(avg_bw) * 8 tgt_bw = self.target_bandwidth_percent * self.speed print("Current bandwidth is %.2fMb/s, target is %.2fMb/s" % (avg_bw, tgt_bw)) return avg_bw > tgt_bw except Exception as concrete_error: print(concrete_error) self.call_remote_server(cmd, 'stop') return False
def write_test(self): """ Write mode test of CDROM :return: """ try: devname = self.device.get_property("DEVNAME") Command("umount %s" % devname).run(ignore_errors=True) if "BD" in self.type or "DVD_PLUS" in self.type: Command("growisofs -Z %s -quiet -R %s" % (devname, self.test_dir)).echo() self.reload_disc(devname) sys.stdout.flush() return True else: write_opts = "-sao" try: command = Command("cdrecord dev=%s -checkdrive" % devname) modes = command.get_str(regex="^Supported modes[^:]*:(?P<modes>.*$)", \ regex_group="modes", single_line=False, ignore_errors=True) if "TAO" in modes: write_opts = "-tao" if "SAO" in modes: write_opts = "-sao" flags = command.get_str(regex="^Driver flags[^:]*:(?P<flags>.*$)", \ regex_group="flags", single_line=False, ignore_errors=True) if "BURNFREE" in flags: write_opts += " driveropts=burnfree" except CertCommandError as concrete_error: print(concrete_error) size = Command("mkisofs -quiet -R -print-size %s " % self.test_dir).get_str() blocks = int(size) Command( "mkisofs -quiet -R %s | cdrecord -v %s dev=%s fs=32M tsize=%ss -" % (self.test_dir, write_opts, devname, blocks)).echo() self.reload_disc(devname) sys.stdout.flush() return True except CertCommandError as concrete_error: return False
def get_interface_ip(self): """ Get interface ip :return: """ com = Command("ip addr show %s" % self.interface) pattern = r".*inet.? (?P<ip>.+)/.*" try: ip_addr = com.get_str(pattern, 'ip', False) return ip_addr except Exception: print("[X] No available ip on the interface.") return None
def get_speed(self): """ Get speed on the interface :return: """ com = Command("ethtool %s" % self.interface) pattern = r".*Speed:\s+(?P<speed>\d+)Mb/s" try: speed = com.get_str(pattern, 'speed', False) return int(speed) except Exception: print("[X] No speed found on the interface.") return None
def get_freq(self, cpu): """ Get CPU frequency :param cpu: :return: """ cmd = Command("cpupower -c %s frequency-info -w" % cpu) try: return int( cmd.get_str(r'.* frequency: (?P<freq>\d+) .*', 'freq', False)) except Exception as concrete_error: print(concrete_error) return False
def get_governor(self, cpu): """ Get cpu governor :param cpu: :return: """ cmd = Command("cpupower -c %s frequency-info -p" % cpu) try: return cmd.get_str(r'.* governor "(?P<governor>\w+)".*', 'governor', False) except Exception as concrete_error: print(concrete_error) return False
def test_icmp(self): """ Test ICMP :return: """ count = 500 com = Command("ping -q -c %d -i 0 %s" % (count, self.server_ip)) pattern = r".*, (?P<loss>\d+\.{0,1}\d*)% packet loss.*" for _ in range(self.retries): try: print(com.command) loss = com.get_str(pattern, 'loss', False) com.print_output() if float(loss) == 0: return True except Exception as concrete_error: print(concrete_error) return False