1.nGrinder 环境准备主要包含(JDK1.8.152 以上版本 和nGrinder安装包)已经有一键部署安装包具体下载地址如下:链接: https://pan.baidu.com/s/1Ip3kvju2nia1jXvtBDlTeA 提取码: jxxv ,或者扫二维码下载 “nGrinderServer.tar.gz”
执行 tar -xvzf nGrinderServer.tar.gz && cd nGrinderServer && sh ./Autoinstall.sh
安装完成后 通过执行 ps -ef|grep "tomcat" 确认nGrinder 服务是否安装成功;如下图
执行ss -tnlp |grep 80 如下图-安装成功
备注:1.一键安装包会试图安装Agent,因目前在本次Demo环境会安装失败,请安装下面步骤手动安装(非root用户,通过sudo su 获取roo't 权限;
2.默认Tomcat端口为80端口,如果冲突,Tomcat应该会启动失败,通过/nGrinderServer/apache-tomcat-9.0.36/conf/server.xml 配置文件修改为可用端口,如下图所示
端口修改后,执行 nGrinderServer/nGrinderStart.sh脚本重新启动nGrinder,启动成功后检查方法同上面
如: http://jenkins部署机器IP或域名(如果端口不是80,请带上指定端口)
出现如下页面,输入用户名和密码,默认为admin/admin
登录成功后
a.修改 /home/ghuser/nGrinderServer/ngrinder-agent/__agent.conf 中的agent.controller_host参数,如本机域名地址:”“krbumatp.chinanorth2.cloudapp.chinacloudapi.cn” 如下图所示,修改成功后,保存并退出;
import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.script.GTest
import net.grinder.script.Grinder
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.ngrinder.http.HTTPRequest
import org.ngrinder.http.HTTPRequestControl
import org.ngrinder.http.HTTPResponse
import org.ngrinder.http.cookie.Cookie
import org.ngrinder.http.cookie.CookieManager
/**
* A simple example using the HTTP plugin that shows the retrieval of a single page via HTTP.
*
* This script is automatically generated by ngrinder.
*
* @author admin
*/
@RunWith(GrinderRunner)
class TestRunner {
public static GTest test
public static HTTPRequest request
public static Map<String, String> headers = [:]
public static List<Cookie> cookies = []
@BeforeProcess
public static void beforeProcess() {
HTTPRequestControl.setConnectionTimeout(300000)
test = new GTest(1, "krbumatp1060.chinanorth2.cloudapp.chinacloudapi.cn")
request = new HTTPRequest()
// Set header data
headers.put("Content-Type", "application/json")
grinder.logger.info("before process.")
}
@BeforeThread
public void beforeThread() {
test.record(this, "test")
grinder.statistics.delayReports = true
grinder.logger.info("before thread.")
}
@Before
public void before() {
request.setHeaders(headers)
CookieManager.addCookies(cookies)
grinder.logger.info("before. init headers and cookies")
}
@Test
public void test() {
//接口报文参数定义可以根据测试需求进行参数化
def body = "{\"name\":\"Test云智慧Devops${grinder.runNumber}\",\"description\":\"湘菜-鲁菜-粤菜${grinder.runNumber}\"}"
HTTPResponse response = request.POST("http://krbumatp1060.chinanorth2.cloudapp.chinacloudapi.cn:5001/api/foodcategory", body.getBytes())
if (response.statusCode == 301 || response.statusCode == 302) {
grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", response.statusCode)
} else {
assertThat(response.statusCode, is(200))
}
}
}