性能测试培训: 监控CPU之python

作为一名测试开发工程师,开发脚本是为了测试服务的,我们在手里没有性能监控工具的情况下,我们会自己来进行开发脚本完成监控任务。下面是python监控cpu

'''

Created on 2015-10-20

@author:laoli

'''

#from sshExpert import sshExpert

import time

import os,sys,platform

class cpuMonitor:

def __init__(self):

return

def getCpu(self):

sysName = platform.system()

if sysName == 'Windows':

return self.getWinCpu()

else:

return self.getLinuxCpu()

return

def getWinCpu(self):

import win32com.client

com=win32com.client.Dispatch("WbemScripting.SWbemRefresher")

obj=win32com.client.GetObject("winmgmts:\\root\cimv2")

processorItems=com.AddEnum(obj,"Win32_PerfFormattedData_PerfOS_Processor").objectSet

while(1):

com.Refresh()

for item in processorItems:

if item.Name == '_Total':

#print item.Name,"= ",

#print 'total=',item.PercentProcessorTime,"%"

return item.PercentProcessorTime

#time.sleep(5)

break

def _read_cpu_usage(self):

"""Read the current system cpu usage from /proc/stat."""

try:

fd = open("/proc/stat", 'r')

lines = fd.readlines()

finally:

if fd:

fd.close()

for line in lines:

l = line.split()

if len(l) < 5:

continue

if l[0].startswith('cpu'):

return l

return []

def getLinuxCpu(self):

"""

get cpu avg used by percent

"""

cpustr=self._read_cpu_usage()

if not cpustr:

return 0

#cpu usage=[(user_2 +sys_2+nice_2) - (user_1 + sys_1+nice_1)]/(total_2 - total_1)*100

usni1=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])+long(cpustr[5])+long(cpustr[6])+long(cpustr[7])+long(cpustr[4])

usn1=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])

#usni1=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])+long(cpustr[4])

# self.sleep=2

time.sleep(self.sleep)

cpustr=self._read_cpu_usage()

if not cpustr:

return 0

usni2=long(cpustr[1])+long(cpustr[2])+float(cpustr[3])+long(cpustr[5])+long(cpustr[6])+long(cpustr[7])+long(cpustr[4])

usn2=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])

cpuper=(usn2-usn1)/(usni2-usni1)

return 100*cpuper

def main():

aaa = cpuMonitor()

print aaa.getCpu()

if __name__ == '__main__':

main()

性能测试培训: 监控CPU之python的更多相关文章

  1. 第一个python实例--监控cpu

    #第一个python实例:监控cpu #/bin/bash/env Python from __future__ import print_function from collections impo ...

  2. 性能测试培训:帮你定位 Linux 性能问题的 18 个命令以及工具

    性能测试培训:帮你定位 Linux 性能问题的 18 个命令以及工具 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在popte ...

  3. 性能测试培训:分布式测试之jmeter

    性能测试培训:分布式测试之jmeter   在使用Jmeter进行性能测试时,如果并发数比较大(比如最近项目需要支持1000并发),单台电脑的配置(CPU和内存)可能无法支持,这时可以使用Jmeter ...

  4. 性能测试培训:定位jvm耗时函数

    性能测试培训:定位jvm耗时函数   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:90882 ...

  5. jmeter+influxdb+granfana+collectd监控cpu+mem+TPS

    1.安装grafana #####gafana过期安装包安装报错 Error unpacking rpm package grafana-5.1.4-1.x86_64error: unpacking ...

  6. 监控CPU和内存的使用

    监控CPU和内存的使用: #!/bin/bash #script to capture system statistics outfile=/home/rainbow/test/file.csv da ...

  7. 性能测试培训:tomcat性能调优方法

    性能测试培训:tomcat性能调优方法   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest的loadrunner ...

  8. 性能测试培训:Ajax接口级性能测试之jmeter版

    性能测试培训:Ajax接口级性能测试之jmeter版   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest认为工具 ...

  9. 性能测试培训:WebSocket协议的接口性能之Jmeter

    性能测试培训:WebSocket协议的接口性能之Jmeter poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.poptest测试开 ...

随机推荐

  1. gridcontrol显示行号,总行,打印,导出Excel,设置标头及内容居中方法

    1.一般为了表格显示数据更直观,经常会显示行号以及总数.让gridcontrol显示行号,首先你需要设置一下显示行号的宽度,也就是IndicatorWith.默认值为-1,可根据实际数值需要设置宽度, ...

  2. [转]部署Let’s Encrypt免费SSL证书&&自动续期

    最近公司网站要用https,从自己摸索到找到国内的免费证书到选购正式的收费证书,最后老板说:太贵!不要.一脸懵逼的听老板提到Let's Encrypt证书,没办法,用呗.之前是有一些了解,国外发布的一 ...

  3. 自定义checkbox, radio样式

    17.2.25.nimil 今天开始做百度前端学院的任务,第一个是自定义checkbox, radio样式. checkbox和radio两个标签是不可以改变样式的,background-color. ...

  4. 使用jQuery操作DOM

    一.DOM操作分为3类 1.DOM Core DOM Core不是Javascript的专属品,任何一种支持DOM的编程语言都可以使用它.它的用途不仅限于处理一种使用标记语言编写出来的文档 2.HTM ...

  5. echarts动态添加数据(饼图为例)

    $.ajax({type : "POST",async : false,url : '${ctx}/basic/bsAllPictureGuarantee/pictJson',da ...

  6. 【需求工程】KANO模型

    引言 1979年10月东京理工大学教授狩野纪昭(Noriaki Kano)和其同事 Fumio Takahashi发表的论文 <Motivator and Hygiene Factor in Q ...

  7. javascript的字符串判断方法

    Javascript中判断符号主要有:==.!=.===.!== ==.!=这两个符号在判断之前会先对变量类型进行转换,如果类型相同会再比较值; ===.!==这是直接判断两个变量的类型,如果类型不一 ...

  8. ceph-deploy install时,远端节点在执行apt-get update命令时失败

    环境 OS:Ubuntu 16.04 背景 使用ceph-deploy部署Ceph集群,调用ceph-deploy install命令在远端节点安装ceph环境,执行apt-get update命令时 ...

  9. Bootstrap学习-排版

    1.标题 <h1>~<h6>,所有标题的行高都是1.1(也就是font-size的1.1倍). 2.副标题 <small>,行高都是1,灰色(#999) <h ...

  10. 3997: [TJOI2015]组合数学

    3997: [TJOI2015]组合数学 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 247  Solved: 174[Submit][Status ...