jmeter压力测试
1.运行
windwos
运行jmeter.bat
2.配置 jmeter.properties
sampleresult.default.encoding=UTF-8
3配置测试计划
3.1 添加 线程 线程组【线程组】
线程数 1000 ramp-up秒 0 循环次数 1
3.2 线程组 添加配置元件
3.2.1 【http请求默认值】
协议 HTTP 服务器 localhost 端口 8080
3.2.2 【CSV数据文件设置】
D:/java/springboot/config.txt userId,userTicket ,
3.2.3 【httpCookie管理器】
userTicket ${userTicket} localhost /
3.3 线程组 取样器 HTTP请求
3.3.1 【商品列表】
GET /goods/tolist
3.3.2 【用户信息】
GET /user/info
3.3.3 【秒杀】
POST /seckill/doSeckill goodsId:goodsId
3.4 线程组-添加监听器 【查看结果树】
3.5 线程组-添加监听器【聚合报告】
吞吐量
3.5 线程组-添加监听器【用表格察看结果】
4 centos Jmeter 压力测试
4.1 安装jdk1.8
4.2 安装mysql8.0
4.3 idea maven 打包项目运行jar包 project lifecycle-clean-package-XXX.jar
java -jar miaosha-0.0.1-SNAPSHOT.jar
4.4 windows保存测试计划 first.jmx 上传到服务器
上传centos config.txt miaosha1.jmx
4.5 运行测试计划
./jmeter.sh -n -t ~/first.jmx -l result.jtl ./jmeter.sh -n -t ~/miaosha1.jmx -l result.jtl
4.6 下载result.jtl 聚合报告
4.7 windows 聚合报告 浏览
5测试准备
5.1 UserController.java 用户信息
package com.example.miaosha.controller;
import com.example.miaosha.pojo.User;
import com.example.miaosha.rabbitmq.MQSender;
import com.example.miaosha.vo.RespBean;
import com.example.miaosha.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private MQSender mqSender;
@RequestMapping("/info")
@ResponseBody
public RespBean info(User user){
return RespBean.success(user);
}
}
5.2 UserUtil .createUser 准备5000测试用户,userticket写入数据库和redis
package com.example.miaosha.utils;
import com.example.miaosha.pojo.User;
import com.example.miaosha.vo.RespBean;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class UserUtil {
private static void createUser(int count) throws Exception {
List<User> users =new ArrayList<>(count);
for(int i=0;i<count;i++){
User user =new User();
user.setId(13800000000L+i);
user.setNickname("user"+i);
user.setSlat("1a2b3c4d");
user.setPassword(MD5Util.inputPassToToDBpass("123456",user.getSlat()));
user.setRegisterDate(new Date());
users.add(user);
}
System.out.println("create user");
Connection con=getConn();
String sql="insert into t_user(nickname,slat,password,id,register_date) values(?,?,?,?,?)";
PreparedStatement preparedStatement = con.prepareStatement(sql);
for (User user: users){
preparedStatement.setString(1, user.getNickname());
preparedStatement.setString(2, user.getSlat());
preparedStatement.setString(3, user.getPassword());
preparedStatement.setLong(4, user.getId());
preparedStatement.setTimestamp(5, new Timestamp(user.getRegisterDate().getTime()));
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
preparedStatement.clearParameters();
con.close();
System.out.println("insert to db");
String urlString="http://localhost:8080/login/doLogin";
File file=new File("D:\\java\\springboot\\config.txt");
if(file.exists()){
file.delete();
}
RandomAccessFile raf= new RandomAccessFile(file,"rw");
raf.seek(0);
for (User user: users){
URL url= new URL(urlString);
HttpURLConnection co = (HttpURLConnection)url.openConnection();
co.setRequestMethod("POST");
co.setDoOutput(true);
OutputStream out=co.getOutputStream();
String params="mobile="+user.getId()+"&password="+MD5Util.inputPassToFromPass("123456");
out.write(params.getBytes());
out.flush();
InputStream inputStream = co.getInputStream();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buff=new byte[1024];
int len=0;
while ((len=inputStream.read(buff))>=0){
bout.write(buff,0,len);
}
inputStream.close();
bout.close();
String response = new String(bout.toByteArray());
ObjectMapper mapper = new ObjectMapper();
RespBean respBean=mapper.readValue(response,RespBean.class);
String userTicket = (String) respBean.getObj();
System.out.println("create userTicket :"+user.getId());
String row=user.getId()+","+userTicket;
raf.seek(raf.length());
raf.write(row.getBytes());
raf.write("\r\n".getBytes());
System.out.println("write to file :"+user.getId());
}
raf.close();
System.out.println("over");
}
private static Connection getConn() throws Exception {
String url="jdbc:mysql://localhost:3306/miaosha?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
String username="root";
String password="123456";
String driver="com.mysql.cj.jdbc.Driver";
Class.forName(driver);
return DriverManager.getConnection(url,username,password);
}
public static void main(String[] args) throws Exception {
createUser(5000);
}
}
5.3 config.txt 用户信息
13800000000,0ccffd352d594ffba10be4a3032b145c
13800000001,d3ffc6d7779849948b80c4b43642facf
13800000002,6f275a59ecf94105b5dabc941b907a90
13800000003,f251fcf8c9dc4bb08a3da4cdd54f0b5b
13800000004,e48dc1b9b5e44c4889eddcfc1f5857d4
13800000005,72033a6c558b4bd5a1a9b6f311d3ad28
13800000006,d37dcb83bb2d4993b8fe6d5ab6006b72
|