Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Chetan Gaonker | f68a750 | 2017-06-29 00:58:04 +0000 | [diff] [blame] | 17 | import os |
| 18 | import subprocess |
| 19 | import requests |
| 20 | import json |
| 21 | import time |
| 22 | from CordTestUtils import log_test as log, getstatusoutput, get_controller |
| 23 | from OnosCtrl import OnosCtrl |
| 24 | |
| 25 | class perf(object): |
| 26 | def __init__(self, controller, interface = 'eth0'): |
| 27 | self.controller = controller |
| 28 | self.interface = interface |
| 29 | |
| 30 | def know_cpu_freq(self): |
| 31 | freq = open('/proc/cpuinfo/','r') |
| 32 | freqs = freq.read() |
| 33 | freq.seek(0) |
| 34 | cpuentry = freq.readline() |
| 35 | cpusplit = cpuentry.split() |
| 36 | |
| 37 | while cpusplit[0] != "cpu": |
| 38 | while cpusplit[0] != "MHz": |
| 39 | cpuline = freq.readline() |
| 40 | cpusplit = cpuline.split() |
| 41 | freq.close() |
| 42 | cpu_mhz = cpusplit[3] |
| 43 | return cpu_mhz |
| 44 | |
| 45 | def retrieve_cpu_stats(self): |
| 46 | cpu = open('/proc/stat/','r').readlines()[0] |
| 47 | return map(float, cpu.split()[1:5]) |
| 48 | |
| 49 | def validate_cpu_performance(self, interval): |
| 50 | time_stamp1 = retrieve_cpu_stats() |
| 51 | time.sleep(interval) |
| 52 | time_stamp2 = retrieve_cpu_stats() |
| 53 | diff = [time_stamp2[i] - time_stamp1[i] for i in range(len(time_stamp1))] |
| 54 | try: |
| 55 | return 1.0 - (diff[-1:].pop()/(sum(diff)*1.0)) |
| 56 | except: |
| 57 | return 0.0 |
| 58 | |
| 59 | def memory_usage(self): |
| 60 | cmd_run = subprocess.check_output(['free','-b']) |
| 61 | memory = cmd_run.split() |
| 62 | total = int(memory[7]) |
| 63 | used = int(memory[8]) |
| 64 | free = int(memory[9]) |
| 65 | return total, used, free |
| 66 | |
| 67 | def rx_network_stats(self, intf): |
| 68 | for entry in open('/proc/net/dev', 'r'): |
| 69 | if intf in entry: |
| 70 | stat = entry.split('%s:' % intf)[1].split() |
| 71 | rx_bytes = stat[0] |
| 72 | rx_packets = stat[1] |
| 73 | rx_errors = stat[2] |
| 74 | rx_drops = stat[3] |
| 75 | return int(rx_bytes), int(rx_packets), int(rx_errors), int(rx_drops) |
| 76 | |
| 77 | def tx_network_stats(self, intf): |
| 78 | for entry in open('/proc/net/dev', 'r'): |
| 79 | if intf in entry: |
| 80 | stat = entry.split('%s:' % intf)[1].split() |
| 81 | tx_bytes = stat[8] |
| 82 | tx_packets = stat[9] |
| 83 | tx_errors = stat[10] |
| 84 | tx_drops = stat[11] |
| 85 | return int(tx_bytes), int(tx_packets), int(tx_errors), int(tx_drops) |
| 86 | |
| 87 | def check_node_uptime(self): |
| 88 | return float(open('/proc/uptime','r').read().split(' ')[0]) |
| 89 | |