Python获取单个程序CPU使用情况趋势图

作者:junjie 时间:2021-11-19 02:36:59 

本文定位:已将CPU历史数据存盘,等待可视化进行分析,可暂时没有思路。
前面一篇文章(https://www.jb51.net/article/61956.htm)提到过在linux下如何用python将top命令的结果进行存盘,本文是它的后续。

python中我们可以用matplotlib很方便的将数据可视化,比如下面的代码:


import matplotlib.pyplot as plt

list1 = [1,2,3]
list2 = [4,5,9]
plt.plot(list1,list2)
plt.show()

执行效果如下:

Python获取单个程序CPU使用情况趋势图

上面只是给plot函数传了两个list数据结构,show一下图形就出来了……哈哈,很方便吧!
获取CPU趋势图就用这个了!
可我们现在得到的数据没那么友好,比如我现在有个文件(file.txt),内容如下:


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

其中,第一列为时间,第六列为CPU的idle值。

要从这组数据中得出CPU使用情况趋势图,我们就要做些工作了。

下面是代码,这里提供一个思路,需要的朋友拷回去改一下吧:


#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]) # 得到key
        t_value = 100.001-string.atof(line.split(':')[1].split(',')[3].split('%')[0]) # 得到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")
# 生成CPU使用情况趋势图
list1 = retMap1.keys()
list1.sort()
list2 = []
for i in list1:list2.append(retMap1[i])
plt.plot(list1,list2)
plt.show()

好,就这些了,希望对你有帮助。

标签:Python,单个程序,CPU,趋势图
0
投稿

猜你喜欢

  • Python使用os.listdir和os.walk获取文件路径

    2023-01-30 11:02:10
  • pandas中merge()函数的用法解读

    2023-10-02 08:49:46
  • Python分析彩票记录并预测中奖号码过程详解

    2023-07-20 04:49:18
  • Jmeter并发执行Python 脚本的完整流程

    2021-05-12 02:52:49
  • SQL对时间处理的语句小结

    2011-12-01 07:53:04
  • Python合并多个装饰器小技巧

    2022-05-31 04:51:45
  • 谈一谈bootstrap响应式布局

    2023-08-22 20:17:39
  • php中Array2xml类实现数组转化成XML实例

    2023-07-14 21:48:13
  • Django 限制访问频率的思路详解

    2021-08-17 16:52:57
  • IE7下 filter:Alpha(opacity=xx) 的小问题

    2008-12-02 16:24:00
  • Python中pass的作用与使用教程

    2023-05-05 23:05:05
  • Python模块结构与布局操作方法实例分析

    2021-11-26 10:49:31
  • 详解python配置虚拟环境

    2021-08-02 22:02:50
  • asp的分词技术

    2007-08-25 17:50:00
  • 在PyCharm的 Terminal(终端)切换Python版本的方法

    2021-10-31 08:37:07
  • python中使用asyncio实现异步IO实例分析

    2021-02-06 10:02:50
  • 使用cx_freeze把python打包exe示例

    2021-06-10 05:36:20
  • 使用MySQL数据库的23个注意事项

    2010-03-18 15:46:00
  • ASP实现最简洁的多重查询的解决方案

    2007-09-28 12:00:00
  • Dreamweaver如何防止及消除垃圾代码的产生

    2007-11-13 17:15:00
  • asp之家 网络编程 m.aspxhome.com