jvm版本: 1.8.0_65javascript
sahi版本: Sahi Pro 6.1.0html
参考来源:java
Sahi官网node
Sahi官方文档:压力测试chrome
Sahi社区论坛:SAHI for performance testing数据库
Sahi (1) —— 快速入门(101 Tutorial)macos
Sahi (2) —— https/SSL配置(102 Tutorial))浏览器
Sahi的压力测试脚本包含了2个子脚本,一个是用来生成压力的,这个脚本被称为“noise”,跑在一个无头(headless)的浏览器上,在“noise”文件中的脚本会一直无限循环执行下去。另外一个脚本被称为“subject”,这个脚本是被用来定时执行,而后查看不一样压力下的性能指标的。cookie
因为“noise”是经过PhantomJS在后台运行的,因此咱们须要先安装PhantomJS。若是有brew的童鞋能够简单运行下面命令行安装:
brew install phantomjs
写这篇文章的时候PhantomJS的最新稳定版本是2.0.0,可是咱们须要较低的1.9.8版本
$ phantomjs --version 2.0.0
缘由是Sahi6.1(也是当前最新稳定版)这个版本对于PhantomJS只支持到1.x,若是使用2.0会出现执行时卡死的状况。
如下是Mac(Linux)环境下的安装步骤:
下载PhantomJS到 /usr/local/share
解压后执行如下命令(1.9如下版本须要)
sudo ln -s /usr/local/share/phantomjs-1.9.8-macosx/bin/phantomjs /usr/local/share/phantomjs sudo ln -s /usr/local/share/phantomjs-1.9.8-macosx/bin/phantomjs /usr/local/bin/phantomjs sudo ln -s /usr/local/share/phantomjs-1.9.8-macosx/bin/phantomjs /usr/bin/phantomjs
完成后能够经过命令行查看当前PhantomJS的版本号
$ phantomjs --version
在Dashboard上点击Configure加入如下配置
<browserType> <name>phantomjs</name> <displayName>PhantomJS</displayName> <icon>phantomjs.png</icon> <path>$sahiDir/ext/phantomjs/sahi_phantomjs.sh</path> <options>--proxy=localhost:9999 $sahiDir/ext/phantomjs/phantomsahi.js</options> <processName>phantomjs</processName> <capacity>100</capacity> <useSystemProxy>false</useSystemProxy> </browserType>
对于SSL错误,还须要修改配置为(--ignore-ssl-errors=yes):
<browserType> <name>phantomjs</name> <displayName>PhantomJS</displayName> <icon>phantomjs.png</icon> <path>$sahiDir/ext/phantomjs/sahi_phantomjs.sh</path> <options>--ignore-ssl-errors=yes --debug=yes --proxy=localhost:9999 $sahiDir/ext/phantomjs/phantomsahi.js</options> <processName>phantomjs</processName> <capacity>100</capacity> <useSystemProxy>false</useSystemProxy> </browserType>
保存并重启Sahi
命令行运行并测试
$ ./testrunner.sh demo/sahi_demo.sah http://sahitest.com/demo/ phantomjs
若是成功查看log:
Sahi6.1官方在安装包下提供了压力测试的示例,咱们尝试运行该测试。
首先启动Sahi Dashboard:
$ ./start_dashboard.sh
再运行:
$ ./dload.sh demo/load/noise.sah demo/load/subject.sah http://sahitest.com/demo/training/ firefox
若是完成没有出错Sahi的DLoadRunner会提示成功
查看log与报告
在Sahi的安装目录下
<?xml version="1.0" encoding="ISO-8859-1"?> <!-- /** * Copyright Tyto Software Pvt. Ltd. */ --> <project name="demo" default="dloadrun"> <taskdef name="sahiload" classname="in.co.sahi.ant.DAntLoadRunner" classpath="lib/ant-sahi.jar"/> <target name="dloadrun"> <sahiload scriptsPathMaster="scripts" subject="demo/load/subject.sah" baseurl="http://sahitest.com/demo/training/" sahihost="localhost" sahiport="9999" failureproperty="sahi.failed" haltonfailure="false" browsertype="chrome" noise="demo/load/noise.sah" noiseBrowserType="phantomjs" min="1" max="9" incrementBy="2" interval="5" subjectRepeatCount="3" ignorePattern=".*(svn|copied).*"> <node host="localhost" port="9999"/> <!-- <node host="machine2" port="9999"/> <node host="machine3" port="9999"/> --> </sahiload> <antcall target="failsahi"/> </target> <target name="failsahi" if="sahi.failed"> <fail message="Sahi tests failed!"/> </target> </project>
而后咱们经过命令行运行
ant -f dload.xml
没有安装好ant的也能够经过brew安装
$ brew install ant
在录制脚本的时候,不知何缘由,没法在经过https访问本地搭建的sso环境https://sso.hoau.com:8433/cas 同时应用程序客户端https://app1.hoau.com:8413/cas1 也没法正常访问。比较Sahi里面的浏览器和正常的浏览器证书的设置发现(Firefox-> Preferences -> Advanced -> Certificates -> View Certificates)Sahi里的代理浏览器缺乏证书,手工导入无果(没有研究如何操做)
因而尝试先用http录制,而后在PhantomJS后台经过--ignore-ssl-errors=yes模式尝试https
首先录制http://sso.hoau.com:8083/cas
_setValue(_textbox("username"), "test01"); _setValue(_password("password"), "psw01"); _click(_submit("登录")); _assertExists(_div("msg")); _assertEqual("Log In Successful You, test01, have successfully logged into the Central Authentication Service. When you are finished, for security reasons, please Log Out and Exit your web browser.", _getText(_div("msg")));
而后更改目标的地址的startURL,而且命令行下运行:
$ ./testrunner.sh my/cas_sso.sah https://sso.hoau.com:8433/cas/ phantomjs
测试成功。
获得相似脚本:
_setValue(_textbox("username"), "test01"); _setValue(_password("password"), "psw01"); _click(_submit("登录")); _assertExists(_link("test01"));
但若是重复playback会出错,由于浏览器会保持JSESSION的session cookie,记录下曾经登录的状态,没法重复playback。所以,咱们须要清除浏览器的cookie。
Sahi官网上介绍了如何清除浏览器cookie,但我尝试过发现此方法无效:
// Shows all cookies for www.yahoo.com domain. _navigateTo("http://www.yahoo.com/_s_/dyn/Cookies_showAll"); // Click the checkbox to choose all cookies. _check(_checkbox("_sahi_chooseAll")); // Delete the cookies. _click(_button("Delete")); // Shows all cookies again so that you can verify that all cookies have been deleted. _navigateTo("http://www.yahoo.com/_s_/dyn/Cookies_showAll");
多是浏览器版本
我本地的Chrome版本为:42.0.2311.152 (64-bit)
Firefox版本为:43.0.2
多是路径缘由Sahi清除cookie页面里的javascript脚本执行的目标路径有问题
"_sendToServer(...)"
这个问题尚未研究出根本缘由在哪,不过有方法能够绕过去。
我有另一片文章介绍过CAS SSO的时序图,而且分析过http的request和Response消息头消息体里面的内容,咱们能够经过将head Cookie置空达到清除cookie的目的,修改好之后的脚本以下:
_setHttpHeader("Cookie", ""); _navigateTo("https://app2.hoau.com:8423/cas2/"); _setValue(_textbox("username"), "test01"); _setValue(_password("password"), "psw01"); _click(_submit("登录"));
这样咱们就能重复playback,并且经测试发现数据库中也会生成大量ticket
以上工做完成后,咱们如今正式开始对CAS SSO的登录作压力测试。
我以官网的样例为模板稍做修改:
<!-- /** * Copyright Tyto Software Pvt. Ltd. */ --> <project name="demo" default="dloadrun"> <taskdef name="sahiload" classname="in.co.sahi.ant.DAntLoadRunner" classpath="../lib/ant-sahi.jar"/> <target name="dloadrun"> <sahiload scriptsPathMaster="scripts" subject="my/load/subject.sah" baseurl="https://app2.hoau.com:8423/cas2/" sahihost="localhost" sahiport="9999" failureproperty="sahi.failed" haltonfailure="false" browsertype="chrome" noise="my/load/subject.sah" noiseBrowserType="phantomjs" min="1" max="999" incrementBy="20" interval="5" subjectRepeatCount="3" ignorePattern=".*(svn|copied).*"> <node host="localhost" port="9999"/> <!-- <node host="machine2" port="9999"/> <node host="machine3" port="9999"/> --> </sahiload> <antcall target="failsahi"/> </target> <target name="failsahi" if="sahi.failed"> <fail message="Sahi tests failed!"/> </target> </project>
咱们将baseurl指定为测试的目标地址,“subject”浏览器为Chrome,“noise”浏览器为PhantomJS,同时咱们将增长线程数的为20,线程从1增加至999最大,为了专一与登录的压力,我同时将subject和noise都指向了登录的脚本。有兴趣的童鞋能够尝试如下其余的方式,以及修改参数试试。
能够经过ant运行
$ ant -f dload-sso.xml
而后居然被我遇到了一个core dev承认,4.1.3版本的major
测试结果:
Github上的答复:
Sahi的压力测试可否支持部署多个分布式Sahi测试节点?
Sahi的压力测试除了Subject和Noise这种模式,是否还有须要其余支持,或者提供其余支持?
还未对Sahi作全面评估,网上评论说Sahi比较适合Smoke Web UI,不过单纯从我的角度就易上手程度来主管评价,Sahi相对于Selenium和JMeter较容易上手。