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

相关推荐
KK溜了溜了6 小时前
JAVA-springboot 整合Redis
java·spring boot·redis
爱上语文11 小时前
Redis基础(6):SpringDataRedis
数据库·redis·后端
Java初学者小白11 小时前
秋招Day14 - Redis - 应用
java·数据库·redis·缓存
奈斯ing12 小时前
【Redis篇】数据库架构演进中Redis缓存的技术必然性—高并发场景下穿透、击穿、雪崩的体系化解决方案
运维·redis·缓存·数据库架构
一眼万年0412 小时前
Redis Cluster模式
redis·微服务
deriva14 小时前
.netcore+ef+redis+rabbitmq+dotcap先同步后异步再同步的方法,亲测有效
redis·rabbitmq·.netcore
爱上语文15 小时前
Redis基础(5):Redis的Java客户端
java·开发语言·数据库·redis·后端
Java初学者小白18 小时前
秋招Day15 - Redis - 缓存设计
java·数据库·redis·缓存
都叫我大帅哥21 小时前
Redis GEO全解:从入门到精通,让你的应用“空间觉醒”
redis
都叫我大帅哥21 小时前
Redis 的 HyperLogLog:用 12KB 数清银河系星星的魔法计数器
redis