MySQL性能测试之select&update【FunTester框架】

2021-12-13 15:47:28 浏览数 (1)

前文提到了MySQL性能测试之insert&delete【FunTester框架】,今天来分享一下FunTester框架MySQL性能测试对于selectupdate语句进行性能测试。

准备工作这里就不细说了,有兴趣可以翻翻上面的文章链接。

select

这个语句应该是最常用的,而且优化的可能性比较大,各类添加索引的方式。随着数据量的增长还会涉及到分库分表等等。这里我简单演示一个最简单的select语句,配合上对于字段age的可视化。

SQL语句准备:"SELECT * FROM funtesters WHERE age = ${getRandomInt(100)};"

这里age参数用了100内的随机参数,如果大家在做业务测试的时候可以使用更加灵活的方式进行参数化。

测试脚本

代码语言:javascript复制
package com.funtest.mysqlt

import com.funtester.base.constaint.FixedThread
import com.funtester.db.mysql.SqlBase
import com.funtester.frame.execute.Concurrent
import com.funtester.httpclient.FunLibrary

/**
 * MySQL insert语句实践
 */
class MysqlSelect extends SqlBase {

    static final String url = "jdbc:mysql://localhost:3306/funtester?useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true&useSSL=false"

    static final int thread = 10

    static final int times = 100

    public static void main(String[] args) {
        RUNUP_TIME = 0
        def task = []
        thread.times {
            task << new FunTester()
        }
        new Concurrent(task, "FunTester框架测试MySQL").start()
        FunLibrary.testOver()
    }

    private static class FunTester extends FixedThread {

        def connection = getConnection(url, "root", "root123456")
        def statement = getStatement(connection)

        FunTester() {
            super(null, times, true)
        }

        @Override
        protected void doing() throws Exception {
            statement.execute("SELECT * FROM funtesters WHERE age = ${getRandomInt(100)};")
        }

        @Override
        protected void after() {
            super.after()
            close(connection, statement)
        }

        @Override
        FixedThread clone() {
            return new FunTester(limit)
        }
    }
}

测试结果

代码语言:javascript复制
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
>  {
>  ① . "rt":115,
>  ① . "failRate":0.0,
>  ① . "threads":10,
>  ① . "deviation":"0.44%",
>  ① . "errorRate":0.0,
>  ① . "executeTotal":4987,
>  ① . "qps2":86.57234615050777,
>  ① . "total":4987,
>  ① . "qps":86.95652173913044,
>  ① . "startTime":"2021-11-26 11:44:40",
>  ① . "endTime":"2021-11-26 11:45:38",
>  ① . "mark":"FunTester框架测试MySQL261144",
>  ① . "table":"eJzt0T0KwjAUB/Bd6B3eASoouNgDOOngxwUKDVgwURoFHf0WnM0oeAInPVCG4jEMWBUUGjEtEXnhQbs0///v1SmA9tRGrEP4kETxYRXvz/FpeznuGpN2sw7lEgy7EfED/S1OwUnPahE 6DNOoBNS4sG4yEkU j1gI rCpEhJEPpMl6HvQUMGt7u8ahUod6k/9sqVknrVf6xXmB4pNmo knwfMVeTe5IUSzX3mPzSpFioecnJOFWKqZrnI1lgAlyrSc03bSPFTM3zkYC/izdt FhCWqcMyxi2T/9n2e8uN9Fj0z w4ox1/2d6F/5ADRSiEIX2a6AQhSi0XwOFKESh/RooRCEK7ddAIQpRaL8GCo2EVy9sTPU="
>  }
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~

不知道是不是因为我没有加索引的原因,QPS太低了。

update

update也是在工作中经常用的数据库操作,对于这个操作我曾经写过两篇文章来区分两种不同update的参数化差别,如下如何对单行多次update接口进行压测、如何对多行单次update接口进行压测,有兴趣可以读一读。

这里只演示一个比较简单的方案,通过ID锁定某一行,然后对name字段进行多次更新,这样可以基本避免update的值前后一致的情况。

测试脚本

代码语言:javascript复制

import com.funtester.base.constaint.FixedThread
import com.funtester.db.mysql.SqlBase
import com.funtester.frame.execute.Concurrent
import com.funtester.httpclient.FunLibrary
import com.funtester.utils.StringUtil

/**
 * MySQL insert语句实践
 */
class MysqlUpdate extends SqlBase {

    static final String url = "jdbc:mysql://localhost:3306/funtester?useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true&useSSL=false"

    static final int thread = 10

    static final int times = 500

    public static void main(String[] args) {
        RUNUP_TIME = 0
        def task = []
        thread.times {
            task << new FunTester()
        }
        new Concurrent(task, "FunTester框架测试MySQL").start()
        FunLibrary.testOver()
    }

    private static class FunTester extends FixedThread {

        def connection = getConnection(url, "root", "root123456")

        def statement = getStatement(connection)


        def id

        FunTester() {
            super(null, times, true)
            id = getRandomRange(30, 10000)
        }

        @Override
        protected void doing() throws Exception {
            statement.execute("UPDATE funtesters SET name="${StringUtil.getString(20)}" WHERE id = $id;")
        }

        @Override
        protected void after() {
            super.after()
            close(connection, statement)
        }

        @Override
        FixedThread clone() {
            return new FunTester(limit)
        }
    }
}

这里我在FunTester类里面增加了一个属性id,用来标记修改行,这个范围是我从数据库中查到的,这里在工作中最好通过脚本实现,避免随机到不存在的行的情况。也可以将随机方式作用于SQL语句中,这样达到对多行进行修改的目的。

测试结果

代码语言:javascript复制
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
>  {
>  ① . "rt":118,
>  ① . "failRate":0.0,
>  ① . "threads":10,
>  ① . "deviation":"0.21%",
>  ① . "errorRate":0.0,
>  ① . "executeTotal":4989,
>  ① . "qps2":84.56505525798359,
>  ① . "total":4989,
>  ① . "qps":84.7457627118644,
>  ① . "startTime":"2021-11-26 12:03:10",
>  ① . "endTime":"2021-11-26 12:04:09",
>  ① . "mark":"FunTester框架测试MySQL261203",
>  ① . "table":"eJztk02qwjAURudC93AXoKAID wCHOlAnxsoGLBgojQKdfjUp4JjHQquwJEuyEFxGV7/KhQ12lqSwQ0f3E6a75yEWBlQrnJfNJjsMS9YT4LVLtjOD5tFdfBbq0AhD72Wx5ymehcrY73uqjPZ7QjJoOFyZoOfk8xznTaIPs/CIMdZ03WEqkPNwV0Bl73s0g9wmeWObxeKp0/1z2qLRGu/HGJwzDBv2cTpGGNw/GNuVd9t3C//MDhGmHvTFBMpfDhiUYSdr6rfJUhOF97k9bCfn3n64wlxFPHKpgXxY4voBRsAGNvo4wdi/DibGUGSjl34ZgygScvQAAwyJEMy1I9BhmRIhvoxyJAMyVA/BhmSIRnqxyBDMiRD/RhkmMjwCOH/8gc="
>  }
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~

FunTester测试框架对于MySQL的性能测试告一段落,下期我会使用Go语言的gorm框架重写一个对MySQL进行性能测试的Demo,敬请期待。

0 人点赞