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

时间:2022-09-05 17:46:06

性能测试培训: 监控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&plus;influxdb&plus;granfana&plus;collectd监控cpu&plus;mem&plus;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. Java避免创建不必要的对象

    小Alan最近看到了<Effective Java>这本书,这本书包含的内容非常丰富,这本书我就不多介绍了,只能默默的说一句,作为一名java开发错过了这本书难免会成为一个小遗憾,所以还是 ...

  2. &lbrack;转&rsqb;C&num;调用FFMPEG&comma;并异步读取输出信息的代码

    http://www.cnblogs.com/Leo_wl/archive/2011/12/31/2308841.html

  3. 【逆向怎么玩】 动态调试一款牛逼C&plus;&plus; IDE实录

    声明 本篇只从逆向兴趣出发,研究其程序运行原理. CLion程序版权为jetBrains所有. 注册码授权为jetBrains及其付费用户所有. 不会释出任何完整的源代码. 涉及能直接推算出注册码的地 ...

  4. NYOJ--27--dfs--水池数目

    /* Name: NYOJ--27--水池数目 Author: shen_渊 Date: 17/04/17 15:42 Description: 经典dfs水题,,, */ #include<i ...

  5. api-gateway实践(08)新服务网关 - 云端发布和日志查看

    一.发布应用 1.新建应用空间 1.1.新建应用空间 1.2.新建应用 1.3.上传程序包 2.创建应用引擎服务 3.发布应用 3.1.为应用容器绑定Web运行环境(应用引擎服务) 3.2.发布应用( ...

  6. 求导程序编写&lpar;oo-java编程&rpar;

    本单元的任务为求导. 即将一个含自变量x的多项式F求导成为另外一个含自变量x的多项式f.使得 dF/dx = f 为降低我们的难度,这个任务被分解成了三个阶段: (1)对幂函数进行求导(不允许嵌套) ...

  7. &lbrack;oracle&rsqb; Oracle存储过程里操作BLOB的字节数据的办法,例如写入32位整数

    作者: zyl910 一.缘由 BLOB是指二进制大对象,也就是英文Binary Large Object的缩写. 在很多时候,我们是通过其他编程语言(如Java)访问BLOB的字节数据,进行字节级的 ...

  8. python3自学第二天,模块,三元运算

    1.模块的认识. sys模块,os模块等 如何引入模块 import os cmd_res1=os.system("dir") # 执行命令dir,不保存结果 print(cmd_ ...

  9. 【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。

    下面程序演示了在嵌入式Linux和PC机Linux下使用popen函数时,程序的运行结果是有差异的. 两个程序 atest.c 和 btest.c,atest 检查是否有 btest 进程运行,如果没 ...

  10. 彻底删除vscode及安装的插件和个人配置信息

    1.卸载vscode应用软件(在控制面板里面找不到改软件,所以只能进入应用所在文件夹进行卸载) ## 此步骤虽然删掉了应用软件,但是此时重新安装会发现之前下载的插件和个人配置信息都还会重新加载出来,所 ...