雖然寫的很不完善,但是當練手吧,對於實時監控cpu還是有點用處的,雖然android studio已經提供了一樣的功能,但是能夠記錄並生成成本地csv檔案還是比as多乙個功能的。結果圖:
直接放原始碼:
# !_*_coding:utf-8_*_
import re
import os
import wx
import thread
import pychartdir
import csv
class
getcpustats
():def
__init__
(self, name):
self.pro_name = name
defstartwatch
(self):
_shell = 'adb shell top -s cpu -n 1| findstr %s' % self.pro_name
result = os.popen(_shell).read()
return self.getinfo(result)
defgetinfo
(self, _result):
return [re.findall('(\d+)%', _result), re.findall('k\s(\d+)k', _result)]
class
myframe
(wx.frame):
flag = false
_label =
_data =
_label02 =
_data02 =
num = 0
num02 = 0
def__init__
(self):
wx.frame.__init__(self, parent=none, title=u'cpu檢測小工具', pos=(700, 100), size=(500, 500))
mpanel = wx.panel(parent=self)
msizer = wx.boxsizer(orient=wx.vertical)
btn_start = wx.button(parent=mpanel, label=u'開始', style=wx.expand)
msizer.add(btn_start)
btn_stop = wx.button(parent=mpanel, label=u'停止', style=wx.expand)
msizer.add(btn_stop)
self.edit = wx.textctrl(parent=mpanel,style=wx.expand,value=u'這裡輸入測試的包名')
msizer.add(self.edit,flag=wx.expand)
self.edit.bind(event=wx.evt_choice, handler=self.requestfocus)
self.bitmap_cpu = wx.staticbitmap(parent=mpanel)
msizer.add(self.bitmap_cpu, flag=wx.expand)
#self.bitmap_mem = wx.staticbitmap(parent=mpanel)
#msizer.add(self.bitmap_mem, flag=wx.expand)
mpanel.setsizer(msizer)
mpanel.fit()
btn_start.bind(event=wx.evt_button, handler=self.startwatch)
btn_stop.bind(event=wx.evt_button, handler=self.stopwatch)
defrequestfocus
(self,event):
self.edit.setvalue("")
defstartwatch
(self, event):
self.flag = true
thread.start_new_thread(self.getinfo, ())
defstopwatch
(self, event):
self.flag = false
defgetinfo
(self):
while(self.flag):
_result = getcpustats(self.edit.getvalue()).startwatch()
self.drawpic(_result)
#self.drawpic_mem(_result)
defwritecsv
(self, _list):
writer = csv.writer(file('tmp.csv', 'a+'))
writer.writerow([_list])
defdrawpic
(self,_list):
self.num+=1
self.writecsv(_list[0][0])
c = pychartdir.xychart(500, 400)
c.setplotarea(30, 20, 400, 300)
c.addlinelayer(self._data)
c.xaxis().setlabels(self._label)
c.makechart("tmp.png")
mimage = wx.image("tmp.png")
mimage = mimage.scale(500,300)
self.bitmap_cpu.setbitmap(wx.bitmapfromimage(mimage))
defdrawpic_mem
(self,_list):
self.num02+=1
c = pychartdir.xychart(500, 400)
c.setplotarea(60, 40, 400, 300)
c.addlinelayer(self._data02)
c.xaxis().setlabels(self._label02)
c.makechart("tmp02.png")
mimage = wx.image("tmp02.png")
mimage = mimage.scale(500,300)
self.bitmap_mem.setbitmap(wx.bitmapfromimage(mimage))
if __name__ == '__main__':
myframe().show()
原理:
介面用wx做的,圖形用pychartdir做的,命令用的是top命令,每次擷取到的資料都生成一張臨時,然後重新整理到螢幕上,看起來就是實時資料監控的效果了,基本上bug一大堆,也懶得改了。
CPU實時監控mpstat
mpstat是multiprocessor statistics的縮寫,是實時系統監控工具。其報告與cpu的一些統計資訊,這些資訊存放在 proc stat檔案中。在多cpus系統裡,其不但能檢視所有cpu的平均狀況資訊,而且能夠檢視特定cpu的資訊。mpstat最大的特點是 可以檢視多核心cpu中...
Python實時監控檔案方法
python logfile access.log command tail f logfile grep timeout popen subprocess.popen command,stdout subprocess.pipe,stderr subprocess.pipe,shell true ...
基於svg的實時監控曲線的實現
為了完成演示的效果,把源 總的ajax部分省略調,解壓就可以在ie中看演示效果,當然需要安裝svg viewer.使用者可以更改lib.js的如下引數改變圖形位置,曲線條數,座標,線段條數以及顏色等等 x width 網格區域寬度 var rdwid 800 y width 網格區域高度 var r...