JMeter压力测试记录

最近在学习redis解决高并发下导致数据库数据不准确的问题,使用到了一些工具,包括Jmeter,Redis-desktop-manager.。Jmeter用于模拟高并发情景,Redis-desktop-manager是redis数据库的GUI界面。

一、单元测试生成测试数据

1)插入2000用户

java 复制代码
package com.xwl.diners.service;

import com.xwl.commons.model.dto.DinersDTO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class DinersServiceTest {
    @Autowired
    private DinersService dinersService;
    /**
     * 生成用户
     */
    @Test
    public void saveUser() {
        for (int i = 0; i < 2000; i++) {
            DinersDTO dinersDTO = new DinersDTO();
            dinersDTO.setUsername("test" + i);
            dinersDTO.setPassword("123");
            dinersDTO.setIsValid(true);
            dinersService.saveUser(dinersDTO, "/user/register");
        }
    }
}

2)使用MockMvc生成2000个token,保存至文件

java 复制代码
package com.xwl.oauth2.server.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xwl.commons.model.domain.ResultInfo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Base64Utils;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

@SpringBootTest
@Transactional
@AutoConfigureMockMvc
public class OAuthControllerTest {
    @Resource
    private MockMvc mockMvc;

    @Test
    public void postAccessToken() throws Exception {
//        mockMvc = MockMvcBuilders.standaloneSetup(new OAuthController()).build();
        StringBuffer tokens = new StringBuffer();
        String authorization = Base64Utils.encodeToString("appId:123456".getBytes(StandardCharsets.UTF_8));
        for (int i = 0; i < 2000; i++) {
            MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders
                            .post("/oauth/token")
                            .header("Authorization", "Basic " + authorization)
                            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                            .param("username", "test" + i)
                            .param("password", "123")
                            .param("grant_type", "password")
                            .param("scope", "api")
                    )
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andReturn();
            String contentAsString = mvcResult.getResponse().getContentAsString();
            ResultInfo resultInfo = JSONUtil.toBean(contentAsString, ResultInfo.class);
            JSONObject result =(JSONObject) resultInfo.getData();
            String token = result.getStr("accessToken");
            tokens.append(token).append("\r\n");
        }
        File file = new File("tokens.txt");
        if(file.exists()){
            file.delete();
        }
        file.createNewFile();
        FileOutputStream outputStream = new FileOutputStream(file);
        outputStream.write(tokens.toString().getBytes(StandardCharsets.UTF_8));
        outputStream.close();
    }
}

二、Jmeter压力测试配置

下载官网Apache JMeter - Apache JMeter™

上面已经生成了2000个有效token,现在我们配置一个5000并发2000用户的压力测试,需要读取上一步生成的文件,于请求头中携带。

1)在jmeter中添加一个线程组,设置5000并发。

2)添加CSV数据文件设置并设置变量名为token

3)添加信息头管理器,将上一步的token填写进去

4)添加HTTP请求

现在就可以运行压力测试了

三、Redis-desktop-manager配置

这个我在官网没下载到Linux版本,于是下载了一个Windows版本

链接: https://pan.baidu.com/s/1mlX_6DZNw1RFjeSYMeNmzg 密码: v4kq

这个需要运行在Windows虚拟机上,测试环境下已将redis.conf的bind改为0.0.0.0,但在连接本机redis过程中总是失败,最后发现是需要将redis.conf中的protected-mode设置为no。这个坑弄了好久。

相关推荐
咖啡啡不加糖1 小时前
Redis大key产生、排查与优化实践
java·数据库·redis·后端·缓存
MickeyCV2 小时前
使用Docker部署MySQL&Redis容器与常见命令
redis·mysql·docker·容器·wsl·镜像
肥仔哥哥19302 小时前
springCloud2025+springBoot3.5.0+Nacos集成redis从nacos拉配置起服务
redis·缓存·最新boot3集成
呼拉拉呼拉5 小时前
Redis故障转移
数据库·redis·缓存·高可用架构
什么都想学的阿超5 小时前
【Redis系列 04】Redis高可用架构实战:主从复制与哨兵模式从零到生产
数据库·redis·架构
1.01^10006 小时前
[5-02-04].第01节:Jmeter环境搭建:
jmeter
我命由我123457 小时前
Spring Boot 项目集成 Redis 问题:RedisTemplate 多余空格问题
java·开发语言·spring boot·redis·后端·java-ee·intellij-idea
篱笆院的狗8 小时前
如何使用 Redis 快速实现布隆过滤器?
数据库·redis·缓存
小鸡脚来咯9 小时前
redis分片集群架构
数据库·redis·架构
什么都想学的阿超9 小时前
【Redis系列 03】掌握Redis编程艺术:事务、管道与Lua脚本完全指南
redis·junit·lua