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。这个坑弄了好久。

相关推荐
卑微的Coder3 小时前
JMeter同步定时器 模拟多用户并发访问场景
java·jmeter·压力测试
乐言3613 小时前
如何解决Jmeter中的乱码问题?
jmeter
柯南二号3 小时前
MacOS 用brew 安装、配置、启动Redis
redis
星星点点洲6 小时前
【Redis】RedLock实现原理
redis·缓存
我来整一篇6 小时前
用Redis的List实现消息队列
数据库·redis·list
加什么瓦6 小时前
Redis——数据结构
数据库·redis·缓存
lybugproducer7 小时前
浅谈 Redis 数据类型
java·数据库·redis·后端·链表·缓存
青山是哪个青山7 小时前
Redis 常见数据类型
数据库·redis·bootstrap
杨不易呀7 小时前
Java面试全记录:Spring Cloud+Kafka+Redis实战解析
redis·spring cloud·微服务·kafka·高并发·java面试·面试技巧