Python gets a single program CPU usage trend Graph _python

Source: Internet
Author: User
Tags cpu usage in python

This article positioning: The CPU history data has been saved, waiting for visualization analysis, can not be thought of for a moment.
This article is a follow-up to the previous article (http://www.jb51.net/article/61956.htm), which mentions how to save the results of the top command in Python under Linux.

In Python, we can easily visualize data using matplotlib, such as the following code:

Copy Code code as follows:

Import Matplotlib.pyplot as Plt

List1 = [1,2,3]
List2 = [4,5,9]
Plt.plot (LIST1,LIST2)
Plt.show ()

The execution effect is as follows:

Above just give the plot function to pass two list data structure, show the graph to come out ... Haha, very convenient!
Get the CPU Trend chart with this!
But the data we're getting is not that friendly, like I have a file (file.txt) that reads:

Copy Code code as follows:

Cpu (s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu (s): 7.7%us, 7.7%sy, 0.0%ni, 76.9%id, 0.0%wa, 0.0%hi, 7.7%si, 0.0%st
Cpu (s): 0.0%us, 9.1%sy, 0.0%ni, 90.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu (s): 9.1%us, 0.0%sy, 0.0%ni, 90.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu (s): 8.3%us, 8.3%sy, 0.0%ni, 83.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu (s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu (s): 0.0%us, 9.1%sy, 0.0%ni, 90.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu (s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st

Where, the first column is the time, and the idle value of the CPU is listed.

To get the CPU usage trend graph from this set of data, we need to do some work.

Here is the code, here to provide a train of thought, need a friend cuff back to change it:

Copy Code code as follows:

#coding: Utf-8
'''
File:cpuUsage.py
Author:mike
E-mail:mike_zhang@live.com
'''
Import Matplotlib.pyplot as Plt
Import string

def getcpuinfdata (fileName):
ret = {}
f = open (FileName, "R")
LineList = F.readlines ()
For line in LineList:
TMP = Line.split ()
SZ = LEN (TMP)
T_key = String.atoi (tmp[0]) # get key
T_value = 100.001-string.atof (Line.split (': ') [1].split (', ') [3].split ('% ') [0]) # Get value
Print T_key,t_value
If not Ret.has_key (T_key):
Ret[t_key] = []
Ret[t_key].append (T_value)
F.close ()
return ret

RetMap1 = Getcpuinfdata ("file.txt")
# Generate CPU Usage Trend Chart
List1 = Retmap1.keys ()
List1.sort ()
List2 = []
For I in List1:list2.append (Retmap1[i])
Plt.plot (LIST1,LIST2)
Plt.show ()

Well, that's all, I hope to help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.