开通读写分离功能后,事务会默认全部路由至主实例上执行。本文将以常用的MySQL压测工具Sysbench 0.5版本为例,介绍如何正确配置其参数来进行读写分离性能的测试。
前提条件
注意事项
建议测试读写分离的负载均衡不要用带prepare或者带事务的case。
避免因写压力过大而造成的主从延迟时间超过设定的监控检查阀值。
推荐使用如下Sysbench脚本,您可以实际情况构造具体的SQL。
-
[backcolor=transparent]
[backcolor=transparent]
function[backcolor=transparent]
thread_init[backcolor=transparent]
([backcolor=transparent]
thread_id[backcolor=transparent]
)
- [backcolor=transparent] db_connect[backcolor=transparent]()
- [backcolor=transparent] [backcolor=transparent]end
- [backcolor=transparent] [backcolor=transparent]function[backcolor=transparent] [backcolor=transparent]event[backcolor=transparent]([backcolor=transparent]thread_id[backcolor=transparent])
- [backcolor=transparent] rs [backcolor=transparent]=[backcolor=transparent] db_query[backcolor=transparent]([backcolor=transparent]"select 1"[backcolor=transparent])
- [backcolor=transparent] [backcolor=transparent]end
设置Sysbench的参数
Sysbench oltp.lua脚本测试默认使用事务,若使用默认参数,所有SQL都会在事务中执行,即使是只读SQL也会全部路由至主库上执行。所以,使用Sysbench压测读写分离的性能时,必须根据需求设置Sysbench的参数。例如,您可以通过设置oltp-skip-trx参数可以使Sysbench运行SQL时不在事务中执行。
设置常用参数
请根据您的实际业务情况,设置如下参数值。
设置事务及读写SQL相关参数
如下参数会影响事务及读写SQL,在进行读写分离性能测试时按照实际需求设置参数值。
压测示例
测试读写性能
执行如下命令,准备数据。[backcolor=transparent]sysbench [backcolor=transparent]--[backcolor=transparent]test[backcolor=transparent]=./[backcolor=transparent]tests[backcolor=transparent]/[backcolor=transparent]db[backcolor=transparent]/[backcolor=transparent]oltp[backcolor=transparent].[backcolor=transparent]lua [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]host[backcolor=transparent]=[backcolor=transparent]127.0[backcolor=transparent].[backcolor=transparent]0.1[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]port[backcolor=transparent]=[backcolor=transparent]3001[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]user[backcolor=transparent]=[backcolor=transparent]abc [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]password[backcolor=transparent]=[backcolor=transparent]abc123456 [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]db[backcolor=transparent]=[backcolor=transparent]testdb [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]tables[backcolor=transparent]-[backcolor=transparent]count[backcolor=transparent]=[backcolor=transparent]10[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]table[backcolor=transparent]-[backcolor=transparent]size[backcolor=transparent]=[backcolor=transparent]500000[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]report[backcolor=transparent]-[backcolor=transparent]interval[backcolor=transparent]=[backcolor=transparent]5[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]skip[backcolor=transparent]-[backcolor=transparent]trx[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]read[backcolor=transparent]-[backcolor=transparent]only[backcolor=transparent]=[backcolor=transparent]off [backcolor=transparent]--[backcolor=transparent]rand[backcolor=transparent]-[backcolor=transparent]init[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]max[backcolor=transparent]-[backcolor=transparent]requests[backcolor=transparent]=[backcolor=transparent]0[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]max[backcolor=transparent]-[backcolor=transparent]time[backcolor=transparent]=[backcolor=transparent]300[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]num[backcolor=transparent]-[backcolor=transparent]threads[backcolor=transparent]=[backcolor=transparent]100[backcolor=transparent] prepare[backcolor=transparent];
执行如下命令,运行测试。
说明:非事务的读写测试更新数据时容易出现类似ALERT: Error 1062 Duplicate entry 'xxx' for key 'PRIMARY'的错误,所以需要增加参数--mysql-ignore-errors=1062来跳过这个错误。若参数mysql-ignore-errors没有生效,则说明Sysbench版本较低,需将其升级至最新的0.5版本。
- [backcolor=transparent]sysbench [backcolor=transparent]--[backcolor=transparent]test[backcolor=transparent]=./[backcolor=transparent]tests[backcolor=transparent]/[backcolor=transparent]db[backcolor=transparent]/[backcolor=transparent]oltp[backcolor=transparent].[backcolor=transparent]lua [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]host[backcolor=transparent]=[backcolor=transparent]127.0[backcolor=transparent].[backcolor=transparent]0.1[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]port[backcolor=transparent]=[backcolor=transparent]3001[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]user[backcolor=transparent]=[backcolor=transparent]abc [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]password[backcolor=transparent]=[backcolor=transparent]abc123456 [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]db[backcolor=transparent]=[backcolor=transparent]testdb [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]tables[backcolor=transparent]-[backcolor=transparent]count[backcolor=transparent]=[backcolor=transparent]10[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]table[backcolor=transparent]-[backcolor=transparent]size[backcolor=transparent]=[backcolor=transparent]500000[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]report[backcolor=transparent]-[backcolor=transparent]interval[backcolor=transparent]=[backcolor=transparent]5[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]skip[backcolor=transparent]-[backcolor=transparent]trx[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]read[backcolor=transparent]-[backcolor=transparent]only[backcolor=transparent]=[backcolor=transparent]off [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]ignore[backcolor=transparent]-[backcolor=transparent]errors[backcolor=transparent]=[backcolor=transparent]1062[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]rand[backcolor=transparent]-[backcolor=transparent]init[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]max[backcolor=transparent]-[backcolor=transparent]requests[backcolor=transparent]=[backcolor=transparent]0[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]max[backcolor=transparent]-[backcolor=transparent]time[backcolor=transparent]=[backcolor=transparent]300[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]num[backcolor=transparent]-[backcolor=transparent]threads[backcolor=transparent]=[backcolor=transparent]100[backcolor=transparent] run[backcolor=transparent];
执行如下命令,清除数据。
- [backcolor=transparent]sysbench [backcolor=transparent]--[backcolor=transparent]test[backcolor=transparent]=./[backcolor=transparent]tests[backcolor=transparent]/[backcolor=transparent]db[backcolor=transparent]/[backcolor=transparent]oltp[backcolor=transparent].[backcolor=transparent]lua [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]host[backcolor=transparent]=[backcolor=transparent]127.0[backcolor=transparent].[backcolor=transparent]0.1[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]port[backcolor=transparent]=[backcolor=transparent]3001[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]user[backcolor=transparent]=[backcolor=transparent]abc [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]password[backcolor=transparent]=[backcolor=transparent]abc123456 [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]db[backcolor=transparent]=[backcolor=transparent]testdb [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]tables[backcolor=transparent]-[backcolor=transparent]count[backcolor=transparent]=[backcolor=transparent]10[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]table[backcolor=transparent]-[backcolor=transparent]size[backcolor=transparent]=[backcolor=transparent]500000[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]report[backcolor=transparent]-[backcolor=transparent]interval[backcolor=transparent]=[backcolor=transparent]5[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]skip[backcolor=transparent]-[backcolor=transparent]trx[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]read[backcolor=transparent]-[backcolor=transparent]only[backcolor=transparent]=[backcolor=transparent]off [backcolor=transparent]--[backcolor=transparent]rand[backcolor=transparent]-[backcolor=transparent]init[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]max[backcolor=transparent]-[backcolor=transparent]requests[backcolor=transparent]=[backcolor=transparent]0[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]max[backcolor=transparent]-[backcolor=transparent]time[backcolor=transparent]=[backcolor=transparent]300[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]num[backcolor=transparent]-[backcolor=transparent]threads[backcolor=transparent]=[backcolor=transparent]100[backcolor=transparent] cleanup[backcolor=transparent];
测试只读性能
执行如下命令,准备数据。[backcolor=transparent]sysbench [backcolor=transparent]--[backcolor=transparent]test[backcolor=transparent]=./[backcolor=transparent]tests[backcolor=transparent]/[backcolor=transparent]db[backcolor=transparent]/[backcolor=transparent]oltp[backcolor=transparent].[backcolor=transparent]lua [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]host[backcolor=transparent]=[backcolor=transparent]127.0[backcolor=transparent].[backcolor=transparent]0.1[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]port[backcolor=transparent]=[backcolor=transparent]3001[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]user[backcolor=transparent]=[backcolor=transparent]abc [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]password[backcolor=transparent]=[backcolor=transparent]abc123456 [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]db[backcolor=transparent]=[backcolor=transparent]testdb [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]tables[backcolor=transparent]-[backcolor=transparent]count[backcolor=transparent]=[backcolor=transparent]10[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]table[backcolor=transparent]-[backcolor=transparent]size[backcolor=transparent]=[backcolor=transparent]500000[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]report[backcolor=transparent]-[backcolor=transparent]interval[backcolor=transparent]=[backcolor=transparent]5[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]skip[backcolor=transparent]-[backcolor=transparent]trx[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]read[backcolor=transparent]-[backcolor=transparent]only[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]rand[backcolor=transparent]-[backcolor=transparent]init[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]max[backcolor=transparent]-[backcolor=transparent]requests[backcolor=transparent]=[backcolor=transparent]0[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]max[backcolor=transparent]-[backcolor=transparent]time[backcolor=transparent]=[backcolor=transparent]300[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]num[backcolor=transparent]-[backcolor=transparent]threads[backcolor=transparent]=[backcolor=transparent]100[backcolor=transparent] prepare[backcolor=transparent];
执行如下命令,运行测试。
- [backcolor=transparent]sysbench [backcolor=transparent]--[backcolor=transparent]test[backcolor=transparent]=./[backcolor=transparent]tests[backcolor=transparent]/[backcolor=transparent]db[backcolor=transparent]/[backcolor=transparent]oltp[backcolor=transparent].[backcolor=transparent]lua [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]host[backcolor=transparent]=[backcolor=transparent]127.0[backcolor=transparent].[backcolor=transparent]0.1[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]port[backcolor=transparent]=[backcolor=transparent]3001[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]user[backcolor=transparent]=[backcolor=transparent]abc [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]password[backcolor=transparent]=[backcolor=transparent]abc123456 [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]db[backcolor=transparent]=[backcolor=transparent]testdb [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]tables[backcolor=transparent]-[backcolor=transparent]count[backcolor=transparent]=[backcolor=transparent]10[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]table[backcolor=transparent]-[backcolor=transparent]size[backcolor=transparent]=[backcolor=transparent]500000[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]report[backcolor=transparent]-[backcolor=transparent]interval[backcolor=transparent]=[backcolor=transparent]5[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]skip[backcolor=transparent]-[backcolor=transparent]trx[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]read[backcolor=transparent]-[backcolor=transparent]only[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]rand[backcolor=transparent]-[backcolor=transparent]init[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]max[backcolor=transparent]-[backcolor=transparent]requests[backcolor=transparent]=[backcolor=transparent]0[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]max[backcolor=transparent]-[backcolor=transparent]time[backcolor=transparent]=[backcolor=transparent]300[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]num[backcolor=transparent]-[backcolor=transparent]threads[backcolor=transparent]=[backcolor=transparent]100[backcolor=transparent] run[backcolor=transparent];
执行如下命令,清除数据。
- [backcolor=transparent]sysbench [backcolor=transparent]--[backcolor=transparent]test[backcolor=transparent]=./[backcolor=transparent]tests[backcolor=transparent]/[backcolor=transparent]db[backcolor=transparent]/[backcolor=transparent]oltp[backcolor=transparent].[backcolor=transparent]lua [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]host[backcolor=transparent]=[backcolor=transparent]127.0[backcolor=transparent].[backcolor=transparent]0.1[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]port[backcolor=transparent]=[backcolor=transparent]3001[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]user[backcolor=transparent]=[backcolor=transparent]abc [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]password[backcolor=transparent]=[backcolor=transparent]abc123456 [backcolor=transparent]--[backcolor=transparent]mysql[backcolor=transparent]-[backcolor=transparent]db[backcolor=transparent]=[backcolor=transparent]testdb [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]tables[backcolor=transparent]-[backcolor=transparent]count[backcolor=transparent]=[backcolor=transparent]10[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]table[backcolor=transparent]-[backcolor=transparent]size[backcolor=transparent]=[backcolor=transparent]500000[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]report[backcolor=transparent]-[backcolor=transparent]interval[backcolor=transparent]=[backcolor=transparent]5[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]skip[backcolor=transparent]-[backcolor=transparent]trx[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]oltp[backcolor=transparent]-[backcolor=transparent]read[backcolor=transparent]-[backcolor=transparent]only[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]rand[backcolor=transparent]-[backcolor=transparent]init[backcolor=transparent]=[backcolor=transparent]on [backcolor=transparent]--[backcolor=transparent]max[backcolor=transparent]-[backcolor=transparent]requests[backcolor=transparent]=[backcolor=transparent]0[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]max[backcolor=transparent]-[backcolor=transparent]time[backcolor=transparent]=[backcolor=transparent]300[backcolor=transparent] [backcolor=transparent]--[backcolor=transparent]num[backcolor=transparent]-[backcolor=transparent]threads[backcolor=transparent]=[backcolor=transparent]100[backcolor=transparent] cleanup[backcolor=transparent];