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

相关推荐
weixin_446122463 小时前
JAVA内存区域划分
java·开发语言·redis
TT哇3 小时前
JavaEE==网站开发
java·redis·java-ee
qq_392397126 小时前
Redis常用操作
数据库·redis·wpf
一只叫煤球的猫7 小时前
真实事故复盘:Redis分布式锁居然失效了?公司十年老程序员踩的坑
java·redis·后端
都叫我大帅哥12 小时前
🌊 Redis Stream深度探险:从秒杀系统到面试通关
java·redis
都叫我大帅哥12 小时前
Redis持久化全解析:从健忘症患者到记忆大师的逆袭
java·redis
野蛮人6号13 小时前
黑马点评系列问题之基础篇p7 06初识redis无法在虚拟机查到图形化界面存进去的键
redis·黑马点评
大春儿的试验田16 小时前
高并发收藏功能设计:Redis异步同步与定时补偿机制详解
java·数据库·redis·学习·缓存
hqxstudying16 小时前
Redis为什么是单线程
java·redis
Fireworkitte18 小时前
Redis 源码 tar 包安装 Redis 哨兵模式(Sentinel)
数据库·redis·sentinel