MySQL数据库

sysbench测试mysql性能(TPS、QPS)

2019-02-26  本文已影响65人  任嘉平生愿

最近接了一个同事的需求他没有成功,所以帮一下他也有助于自己的提高。

如果没有安装mysql的可以看我这篇文章

docker 安装mysql - 简书

安装sysbench

https://github.com/akopytov/sysbench/archive/1.0.zip

下载并压缩  然后用winscp工具上传

yum install automake libtool –y

cd sysbench-1.0

./autogen.sh

./configure

注意:如果报错


执行:

yum install mysql-devel gcc gcc-devel python-devel

因为运行下面的make时候会不成功

make

make install

测试:

sysbench --version


启动mysql:

docker run -p 3306:3306 --name mymysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

首先在mysql中创建数据库sbtest

然后先执行命令

sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-host=192.168.229.129 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --oltp-test-mode=complex --oltp-tables-count=10 --oltp-table-size=100000 --threads=10 --time=120 --report-interval=10 prepare

在/home路径先创建mysysbench.log 

vi mysysbench.log 

再执行

sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-host=192.168.229.129 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --oltp-test-mode=complex --oltp-tables-count=10 --oltp-table-size=100000 --threads=10 --time=120 --report-interval=10 run >> /home/mysysbench.log

注意:参数说明

使用了10个表,每个表有10万条数据,客户端的并发线程数为10,执行时间为120秒,每10秒生成一次报告。

等大约20秒查看

cat mysysbench.log 

其中,对于我们比较重要的信息包括:

queries:查询总数及qps

transactions:事务总数及tps

Latency-95th percentile:前95%的请求的最大响应时间,本例中是28.16毫秒。

查看mysql连接数 

show variables like 'max_connections%';

建议

下面是使用sysbench的一些建议。

1、在开始测试之前,应该首先明确:应采用针对整个系统的基准测试,还是针对MySQL的基准测试,还是二者都需要。

2、如果需要针对MySQL的基准测试,那么还需要明确精度方面的要求:是否需要使用生产环境的真实数据,还是使用工具生成也可以;前者实施起来更加繁琐。如果要使用真实数据,尽量使用全部数据,而不是部分数据。

3、基准测试要进行多次才有意义。

4、测试时需要注意主从同步的状态。

5、测试必须模拟多线程的情况,单线程情况不但无法模拟真实的效率,也无法模拟阻塞甚至死锁情况

补充进阶

上面只是一个测试demo在实际的开发测试中我们要是想用自己数据库中的数据表进行测试应该修改test demo这种的lua脚本

cd sysbench-1.0/

cd  tests/

cd include/

cd oltp_legacy/

cat oltp.lua

---------------------------------------------------------------------------------------------

pathtest = string.match(test, "(.*/)")

if pathtest then

  dofile(pathtest .. "common.lua")

else

  require("common")

end

function thread_init()

  set_vars()

  if (((db_driver == "mysql") or (db_driver == "attachsql")) and mysql_table_engine == "myisam") then

      local i

      local tables = {}

      for i=1, oltp_tables_count do

        tables[i] = string.format("sbtest%i WRITE", i)

      end

      begin_query = "LOCK TABLES " .. table.concat(tables, " ,")

      commit_query = "UNLOCK TABLES"

  else

      begin_query = "BEGIN"

      commit_query = "COMMIT"

  end

end

function get_range_str()

  local start = sb_rand(1, oltp_table_size)

  return string.format(" WHERE id BETWEEN %u AND %u",

                        start, start + oltp_range_size - 1)

end

function event()

  local rs

  local i

  local table_name

  local c_val

  local pad_val

  local query

  table_name = "sbtest".. sb_rand_uniform(1, oltp_tables_count)

  if not oltp_skip_trx then

      db_query(begin_query)

  end

  if not oltp_write_only then

  for i=1, oltp_point_selects do

      rs = db_query("SELECT c FROM ".. table_name .." WHERE id=" ..

                      sb_rand(1, oltp_table_size))

  end

  if oltp_range_selects then

  for i=1, oltp_simple_ranges do

      rs = db_query("SELECT c FROM ".. table_name .. get_range_str())

  end

  for i=1, oltp_sum_ranges do

      rs = db_query("SELECT SUM(K) FROM ".. table_name .. get_range_str())

  end

  for i=1, oltp_order_ranges do

      rs = db_query("SELECT c FROM ".. table_name .. get_range_str() ..

                    " ORDER BY c")

  end

  for i=1, oltp_distinct_ranges do

      rs = db_query("SELECT DISTINCT c FROM ".. table_name .. get_range_str() ..

                    " ORDER BY c")

  end

  end

  end

  if not oltp_read_only then

  for i=1, oltp_index_updates do

      rs = db_query("UPDATE " .. table_name .. " SET k=k+1 WHERE id=" .. sb_rand(1, oltp_table_size))

  end

  for i=1, oltp_non_index_updates do

      c_val = sb_rand_str("###########-###########-###########-###########-###########-###########-###########-###########-###########-###########")

      query = "UPDATE " .. table_name .. " SET c='" .. c_val .. "' WHERE id=" .. sb_rand(1, oltp_table_size)

      rs = db_query(query)

      if rs then

        print(query)

      end

  end

  for i=1, oltp_delete_inserts do

  i = sb_rand(1, oltp_table_size)

  rs = db_query("DELETE FROM " .. table_name .. " WHERE id=" .. i)

  c_val = sb_rand_str([[

###########-###########-###########-###########-###########-###########-###########-###########-###########-###########]])

  pad_val = sb_rand_str([[

###########-###########-###########-###########-###########]])

  rs = db_query("INSERT INTO " .. table_name ..  " (id, k, c, pad) VALUES " .. string.format("(%d, %d, '%s', '%s')",i, sb_rand(1, oltp_table_size) , c_val, pad_val))

  end

  end -- oltp_read_only

  if not oltp_skip_trx then

      db_query(commit_query)

  end

end

---------------------------------------------------------------------

将里面的sbtest该文我们需要的数据表即可(有两处)

上一篇 下一篇

猜你喜欢

热点阅读