Jmeter测试Dubbo接口

Jmeter测试Dubbo接口:创建Dubbo服务消费者工程,再使用Jmeter压测该工程

StressHelper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class StressHelper {
private static ClassPathXmlApplicationContext context;

static {
context = new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
}

public static <T> T getBean(String name) {
return (T)context.getBean(name);
}

public static <T> T getBean(Class<T> type) {
return context.getBean(type);
}

public static void close() {
context.close();
}

}

ExampleStress.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import com.alibaba.fastjson.JSON;
import cn.appblog.dubbo.stress.StressHelper;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

public class ExampleStress extends AbstractJavaSamplerClient {

private IExampleService exampleService;
private SimpleDateFormat dateFormat;

public void setupTest(JavaSamplerContext arg0){
exampleService = StressHelper.getBean("exampleService");
dateFormat = new SimpleDateFormat("yyyy-MM-dd");
}

@Override
public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
SampleResult sr = new SampleResult();
sr.setSampleLabel("pay_create");
sr.sampleStart();
try {
//getNewLogger().info("==========begin==========");
ExampleRequest request = new ExampleRequest();
request.setParamLong(javaSamplerContext.getLongParameter("paramLong"));
request.setParamString(javaSamplerContext.getParameter("paramString"));
request.setParamDate(dateFormat.parse(javaSamplerContext.getParameter("paramDate")));
request.setParamEnum(ExampleEnum.valueOf(javaSamplerContext.getParameter("paramEnum")));
request.setParamObject(JSON.parseObject(javaSamplerContext.getParameter("paramObject"), ExampleBean.class));
request.setParamList(JSON.parseArray(javaSamplerContext.getParameter("paramList"), ExampleBean.class));
request.setParamMap(JSON.parseObject(javaSamplerContext.getParameter("paramMap"), Map.class));

sr.setSamplerData(JSON.toJSONString(request));
ExampleResponse response = exampleService.method(request);
if (response != null && response.getCode() == 0) {
sr.setSuccessful(true);
}
sr.setResponseData(JSON.toJSONBytes(response));
//getNewLogger().info("=========end============");
//System.out.println(JSON.toJSONString(response));
} catch (Exception e) {
getNewLogger().error("", e);
} finally {
sr.sampleEnd();
}

return sr;
}

@Override
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("paramLong", "201806062010181234");
params.addArgument("paramString", "test");
params.addArgument("paramDate", "2018-06-06");
params.addArgument("paramEnum", "RMB");
params.addArgument("paramObject", "{\"id\":001, \"name\":\"Joe.Ye\", \"blog\":\"www.appblog.cn\"}");
params.addArgument("paramList", "[{\"id\":001, \"name\":\"Joe.Ye\", \"blog\":\"www.appblog.cn\"}]");
params.addArgument("paramMap", "{\"id\":001, \"name\":\"Joe.Ye\", \"blog\":\"www.appblog.cn\"}");
return params;
}

@Override
public void teardownTest(JavaSamplerContext context) {
//StressHelper.close();
}

}

dubbo-consumer.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<dubbo:application name="dubbo_stress_application" owner=""/>
<dubbo:registry address="zookeeper://192.168.1.10:2181?backup=192.168.1.11:2181,192.168.1.12:2181"/>
<dubbo:consumer check="false" timeout="120000"/>

<dubbo:reference id="exampleService" interface="cn.appblog.service.IExampleService" />

</beans>

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?xml version="1.0" encoding="UTF-8"?>

<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>cn.appblog.dubbo.stress</groupId>
<artifactId>stress</artifactId>
<version>1.0-SNAPSHOT</version>

<name>stress</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>4.3.7.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>cn.appblog.cloud</groupId>
<artifactId>example-interfaces</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

<!--<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>4.0</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</exclusion>
<exclusion>
<groupId>commons-math3</groupId>
<artifactId>commons-math3</artifactId>
</exclusion>
<exclusion>
<groupId>commons-pool2</groupId>
<artifactId>commons-pool2</artifactId>
</exclusion>
</exclusions>
</dependency>-->

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.6</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_java -->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>3.2</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</exclusion>
<exclusion>
<groupId>commons-math3</groupId>
<artifactId>commons-math3</artifactId>
</exclusion>
<exclusion>
<groupId>commons-pool2</groupId>
<artifactId>commons-pool2</artifactId>
</exclusion>
<exclusion>
<artifactId>ApacheJMeter_components</artifactId>
<groupId>org.apache.jmeter</groupId>
</exclusion>
</exclusions>
</dependency>

<!--spring依赖的jar包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.36</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

</dependencies>

<build>
<plugins>
<!--复制jar包插件,将使用到的jar包,复制到target/lib中-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-resource</id>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :