开发者社区> 问答> 正文

用户指南-读写分离-测试读写分离性能


开通读写分离功能后,事务会默认全部路由至主实例上执行。本文将以常用的MySQL压测工具Sysbench 0.5版本为例,介绍如何正确配置其参数来进行读写分离性能的测试。

前提条件



注意事项


  • 建议测试读写分离的负载均衡不要用带prepare或者带事务的case。

  • 避免因写压力过大而造成的主从延迟时间超过设定的监控检查阈值。

  • 推荐使用如下Sysbench脚本,您可以实际情况构造具体的SQL。function thread_init(thread_id)
  •       db_connect()
  •   end
  •   function event(thread_id)
  •       rs =  db_query("select 1")
  •   end





  • 设置Sysbench的参数

    Sysbench oltp.lua脚本测试默认使用事务,若使用默认参数,所有SQL都会在事务中执行,即使是只读SQL也会全部路由至主库上执行。所以,使用Sysbench压测读写分离的性能时,必须根据需求设置Sysbench的参数。例如,您可以通过设置oltp-skip-trx参数可以使Sysbench运行SQL时不在事务中执行。

    设置常用参数

    请根据您的实际业务情况,设置如下参数值。
    [tr=rgb(51, 205, 229)][td]名称
描述test指定测试文件路径。mysql-hostMySQL服务器地址。mysql-portMySQL服务器端口。mysql-user用户名。mysql-password密码。mysql-db测试使用数据库,需提前创建。oltp-tables-count建立表的个数。oltp-table-size每个表产生的记录数量。rand-init是否随机初始化数据。max-time压测持续时间。max-requests压测期间请求总数。num-threads并发线程数量。report-interval运行日志打印间隔。

设置事务及读写SQL相关参数

如下参数会影响事务及读写SQL,在进行读写分离性能测试时按照实际需求设置参数值。 1062 1062
[tr=rgb(51, 205, 229)][td]名称 描述oltp-test-mode测试类型,但在Sysbench 0.5版本中此参数没有生效,可以忽略。可选参数值如下:
  • complex:默认值,事务测试。
  • simple:简单只读SQL测试。
  • nontrx:非事务测试。
  • sp:存储过程。
  • oltp-skip-trx是否跳过SQL语句开头的begin和结尾的commit。可选参数值如下:
    • off:默认值,执行的SQL全部在事务中。
    • on:非事务模式,若执行连续的对比压测,需要重新准备数据(prepare)和清除数据(cleanup)。
    [table][tr=transparent][td=1,2][url=http://g.alicdn.com/aliyun-icms/assets/icms-main/images/note.png][/url]说明[tr=transparent][td]非事务的读写测试更新数据时容易出现类似ALERT: Error Duplicate entry 'xxx' for key 'PRIMARY'的错误,所以需要增加参数--mysql-ignore-errors=来跳过这个错误。若参数mysql-ignore-errors没有生效,则说明Sysbench版本较低,需将其升级至最新的0.5版本。 sysbench --test=./tests/db/oltp.lua --mysql-host=127.0.0.1 --mysql-port=3001 --mysql-user=abc --mysql-password=abc123456 --mysql-db=testdb --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=5 --oltp-skip-trx=on --oltp-read-only=off --mysql-ignore-errors=1062 --rand-init=on --max-requests=0 --max-time=300 --num-threads=100 run;
  • 执行如下命令,清除数据。sysbench --test=./tests/db/oltp.lua --mysql-host=127.0.0.1 --mysql-port=3001 --mysql-user=abc --mysql-password=abc123456 --mysql-db=testdb --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=5 --oltp-skip-trx=on --oltp-read-only=off --rand-init=on --max-requests=0 --max-time=300 --num-threads=100 cleanup;
  • 测试只读性能
    1. 执行如下命令,准备数据。sysbench --test=./tests/db/oltp.lua --mysql-host=127.0.0.1 --mysql-port=3001 --mysql-user=abc --mysql-password=abc123456 --mysql-db=testdb --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=5 --oltp-skip-trx=on --oltp-read-only=on --rand-init=on --max-requests=0 --max-time=300 --num-threads=100 prepare;
    2. 执行如下命令,运行测试。sysbench --test=./tests/db/oltp.lua --mysql-host=127.0.0.1 --mysql-port=3001 --mysql-user=abc --mysql-password=abc123456 --mysql-db=testdb --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=5 --oltp-skip-trx=on --oltp-read-only=on --rand-init=on --max-requests=0 --max-time=300 --num-threads=100 run;
    3. 执行如下命令,清除数据。sysbench --test=./tests/db/oltp.lua --mysql-host=127.0.0.1 --mysql-port=3001 --mysql-user=abc --mysql-password=abc123456 --mysql-db=testdb --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=5 --oltp-skip-trx=on --oltp-read-only=on --rand-init=on --max-requests=0 --max-time=300 --num-threads=100 cleanup;



    展开
    收起
    梨好橙 2018-09-03 23:51:11 1527 0
    0 条回答
    写回答
    取消 提交回答
    问答排行榜
    最热
    最新

    相关电子书

    更多
    F2etest — 多浏览器兼容性测试整体解决方案 立即下载
    MaxCompute基于BigBench标准的最新测试进展 立即下载
    前端自动化测试 立即下载