python做硬件自动化测试-用python做自动化测试--Python实现远程性能监控
http://blog.csdn.net/powerccna/article/details/8044222
在性能测试中,监控被测试服务器的性能指标是个重要的工作,包括CPU/Memory/IO/Network,但大多数人估计都是直接在被测试服务器的运行监控程序。我们开始也是这样做的。但这样做带来一个问题是,测试人员需要在每台被测试服务器上部署监控程序,增加了部署的工作量,而且经常因为Python版本的问题,有些模块不兼容,或者第三方模块需要再次安装。
改进性能测试监控工具:
1. 能远程监控被测试服务器,这样测试人员就不需要在每个被测试机器安装监控工具了。
2. 被测试服务器上不需要安装agent,监控结果能取到本地。
3. 本地服务器上的python模块和兼容性问题,可以通过Python virtualenv解决,每个测试人员维护自己的一套Python环境。
Google了下,找到了pymeter(thttp://pymeter.sourceforge.net/), 看了下源代码,很多工作还没完成,但这个思路和我的是一样的。而且我在其他项目中已经实现了远程发送命令的模块。 所以不如直接在自己的项目上扩展。
远程发送命令的模块开始是基于Pexpect(http://www.noah.org/wiki/Pexpect)实现的, Pexpect很强大,它是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Python 模块。用他来可以很容易实现telnet,ftp,ssh的操作。 但Pexpect无windows下的版本,这是我抛弃它的原因,无法达到测试工具兼容所有系统的要求。 所以就用telent模块替换了Pexpect,实现了远程发送命令和获取结果。
#file name: telnetoperate.py
#!/usr/bin/env python
#coding=utf-8
import time,sys,logging,traceback,telnetlib,socket
class TelnetAction:
def __init__(self,host,prompt,account,accountPasswd,RootPasswd=""):
self.log=logging.getLogger()
self.host=host
self.account=account
self.accountPasswd=accountPasswd
self.RootPasswd=RootPasswd
self.possible_prompt = ["#","$"]
self.prompt=prompt
self.default_time_out=20
self.child =None
self.login()
def expand_expect(self,expect_list):
try:
result=self.child.expect(expect_list,self.default_time_out)
except EOFError:
self.log.error("No text was read, please check reason")
if result[0]==-1:
self.log.error("Expect result"+str(expect_list)+" don"t exist")
else:
pass
return result
def login(self):
"""Connect to a remote host and login.
"""
try:
self.child = telnetlib.Telnet(self.host)
self.expand_expect(["login:"])
self.child.write(self.account+ " ")
self.expand_expect(["assword:"])
self.child.write(self.accountPasswd + " ")
self.expand_expect(self.possible_prompt)
self.log.debug("swith to root account on host "+self.host)
if self.RootPasswd!="":
self.child.write("su -"+" ")
self.expand_expect(["assword:"])
self.child.write(self.RootPasswd+" ")
self.expand_expect(self.possible_prompt)
#self.child.write("bash"+" ")
#self.expand_expect(self.possible_prompt)
self.child.read_until(self.prompt)
self.log.info("login host "+self.host+" successfully")
return True
except:
print "Login failed,please check ip address and account/passwd"
self.log.error("log in host "+self.host+" failed, please check reason")
return False
def send_command(self,command,sleeptime=0.5):
"""Run a command on the remote host.
@param command: Unix command
@return: Command output
@rtype: String
"""
self.log.debug("Starting to execute command: "+command)
try:
self.child.write(command + " ")
if self.expand_expect(self.possible_prompt)[0]==-1:
self.log.error("Executed command "+command+" is failed, please check it")
return False
else:
time.sleep(sleeptime)
self.log.debug("Executed command "+command+" is successful")
return True
except socket.error:
self.log.error("when executed command "+command+" the connection maybe break, reconnect")
traceback.print_exc()
for i in range(0,3):
self.log.error("Telnet session is broken from "+self.host+ ", reconnecting....")
if self.login():
break
return False
def get_output(self,time_out=2):
reponse=self.child.read_until(self.prompt,time_out)
#print "response:",reponse
self.log.debug("reponse:"+reponse)
return self.__strip_output(reponse)
def send_atomic_command(self, command):
self.send_command(command)
command_output = self.get_output()
self.logout()
return command_output
def process_is_running(self,process_name,output_string):
self.send_command("ps -ef | grep "+process_name+" | grep -v grep")
output_list=[output_string]
if self.expand_expect(output_list)[0]==-1:
return False
else:
return True
def __strip_output(self, response):
#Strip everything from the response except the actual command output.
#split the response into a list of the lines
lines = response.splitlines()
self.log.debug("lines:"+str(lines))
if len(lines)>1:
#if our command was echoed back, remove it from the output
if self.prompt in lines[0]:
lines.pop(0)
#remove the last element, which is the prompt being displayed again
lines.pop()
#append a newline to each line of output
lines = [item + " " for item in lines]
#join the list back into a string and return it
return "".join(lines)
else:
self.log.info("The response is blank:"+response)
return "Null response"
def logout(self):
self.child.close()
telnetoperate.py代码说明:
1. __init__(self,host,prompt,account,accountPasswd,RootPasswd="")
这里用到了多个登陆账号(account,root),原因是我们的机器开始不能直接root登陆,需要先用普通用户登陆,才能切换到root账号,所以这里出现了account, rootPasswd这2个参数,如果你的机器可以直接root账号登陆,或者你不需要切换到root账号,可以就用account, accountPasswd就可以了。
prompt是命令行提示符,机器配置不一样,可能是$或者#,用来判断一个命令执行是否完成。
2. send_command(self,command,sleeptime=0.5)
这里sleeptime=0.5是为了避免很多机器性能不好,命令执行比较慢,命令还没返回,会导致获取命令后的结果失败。如果你嫌这样太慢了,可以调用的时候send_command(command,0)
process_is_running(
process_name
监控远程机器:
#simplemonitor.py
#!/usr/bin/env python
#coding=utf-8
import time
import telnetoperate
remote_server=telnetoperate.TelnetAction("192.168.23.235","#","user","passwd123")
#get cpu information
cpu=remote_server.get_output("sar 1 1 |tail -1")
memory=remote_server.get_output("top | head -5 |grep -i memory")
io=remote_server.get_output("iostat -x 1 2|grep -v "^$" |grep -vi "dev"")
这样在任何一台机器上就可以实现监控远程多个机器了,信息集中化管理,方便进一步分析。如果你想cpu, memory, io独立的监控,可以多线程或者起多个监控进程,在多线程中需要注意的时候,必须对每个监控实例建立一个telnet连接,get_output是从telnet 建立的socket里面去获取数据,如果多个监控实例用同一个socket会导致数据混乱。
python做硬件自动化测试-用python做自动化测试--Python实现远程性能监控相关推荐
- python可以做硬件吗_Micropython让你会python就能做硬件
0 引言python真得使用越来越广泛了,记得我在6年前学习python时,当时还只是把它做为一个脚本语言来看,没事学着大神们搞搞网络安全的相关知识.然后,几年用python的应用遍地都是,让人更加吃 ...
- java和python的web自动化有什么区别-Python和Java哪个更适合做自动化测试?
Python:易于学习,语法简洁 不可否认的是,Python相对于其他语言来说,要容易的多.因为Python语言的简单明了,很多外行人也能读懂它的代码.Python语言不需要拥有很丰富的词汇,简单明了 ...
- python做硬件自动化测试仪器_基于Python PyVisa和GPIB的硬件测试仪器控制方法
基于Python和GPIB的硬件测试仪器控制方法 背景 在物联网通信时代,嵌入式模块开发越发广泛,自动化测试成为大家老生常谈的话题.对于一些高精度仪器,我们知道它是用GPIB控制用来测试,也希望可以通 ...
- python自动测试p-python网络爬虫之自动化测试工具selenium[二]
@ 前言 hello,大家好,在上章的内容里我们已经可以爬取到了整个网页下来,当然也仅仅就是一个网页. 因为里面还有很多很多的标签啊之类我们所不需要的东西. 额,先暂且说下本章内容,如果是没有丝毫编程 ...
- python编程做什么工作-学习Python编程后在成都可以做哪些工作?
原标题:学习Python编程后在成都可以做哪些工作? Python被称为编程语言中的万能胶水,比如可以用来做爬虫,做网页,运维还有现在很火的人工智能Al都可以用上Python.一是Python有很多数 ...
- Python自动化测试(1)-自动化测试及基本技术手段概述
生产力概述 在如今以google为首的互联网时代,软件的开发和生产模式都已经发生了变化, 在<参与感>一书提到:某位从微软出来的工程师很困惑,微软在google还有facebook这些公司 ...
- python能做什么脚本_Python能做什么
Python 作为一种功能强大的编程语言,因其简单易学而受到很多开发者的青睐.那么,Python 的应用领域有哪些呢? 概括起来,Python 的应用领域主要有如下几个. Web应用开发 Python ...
- python自动化测试脚本后端_基于 python 的接口自动化测试
本文来自作者:孙彦辉 在 GitChat 上精彩分享,「阅读原文」看看大家和作者交流了哪些问题 一.简介 本文从一个简单的登录接口测试入手,一步步调整优化接口调用姿势: 然后简单讨论了一下接口测试框架 ...
- python 接口自动化的sql验证_基于Python的接口自动化实战-基础篇之pymysql模块操做数据库...
引言 在进行功能或者接口测试时经常须要经过链接数据库,操做和查看相关的数据表数据,用于构建测试数据.核对功能.验证数据一致性,接口的数据库操做是否正确等.所以,在进行接口自动化测试时,咱们同样绕不开接 ...
最新文章
- 科大讯飞与优刻得、寒武纪等联合设立合肥智能语音创新发展有限公司
- 2011 Michigan Invitational Programming Contest
- mysql5.7.17配置_mysql5.7.17安装配置
- 【图文详解】Mysql8.0安装教程
- some demos
- Kubernetes中分布式存储Rook-Ceph的使用:一个ASP.NET Core MVC的案例
- 一个例子彻底搞懂C++的虚函数和纯虚函数
- 免费!200块全志XR806开源鸿蒙开发板试用
- Delphi Format函数功能及用法详解
- 实例讲解朴素贝叶斯分类器
- django种表单post出现CSRF verification failed( CSRF验证失败 ) 的两种解决方式
- Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
- 关于保利威视平台的API调用签名
- yolov5 deepsort 行人车辆 双向计数 跟踪检测 | 开源项目分享
- android 顶部弹窗,android显示通知,并在任何应用程序顶部弹出
- 个人关于颈椎病治疗和保养的一些经验总结!
- 题目 1904: 蓝桥杯算法提高VIP-求arccos值
- (附源码)ssm小型超市管理系统的设计与实现 毕业设计 011136
- 计算机组装硬件要求,组装电脑必懂的硬件知识,全是干货,教你选购硬件不求人...
- python用一维数组存储学号和成绩_用一维数组存储学号和成绩,然后,按成绩排序输出...
热门文章
- 二、【List、Set、数据结构、Collections】
- MySQL 常用内置函数
- GitHub+Hexo搭建自己的Blog之-本地环境部署01
- require.js用法简介
- 热血街头Java,下载_我爱法语 V3.01 多国语言版_6z6z下载站
- .net core EPPlus npoi_2020 ASP.NET界面开发:DevExpress v20.1支持.NET Core设计时
- redis的java客户端名称_java里常用的redis客户端简介
- Linux文件属性3——文件权限管理
- JVM内存溢出时快照转存HeapDump到文件
- Vue-watch选项