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

相关推荐
老汉忒cpp6 分钟前
Redis string类型&&hash类型
java·redis·mybatis
ljp_nan2 小时前
Redis --- 第一讲 --- 分布式简单介绍
数据库·redis·缓存
极客先躯10 小时前
高级java每日一道面试题-2024年9月30日-算法篇-LRU是什么?如何实现?
java·redis·lru·算法篇·缓存淘汰策略
kingapex111 小时前
压力测试指南-压力测试基础入门
自动化测试·压力测试
小小小小关同学11 小时前
【Redis】持久化机制--RDB和AOF
数据库·redis·mybatis
编程卡拉米12 小时前
redis的数据结构,内存处理,缓存问题
数据库·redis·缓存
胡耀超14 小时前
缓存是什么?缓存机制、Spring缓存管理、Redis数据一致性、缓存问题(缓存穿透、缓存雪崩、缓存击穿)及Redis与MySQL使用场景对比
redis·spring·缓存
wclass-zhengge14 小时前
Redis篇(缓存机制 - 多级缓存)(持续更新迭代)
数据库·redis·缓存
big_noob20 小时前
centos7安装Redis单机版
数据库·redis·缓存·redis安装·redis安装教程·redis安装步骤·centos安装redis
会洗碗的CV工程师1 天前
828华为云征文 | 利用FIO工具测试Flexus云服务器X实例存储性能
运维·服务器·功能测试·华为云·压力测试