性能测试培训: 监控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. VMware上安装MS-DOS 6.22之一:基本系统的安装

    在网上看了一遍,发现关于在VMware上安装DOS的教程比较少,并且还有很多人发问怎么在VMware上安装DOS.为了方便大家,我便把我实际安装DOS的步骤讲出来分享一下,当然这其中也参考了一些网上资 ...

  2. React Native与原生项目连接与发布

    前面的各种环境配置按照官方文档一步一步来,挺详细,宝宝在这里就不多说废话了. 其次,前面的配置,我参照的这个博主的文章React Native 集成到iOS原生项目 下面是宝宝掉过的坑(半径15M): ...

  3. ios 相机调用之读取相册

    UIIamgePickerControllerr可以从照片库中读取一张图片到咱们应用程序中来   步骤:   //创建图片判断图片库是否可以使用   if([UIImagePickerControll ...

  4. lxd-启动篇分析

    lxd是什么:lxd是基于lxc构筑的容器管理进程,提供镜像,网络,存储,以及容器的能力,对外暴漏restfull API.其与docker的区别是docker更切近与app container,以应 ...

  5. 做推送,怎么能不了解推送的 4 种消息形式呢?(iOS 篇)

    极光推送是为 App 提供第三方推送服务的平台之一,它提供四种消息形式:通知,自定义消息,富媒体和本地通知.笔者将基于官方说明与个人理解来谈一下这四种消息.本篇为 iOS 篇,Android 篇入口. ...

  6. github学习(二)

    Git学习(一) 学习github一定要学会git,否则在后续的github运用中会出现很多问题. 1.安装Git: Mac自带Git,Windows需要自己安装. 2.配置git: 配置user.n ...

  7. daterangepicker 时间插件

    在工作中学习到的一种插件 上网查询之后发现 在bootstrap中的时间选择器有两种:dateTimePicker和dateRangePicker HTML <div id="repo ...

  8. 转:js中cookie的使用详细分析

    cookie机制将信息存储于用户硬盘,因此可以作为全局变量,这是它最大的一个优点.它可以用于以下几种场合. (1)保存用户登录状态.例如将用户id存储于一个cookie内,这样当用户下次访问该页面时就 ...

  9. BUG,带给我的思考

    今天打开EverNote时,翻到了四年前在anjuke时做的一些bug分析总结.现在回过头看看也是有些价值所在,挑选出部分bug分享,希望能有所启发. 一. iOS新房APP4.4由于在91市场进行试 ...

  10. String字符串截取跟替换经典案例

    分享下今天的一个面试题吧!不算有难度,但是没做出来 题目:将String  str="姓名:武亚伟,年龄:27,地址:西安市": 输出结果为:姓名=武亚伟 年龄=27 地址=西安市 ...