性能测试培训: 监控CPU之python
性能测试培训: 监控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的更多相关文章
- 第一个python实例--监控cpu
#第一个python实例:监控cpu #/bin/bash/env Python from __future__ import print_function from collections impo ...
- 性能测试培训:帮你定位 Linux 性能问题的 18 个命令以及工具
性能测试培训:帮你定位 Linux 性能问题的 18 个命令以及工具 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在popte ...
- 性能测试培训:分布式测试之jmeter
性能测试培训:分布式测试之jmeter 在使用Jmeter进行性能测试时,如果并发数比较大(比如最近项目需要支持1000并发),单台电脑的配置(CPU和内存)可能无法支持,这时可以使用Jmeter ...
- 性能测试培训:定位jvm耗时函数
性能测试培训:定位jvm耗时函数 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:90882 ...
- jmeter+influxdb+granfana+collectd监控cpu+mem+TPS
1.安装grafana #####gafana过期安装包安装报错 Error unpacking rpm package grafana-5.1.4-1.x86_64error: unpacking ...
- 监控CPU和内存的使用
监控CPU和内存的使用: #!/bin/bash #script to capture system statistics outfile=/home/rainbow/test/file.csv da ...
- 性能测试培训:tomcat性能调优方法
性能测试培训:tomcat性能调优方法 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest的loadrunner ...
- 性能测试培训:Ajax接口级性能测试之jmeter版
性能测试培训:Ajax接口级性能测试之jmeter版 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest认为工具 ...
- 性能测试培训:WebSocket协议的接口性能之Jmeter
性能测试培训:WebSocket协议的接口性能之Jmeter poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.poptest测试开 ...
随机推荐
- Memcached服务安装
安装Memcached服务 memcache分为服务端和客户端程序 服务端程序用来支持存储k-v值,程序名称memcached 客户端与服务端通信,进行存取值(常用的如php的memcache扩展,m ...
- .NET Core log4net 使用
log4net .NET Core 版使用,log4net 2.0.7版发布也有一段时间了,从2.0.6 版开始就已经支持.NET Core. 之前有介绍NLog .NET Core版的使用,ASP. ...
- 关于jquery中封装函数如何传递当前元素的问题
需要对页面上一组元素进行ajax操作 <div id="aid"></div> <div id="aid"></div ...
- LOGISTIC回归分析
前面的博客有介绍过对连续的变量进行线性回归分析,从而达到对因变量的预测或者解释作用.那么如果因变量是离散变量呢?在做行为预测的时候通常只有"做"与"不做的区别" ...
- Lucas,.net Web框架
#LucasLucasLucasWeb框架是基于新的魔法糖思想创建的框架.本框架有两大特点.1.所有框架里最简单易学易配置的.2.所有框架里效率最快的.1.UrlRouter没有使用IOC注入及反射, ...
- mysql之连接localhost与127.0.0.1的区别
引言 在聊天群里看到有人提到 mysql 中 localhost 和 127.0.0.1 的区别,这个之前并没有仔细考虑过.现在来学习下. localhost 与 127.0.0.1 区别 local ...
- 读阮一峰对《javascript语言精粹》的笔记,我有疑问。
<javascript语言精粹>是一本很棒的书籍,其中作者在附录列出了12种他所认为的javascript语言中的糟粕. 我最近开始跟读前端前辈的博客,其中读到了阮一峰的<12种不宜 ...
- 【js】性能问题
执行环境和作用域 一.全局变量和局部变量 每个执行环境都有与之关联的变量对象(变量和函数存储在这里),全局执行环境是最外围的执行环境,根据ECMA实现所在的宿主不同,表示的执行环境的对象也不同.在we ...
- ubuntu Error mounting /dev/sda6 at /media/xxx...
问题原因:可能是windows没有完全关机,详细请看我的另一博文: http://www.cnblogs.com/ediszhao/p/3794459.html 为了学习linux我装了双系统,因为我 ...
- HDFS Namenode启动过程
文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/6564032.html 转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点, ...