性能测试培训: 监控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测试开 ...
随机推荐
- C#开发人员应该知道的13件事情
本文讲述了C#开发人员应该了解到的13件事情,希望对C#开发人员有所帮助. 1. 开发过程 开发过程是错误和缺陷开始的地方.使用工具可以帮助你在发布之后,解决掉一些问题. 编码标准 遵照编码标准可以编 ...
- Redis之Sentinel高可用安装部署
背景: 之前通过Redis Sentinel 高可用实现说明和Redis 复制.Sentinel的搭建和原理说明大致已经介绍了sentinel的原理和实现,本文再次做个简单的回顾.具体的信息见前面的两 ...
- BZOJ ac100题存档
不知不觉AC100题了,放眼望去好像都是水题.在这里就做一个存档吧(特别感谢各位大神尤其是云神http://hi.baidu.com/greencloud和丽洁姐http://wjmzbmr.com/ ...
- nodejs 代码设计模式1:同步函数变异步
同步函数变异步 1 问题: 1.1 碰到需要调用你刚正在创建的对像. function createServer(data, cb) { data.num = 1; cb(); return data ...
- 从Angular2路由引发的前后端路由浅谈
笔者的学习进度比较慢,直到两年以前写的网站都还是以服务端为主导的,即网站的所有视图都由服务器视图模板来渲染,笔者使用的是 DotNet MVC,开发套路就是在Controller里面写Action,在 ...
- oracle 用系统用户以SYSDBA身份登陆
最近发现很多人问我 这么直接用系统OS用户 登陆 oracle : 1.首先通过用管理身份打开DOS命令窗口: 然后使用命令: sqlplus / as sysdba 即可: 其实这个命令和用sys用 ...
- IBatis入门
iBatis 简介: iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快.如果不需要太多复杂的功能,iBatis 是能够满足 ...
- MYSQL创建用户Unknown column 'plugin' in 'mysql.user'的解决方法
执行下列sql ALTER TABLE `user` ADD `Create_tablespace_priv` ENUM('N','Y') NOT NULL DEFAULT 'N' AFTER `Tr ...
- Javascript基础 - js中曾经忽略的知识点
深入那些曾经忽略的Javascript知识 1. parseInt(string, [radix]),parseFloat(string) 一般我们省略第二个参数,parseInt(‘100’) == ...
- 利刃 MVVMLight 4:绑定和绑定的各种使用场景
一.绑定: 主要包含元素绑定和非元素绑定两种. 1.元素绑定,是绑定的最简单形式,源对象是WPF的元素,并且源对象的属性是依赖项属性. 根据我们之前的知识 ,依赖项属性具有内置的更改通知支持.所以当我 ...