1团赏、Jmeter簡介
Apache JMeter是Apache組織開發(fā)的基于Java的壓力測試工具松捉。用于對軟件做壓力測試,它最初被設(shè)計(jì)用于Web應(yīng)用測試馆里,但后來擴(kuò)展到其他測試領(lǐng)域隘世。 它可以用于測試靜態(tài)和動態(tài)資源可柿,例如靜態(tài)文件、Java 小服務(wù)程序丙者、CGI 腳本复斥、Java 對象、數(shù)據(jù)庫械媒、FTP 服務(wù)器目锭, 等等。JMeter 可以用于對服務(wù)器纷捞、網(wǎng)絡(luò)或?qū)ο竽M巨大的負(fù)載痢虹,來自不同壓力類別下測試它們的強(qiáng)度和分析整體性能。另外主儡,JMeter能夠?qū)?yīng)用程序做功能/回歸測試奖唯,通過創(chuàng)建帶有斷言的腳本來驗(yàn)證你的程序返回了你期望的結(jié)果。為了最大限度的靈活性糜值,JMeter允許使用正則表達(dá)式創(chuàng)建斷言丰捷。
Apache jmeter 可以用于對靜態(tài)的和動態(tài)的資源(文件,Servlet寂汇,Perl腳本病往,java 對象,數(shù)據(jù)庫和查詢骄瓣,FTP服務(wù)器等等)的性能進(jìn)行測試停巷。它可以用于對服務(wù)器、網(wǎng)絡(luò)或?qū)ο竽M繁重的負(fù)載來測試它們的強(qiáng)度或分析不同壓力類型下的整體性能榕栏。你可以使用它做性能的圖形分析或在大并發(fā)負(fù)載測試你的服務(wù)器/腳本/對象叠穆。
2、我們?yōu)槭裁词褂肑meter
開源免費(fèi)臼膏,基于Java編寫硼被,可集成到其他系統(tǒng)可拓展各個功能插件
支持接口測試,壓力測試等多種功能渗磅,支持錄制回放嚷硫,入門簡單
相較于自己編寫框架活其他開源工具,有較為完善的UI界面始鱼,便于接口調(diào)試
多平臺支持仔掸,可在Linux,Windows医清,Mac上運(yùn)行
3.1起暮、創(chuàng)建項(xiàng)目
pom.xml
<?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.memedai</groupId>
<artifactId>me-jmeter-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- jmeter 必須1-->
<!-- <dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>2.1.3</version>
</dependency>-->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>cn.memedai</groupId>
<artifactId>me-notification-sms-facade</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.memedai.framework</groupId>
<artifactId>me-fm-dubbo-ext</artifactId>
<version>2.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.12</version>
</dependency>
<!-- spring 必須4-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<!-- dubbo 必須5-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1mm</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<!-- https://issues.apache.org/jira/browse/ZOOKEEPER-1371 zookeeper Remove
dependency on log4j in the source code. slf4j-log4j12會與log4j-over-slf4j循環(huán)依賴導(dǎo)致沖突 -->
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.fxc.rpc.impl.member.MemberProvider</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
注意:dubbo和facade請根據(jù)自己公司使用的進(jìn)行更換 不然肯定會報(bào)錯的
package cn.memedai.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author hongwang.zhang
* @version: 1.0
* @date 2018/7/59:39
* @see
**/
public class DubboInit {
private static DubboInit init = null;
private DubboInit(){}
private static ApplicationContext context;
public synchronized static DubboInit getInstance(){
if(init == null){
init = new DubboInit();
}
return init;
}
public DubboInit initApplicationContext(){
context = new ClassPathXmlApplicationContext("classpath:/applicationContext-dubbo.xml");
if(context==null)
{
throw new IllegalArgumentException("Load dubbo-client.xml fail");
}
return this;
}
public Object getBean(String beanName) {
return context.getBean(beanName);
}
public Object getBean(Class beanName) {
return context.getBean(beanName);
}
}
JMeter java Sampler介紹
返回類型 | 方法 |
---|---|
SampleResult | getDefaultParameters() 用于獲取界面的參數(shù) |
SampleResult | runTest(JavaSamplerContext context) 類似于LR的Action,result.sampleStart()一個事務(wù)開始会烙,result.sampleEnd()一個事務(wù) 結(jié)束 |
void | setupTest(JavaSamplerContext context) 初始化方法负懦,類似于LR的init和Junit中的setUp() |
void | teardownTest(JavaSamplerContext context) 類似于LR的end和Junit中的tearDown() |
package cn.memedai.jmeter.notiectionsms.notionficationsms;
import cn.memedai.menotification.facade.sms.business.MeSmsBusiness;
import cn.memedai.util.DubboInit;
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;
import java.util.Map;
/**
* @author hongwang.zhang
* @version: 1.0
* @date 2018/7/510:10
* @see
**/
public class MeSmsBusinessJmeter extends AbstractJavaSamplerClient {
MeSmsBusiness meSmsBusiness;
private long start = 0;//記錄測試開始時(shí)間筒捺;
private long end = 0;//記錄測試結(jié)束時(shí)間;
private String phoneNo;
private String ext;
/**
* 初始化操作
*
* @param context
*/
@Override
public void setupTest(JavaSamplerContext context) {
DubboInit instance = DubboInit.getInstance().initApplicationContext();
meSmsBusiness = (MeSmsBusiness) instance.getBean("meSmsBusiness");
}
// 這個方法是用來自定義java方法入?yún)⒌摹? // params.addArgument("num1","");表示入?yún)⒚纸衝um1纸厉,默認(rèn)值為空系吭。
// 設(shè)置可用參數(shù)及的默認(rèn)值;
@Override
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("phoneNo", phoneNo);//未設(shè)默認(rèn)值
params.addArgument("ext", ext);
return params;
}
public SampleResult runTest(JavaSamplerContext agr0) {
start = System.currentTimeMillis();
SampleResult result = new SampleResult();
phoneNo = agr0.getParameter("phoneNo");
ext = agr0.getParameter("ext");
Map<String, Object> stringObjectMap = null;
try {
stringObjectMap = meSmsBusiness.sendVerificationCode(phoneNo, ext);
result.setSuccessful(true);
result.setResponseData(stringObjectMap.toString(), null);
result.setDataType(SampleResult.TEXT);
} catch (Exception e) {
getLogger().error("LqueryTakeRegNo response error : " + e.getMessage());
result.setSuccessful(false);
}
end = System.currentTimeMillis();
return result;
}
@Override
public void teardownTest(JavaSamplerContext arg0) {
end = System.currentTimeMillis();
getLogger().info(" cost time: " + (end - start) + "ms");
}
}
然后打成jar包 放在D:\tool\apache-jmeter-4.0\lib\ext 目錄下
注意最好使用和maven版本一樣的 客戶端
然后打開jmeter
選擇需要測試的類 填寫參數(shù)
點(diǎn)擊運(yùn)行