Chetan Gaonker | f68a750 | 2017-06-29 00:58:04 +0000 | [diff] [blame] | 1 | import os |
| 2 | import subprocess |
| 3 | import requests |
| 4 | import json |
| 5 | import time |
| 6 | from CordTestUtils import log_test as log, getstatusoutput, get_controller |
| 7 | from OnosCtrl import OnosCtrl |
| 8 | |
| 9 | class perf(object): |
| 10 | def __init__(self, controller, interface = 'eth0'): |
| 11 | self.controller = controller |
| 12 | self.interface = interface |
| 13 | |
| 14 | def know_cpu_freq(self): |
| 15 | freq = open('/proc/cpuinfo/','r') |
| 16 | freqs = freq.read() |
| 17 | freq.seek(0) |
| 18 | cpuentry = freq.readline() |
| 19 | cpusplit = cpuentry.split() |
| 20 | |
| 21 | while cpusplit[0] != "cpu": |
| 22 | while cpusplit[0] != "MHz": |
| 23 | cpuline = freq.readline() |
| 24 | cpusplit = cpuline.split() |
| 25 | freq.close() |
| 26 | cpu_mhz = cpusplit[3] |
| 27 | return cpu_mhz |
| 28 | |
| 29 | def retrieve_cpu_stats(self): |
| 30 | cpu = open('/proc/stat/','r').readlines()[0] |
| 31 | return map(float, cpu.split()[1:5]) |
| 32 | |
| 33 | def validate_cpu_performance(self, interval): |
| 34 | time_stamp1 = retrieve_cpu_stats() |
| 35 | time.sleep(interval) |
| 36 | time_stamp2 = retrieve_cpu_stats() |
| 37 | diff = [time_stamp2[i] - time_stamp1[i] for i in range(len(time_stamp1))] |
| 38 | try: |
| 39 | return 1.0 - (diff[-1:].pop()/(sum(diff)*1.0)) |
| 40 | except: |
| 41 | return 0.0 |
| 42 | |
| 43 | def memory_usage(self): |
| 44 | cmd_run = subprocess.check_output(['free','-b']) |
| 45 | memory = cmd_run.split() |
| 46 | total = int(memory[7]) |
| 47 | used = int(memory[8]) |
| 48 | free = int(memory[9]) |
| 49 | return total, used, free |
| 50 | |
| 51 | def rx_network_stats(self, intf): |
| 52 | for entry in open('/proc/net/dev', 'r'): |
| 53 | if intf in entry: |
| 54 | stat = entry.split('%s:' % intf)[1].split() |
| 55 | rx_bytes = stat[0] |
| 56 | rx_packets = stat[1] |
| 57 | rx_errors = stat[2] |
| 58 | rx_drops = stat[3] |
| 59 | return int(rx_bytes), int(rx_packets), int(rx_errors), int(rx_drops) |
| 60 | |
| 61 | def tx_network_stats(self, intf): |
| 62 | for entry in open('/proc/net/dev', 'r'): |
| 63 | if intf in entry: |
| 64 | stat = entry.split('%s:' % intf)[1].split() |
| 65 | tx_bytes = stat[8] |
| 66 | tx_packets = stat[9] |
| 67 | tx_errors = stat[10] |
| 68 | tx_drops = stat[11] |
| 69 | return int(tx_bytes), int(tx_packets), int(tx_errors), int(tx_drops) |
| 70 | |
| 71 | def check_node_uptime(self): |
| 72 | return float(open('/proc/uptime','r').read().split(' ')[0]) |
| 73 | |