mysql压力测试与qps监控

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql压力测试与qps监控相关的知识,希望对你有一定的参考价值。

https://blog.csdn.net/csd753111111/article/details/100428512

脚本:

#cat mysql_qps.sh 

#!/bin/bash

MYSQL_CON="/usr/local/mysql/bin/mysql"

TIME="$1"

OLD_QUERY=`$MYSQL_CON -e "show global status like ‘questions‘;" | awk -F[‘ ‘,‘s‘] ‘NR==2{print $3}‘`

echo "$OLD_QUERY"

sleep "$TIME"

NEW_QUERY=`$MYSQL_CON -e "show global status like ‘questions‘;" | awk -F[‘ ‘,‘s‘] ‘NR==2{print $3}‘`

echo "$NEW_QUERY"

TIME_QUERY=`expr $NEW_QUERY - $OLD_QUERY`

QPS=`expr $TIME_QUERY / $TIME`

echo "$QPS"

 

0状态

show global status like ‘questions‘;

40927

 

测试代码:

package jdbctimezone;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.protobuf.InvalidProtocolBufferException;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import serial.MyBaseBean;
import serial.MyBaseProto;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 *
 * Created by joyce on 2019/10/24.
 */
@BenchmarkMode(Mode.Throughput)//基准测试类型
@OutputTimeUnit(TimeUnit.SECONDS)//基准测试结果的时间类型
@Threads(10)//测试线程数量(IO密集)
@State(Scope.Thread)//该状态为每个线程独享
public class YaliMysql {

    private static final String URL_NO_TIMEZONE="jdbc:mysql://127.0.0.1:53306/mytest?useUnicode=true&characterEncoding=utf-8&useSSL=false";
    private static final String USER="root";
    private static final String PASSWORD="memories";

    private static ThreadLocal<Connection> connectionThreadLocal = new ThreadLocal<>();

    public static void main(String[] args) throws Exception {

        if(true) {
            Options opt = new OptionsBuilder().include(YaliMysql.class.getSimpleName()).forks(1).warmupIterations(1)
                    .measurementIterations(3).build();

            new Runner(opt).run();
        }

    }

    @Benchmark
    public static void testMysql() {
        Connection connection = connectionThreadLocal.get();
        if(connection == null) {
            connection = getConnection();
            connectionThreadLocal.set(connection);
        }
        try {
            PreparedStatement pstmt = connection.prepareStatement("select * from mytest where id = 1");
            ResultSet rs = pstmt.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL_NO_TIMEZONE, USER, PASSWORD);
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

 

41秒:

# Warmup Iteration 1: 1308.201 ops/s
Iteration 1: 1657.662 ops/s
Iteration 2: 1439.036 ops/s
Iteration 3: 1732.052 ops/s


Result "jdbctimezone.YaliMysql.testMysql":
1609.583 ±(99.9%) 2778.689 ops/s [Average]
(min, avg, max) = (1439.036, 1609.583, 1732.052), stdev = 152.309
CI (99.9%): [≈ 0, 4388.272] (assumes normal distribution)


# Run complete. Total time: 00:00:41

 

101446-40927=60519

qps计算=60519/41=1476

以上是关于mysql压力测试与qps监控的主要内容,如果未能解决你的问题,请参考以下文章

Mysql 监控性能状态 QPS/TPS

mysql

压力测试过程中MySQL服务CPU占用率过高的问题排查思路

压力测试:系统吞吐量TPS(QPS)用户并发量性能测试概念和公式

MySQL写压力性能监控与调优

MySQL关键性能监控(QPS/TPS)