ngrinder项目-本地调试遇到的坑

发布于:2024-05-06 ⋅ 阅读:(31) ⋅ 点赞:(0)

前提-maven mirrors配置

 <mirrors>
 <!--阿里公有仓库-->
	<mirror>
		<id>nexus-aliyun</id>
		<mirrorOf>central</mirrorOf>
		<name>Nexus aliyun</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public</url>
	</mirror>
</mirrors>

问题1

描述:ngrinder-groovy3.4.jar包下载失败

ngrinder3.4创建的maven项目-ngrinder-groovy3.4依赖一直下载失败-解决办法

通过运行脚本,报错信息如下:

Cannot resolve org.ngrinder:ngrinder-groovy:pom:3.4 failed to transfer from https://github.com/naver/ngrinder/raw/ngrinder.maven.repo/releases during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of ngrinder.maven.repo has elapsed or updates are forced. Original error: Could not transfer artifact org.ngrinder:ngrinder-runtime:pom:3.4.4 from/to ngrinder.maven.repo (https://github.com/naver/ngrinder/raw/ngrinder.maven.repo/releases): transfer failed for https://github.com/naver/ngrinder/raw/ngrinder.maven.repo/releases/org/ngrinder/ngrinder-groovy/3.4/ngrinder-groovy-3.4.pom

解决:下载ngrinder-groovy-3.4.jar

1、通过运行脚本,报错信息可以看到,ngrinder-groovy-3.4.jar对应的ngrinder的github远程仓库地址为 https://github.com/naver/ngrinder/raw/ngrinder.maven.repo/releases

下载ngrinder-groovy-3.4.jar,如下图:

https://github.com/naver/ngrinder/tree/ngrinder.maven.repo/releases/org/ngrinder/ngrinder-groovy/3.4

点击下载 ngrinder-groovy-3.4.jar。

2、存储到项目对应的maven目录下,和线上仓库相同路径。

并且放入maven仓库对应的目录下(maven配置参考地址),如下:

C:\D\soft\Maven_Repo\org\ngrinder\ngrinder-groovy\3.4

刷新maven依赖,后依赖问题解决。

刷新maven依赖,后依赖问题解决。

###############

问题2

描述:本地调试脚本时,提示错误,如下

java.lang.RuntimeException: Please add -javaagent:C:\D\soft\Maven_Repo\net\sf\grinder\grinder-dcr-agent\3.9.1\grinder-dcr-agent-3.9.1.jar in 'Run As JUnit' vm argument.

解决:配置grinder-dcr-agent-3.9.1.jar

这个错误信息指示你需要在运行JUnit测试时,向Java虚拟机(JVM)添加一个Java代理(javaagent)参数。这个参数是为了加载grinder-dcr-agent-3.9.1.jar,它是Grinder分布式测试框架的一部分,用于监控和管理性能测试。

要解决这个问题,在你的IDEA中按照以下步骤操作:

IntelliJ IDEA:

  1. 打开运行/调试配置:在顶部菜单中选择“Run” -> “Edit Configurations...”。

  2. 选择或创建JUnit配置:在弹出的窗口中,找到你的测试类对应的JUnit配置,或者点击左上角的“+”号添加一个新的JUnit配置。

  3. 配置VM选项:在配置界面中,找到“VM options”字段。

  4. 添加代理参数:在此字段中,输入与Eclipse中相同的Java代理参数:-ea -javaagent:C:\D\soft\Maven_Repo\net\sf\grinder\grinder-dcr-agent\3.9.1\grinder-dcr-agent-3.9.1.jar,如图

  5. 检查下载的maven仓库中,C:\D\soft\Maven_Repo\net\sf\grinder\grinder-dcr-agent\3.9.1\grinder-dcr-agent-3.9.1.jar是正确的,并且该JAR文件存在于指定位置。如果路径有误,你需要根据实际情况调整,确保grinder-dcr-agent-3.9.1.jar文件正常。如果下载的文件不全,则删除不完整的文件后,重新下载。

  6. 应用并运行:点击“OK”应用设置,然后你可以直接运行这个配置来执行测试。运行正常。如图注意:本地调试ngrinder时,不需要启动agent客户端。

问题3:ngrinder-runtime下载失败

添加pom.xml 中添加 maven如下
        <dependency>
			<groupId>org.ngrinder</groupId>
			<artifactId>ngrinder-runtime</artifactId>
			<version>3.4.4</version>
		</dependency>
刷新maven时,idea项目提示如下错误

Cannot resolve org.ngrinder:ngrinder-runtime:pom:3.4.4 failed to transfer from https://github.com/naver/ngrinder/raw/ngrinder.maven.repo/releases during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of ngrinder.maven.repo has elapsed or updates are forced. Original error: Could not transfer artifact org.ngrinder:ngrinder-runtime:pom:3.4.4 from/to ngrinder.maven.repo (https://github.com/naver/ngrinder/raw/ngrinder.maven.repo/releases): transfer failed for https://github.com/naver/ngrinder/raw/ngrinder.maven.repo/releases/org/ngrinder/ngrinder-runtime/3.4.4/ngrinder-runtime-3.4.4.pom

错误提示表明

1、没有下载成功的依赖包名为:ngrinder-runtime-3.4.4.jar

2、通过远端仓库路径,对应本地仓库中没有下载成功的文件:maven本地仓库地址 + org/ngrinder/ngrinder-runtime/3.4.4/目录下,如图

3、也可以在本地maven仓库地址下搜索关键字:lastUpdated,也能查到上图未下载完成的文件。

4、可以通过maven下载失败的提示,从github,ngrinder远程仓库下载,远程仓库地址为:

https://github.com/naver/ngrinder/raw/ngrinder.maven.repo/releases

从远端参考下载jar包

下载ngrinder-runtime-3.4.4.jar文件后,放入对应的 本地maven本地仓库路径,刷新maven依赖。问题解决。

注意点

注意1:ngrinder-groovy依赖包

ngrinder的基础依赖,必须下载正常,否则几乎脚本的所有内容都报红。

        <dependency>
			<groupId>org.ngrinder</groupId>
			<artifactId>ngrinder-groovy</artifactId>
			<version>${ngrinder.version}</version>
			<scope>provided</scope>
		</dependency>

读文件&发起http请求,只需要这一个依赖就足够了。

 注意2:读文件操作,和设置headers参数设置
@BeforeProcess
	public static void beforeProcess() {

		// 设置header参数
		List<NVPair> headerList = new ArrayList<NVPair>()
		headerList.add(new NVPair("Content-Type", "application/json"));
		headers = headerList.toArray()

		//获取数据,文件:/resources/phone.txt
		String path = this.getClassLoader().getResource("phone.txt").getPath()
		phoneNumList = new File(path).readLines(); #三行内容为 [111, 222, 333]

    }

注意3:json格式处理

如果接口返回的时json格式,压测脚本应导入工具类RecorderUtils

使用RecorderUtils类时,需要maven导入ngrinder-runtime-3.4.4.jar包。

        <dependency>
			<groupId>org.ngrinder</groupId>
			<artifactId>ngrinder-runtime</artifactId>
			<version>3.4.4</version>
		</dependency>

压测脚本: 

import org.ngrinder.recorder.RecorderUtils
import org.hamcrest.Matchers

#使用工具类的RecorderUtils.parseRequestToJson 方法解析json数据,使用如下:
def json_object = RecorderUtils.parseRequestToJson(result.getText()) println(json_object); println(json_object['msg']);

#assert方式如下 assertThat(json_object.get('msg'), Matchers.is("success")) assertThat(1,Matchers.is(1))
assertEquals(1,1)

注意4:生成随机字符串

需要maven导入依赖:

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.12.0</version> <!-- 可以指定为最新的稳定版本 -->
</dependency>

压测脚本编写导入RandomStringUtils

  1. 数字:RandomStringUtils.randomNumeric(length);

  2. 字母:RandomStringUtils.randomAlphabetic(length);

  3. 字母加数字:RandomStringUtils.randomAlphanumeric(length);

  4. 所有ASCCII字符:RandomStringUtils.randomAscii(length);

  5. 自定义混合字符:RandomStringUtils.randomAscii(length, string);

压测脚本:

import org.apache.commons.lang.RandomStringUtils
RandomStringUtils.randomNumeric(3)

pom.xml文件 参考

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>ngrinder</groupId>
	<artifactId>www.baidu.com</artifactId>
	<version>0.0.1</version>

	<properties>
		<ngrinder.version>3.4</ngrinder.version>
		<maven.compiler.source>1.7</maven.compiler.source>
		<maven.compiler.target>1.7</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<repositories>
		<!--

    <repository>
        <id>ngrinder.maven.repo</id>
        <url>https://github.com/naver/ngrinder/raw/ngrinder.maven.repo/releases</url>
    </repository>
    -->
	</repositories>
	<dependencies>
		<dependency>
			<groupId>org.ngrinder</groupId>
			<artifactId>ngrinder-groovy</artifactId>
			<version>${ngrinder.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>net.sf.grinder</groupId>
			<artifactId>grinder-core</artifactId>
			<version>3.9.1</version>
		</dependency>
		<dependency>
			<groupId>org.ngrinder</groupId>
			<artifactId>grinder</artifactId>
			<version>3.9.1-patch</version>
		</dependency>
		<dependency>
			<groupId>net.sf.grinder</groupId>
			<artifactId>grinder-http</artifactId>
			<version>3.9.1</version>
		</dependency>
		<dependency>
			<groupId>net.sf.grinder</groupId>
			<artifactId>grinder-httpclient</artifactId>
			<version>3.9.1</version>

		</dependency>
		<dependency>
			<groupId>net.sf.grinder</groupId>
			<artifactId>grinder-dcr-agent</artifactId>
			<version>3.9.1</version>
		</dependency>
		<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>3.1</version>
		</dependency>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<version>1.3.168</version>
		</dependency>

		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>

		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>

		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2.2</version>
		</dependency>

		<!-- Json conversion -->
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.2</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit-dep</artifactId>
			<version>4.9</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.22</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.22</version>
		</dependency>

		<!-- Commons -->
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.0.1</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.9</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<version>2.9</version>
				<configuration>
					<additionalProjectnatures>
						<projectnature>
							org.eclipse.jdt.groovy.core.groovyNature
						</projectnature>
						<projectnature>
							org.eclipse.m2e.core.maven2Nature
						</projectnature>
					</additionalProjectnatures>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>


参考:

https://www.cnblogs.com/xiaowei89426/p/9365211.html

nGrinder介绍、编写脚本与执行(完整版)_ngrinder 压测脚本-CSDN博客