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;
/**
* <p>
* 前端控制器
* </p>
*
* @author cch
* @since 2021-11-14
*/
@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");
//登录,生成UserTicket
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){
//http 登录url
URL url= new URL(urlString);
HttpURLConnection co = (HttpURLConnection)url.openConnection();
co.setRequestMethod("POST");
co.setDoOutput(true);
//http输出流
OutputStream out=co.getOutputStream();
String params="mobile="+user.getId()+"&password="+MD5Util.inputPassToFromPass("123456");
out.write(params.getBytes());
out.flush();
//http响应输入流 转为字节流
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());
//响应字节流转 RespBean,获取obj中的userTicket
ObjectMapper mapper = new ObjectMapper();
//System.out.println("response :"+response);
RespBean respBean=mapper.readValue(response,RespBean.class);
String userTicket = (String) respBean.getObj();
System.out.println("create userTicket :"+user.getId());
//写入config.txt 一行
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);
}
}