Skip to content

Instantly share code, notes, and snippets.

@singhpratyush
Created May 13, 2017 16:48
Show Gist options
  • Save singhpratyush/9837887aa67ce9297949cd92c471ff0e to your computer and use it in GitHub Desktop.
Save singhpratyush/9837887aa67ce9297949cd92c471ff0e to your computer and use it in GitHub Desktop.
A program to plot output of top command, dumped in several files for different processes.
"""
This program plots output of top commands dumped in different files
"""
import sys
import numpy
from matplotlib import pyplot
def clean(line):
"""
Clean a given line for a particular iteration of top. Return VIRT, RES,
SHR, CPU and MEM for the instance. Resolves k, m and g suffix in data.
:param line: Line to be cleaned
:returns: list of the fields
"""
line = line.replace('\n', '')
while ' ' in line:
line = line.replace(' ', ' ')
content = line.split(' ')
content = [content[4], content[5], content[6], content[8], content[9]]
for i in range(len(content)):
if content[i].endswith('g'):
content[i] = float(content[i][:-1]) * 10 ** 9
elif content[i].endswith('m'):
content[i] = float(content[i][:-1]) * 10 ** 6
elif content[i].endswith('k'):
content[i] = float(content[i][:-1]) * 10 ** 3
else:
content[i] = float(content[i])
return content
def load_files(files):
"""
Load and clean files.
:param files: Files to fetch data from
:returns: Dictionary containing data against file name
"""
content = {}
min_size = None
for file in files:
lines = open(file, 'r').readlines()
lines = lines[7:len(lines):9]
content[file] = list(map(clean, lines))
if min_size is None:
min_size = len(content[file])
elif min_size > len(content[file]):
min_size = len(content[file])
for file in content:
content[file] = numpy.array(content[file][:min_size])
return content
def show_single_plot(content, axis, name, xlabel='Iteration', ylabel='Size'):
"""
Plot property from given axis on a graph. Also show average and standard
deviation for each file
:param content: Doctionary containing data against file name
:param axis: Axis of property
:param name: Name of property
:param xlabel: Label for x axis
:param ylabel: Label for y axis
"""
pyplot.title('Comparison of ' + name)
pyplot.grid(True)
for i in content:
data_arr = content[i][:, axis]
display_name = i.split('.')[-2].split('/')[-1]
average = numpy.mean(data_arr)
std = numpy.std(data_arr)
size = len(data_arr)
print('Average of %s for %s: %s' % (name, display_name, average))
print('Std dev of %s for %s: %s' % (name, display_name, std))
pyplot.plot(data_arr, label=display_name)
pyplot.plot([average] * size, label='Average for ' + display_name)
pyplot.legend(loc='best')
pyplot.xlabel(xlabel)
pyplot.ylabel(ylabel)
pyplot.show()
pyplot.close()
def draw_plots(files):
"""
Load data and plot
:param files: Files containing data
"""
content = load_files(files)
for i in [(0, 'VIRT'), (1, 'RES'), (2, 'SHR'), (3, 'CPU'), (4, 'MEM')]:
show_single_plot(content, i[0], i[1])
def main():
files = sys.argv[1:]
draw_plots(files)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment