性能测试

目标

对Sharding-JDBC,Sharding-Proxy及MySQL进行性能对比。从业务角度考虑,在基本应用场景(单路由,主从+脱敏+分库分表,全路由)下,INSERT+UPDATE+DELETE通常用作一个完整的关联操作,用于性能评估,而SELECT关注分片优化可用作性能评估的另一个操作;而主从模式下,可将INSERT+SELECT+DELETE作为一组评估性能的关联操作。为了更好的观察效果,设计在一定数据量的基础上,使用jmeter 20并发线程持续压测半小时,进行增删改查性能测试,且每台机器部署一个MySQL实例。

测试场景

单路由

在1000数据量的基础上分库分表,根据id分为4个库,部署在同一台机器上,根据k分为1024个表,查询操作路由到单库单表。

主从

基本主从场景,设置一主库一从库,部署在两台不同的机器上,在10000数据量的基础上,观察读写性能。

主从+脱敏+分库分表

在1000数据量的基础上,根据id分为4个库,部署在四台不同的机器上,根据k分为1024个表,c使用aes加密,pad使用md5加密,查询操作路由到单库单表。

全路由

在1000数据量的基础上,分库分表,根据id分为4个库,部署在四台不同的机器上,根据k分为1个表,查询操作使用全路由。

测试环境搭建

数据库表结构

此处表结构参考sysbench的sbtest表

  1. CREATE TABLE `tbl` (
  2. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  3. `k` int(11) NOT NULL DEFAULT 0,
  4. `c` char(120) NOT NULL DEFAULT '',
  5. `pad` char(60) NOT NULL DEFAULT '',
  6. PRIMARY KEY (`id`)
  7. );

测试场景配置

Sharding-JDBC使用与Sharding-Proxy一致的配置,MySQL直连一个库用作性能对比,下面为四个场景的具体配置:

单路由配置
  1. schemaName: sharding_db
  2. dataSources:
  3. ds_0:
  4. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  5. username: test
  6. password:
  7. connectionTimeoutMilliseconds: 30000
  8. idleTimeoutMilliseconds: 60000
  9. maxLifetimeMilliseconds: 1800000
  10. maxPoolSize: 200
  11. ds_1:
  12. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  13. username: test
  14. password:
  15. connectionTimeoutMilliseconds: 30000
  16. idleTimeoutMilliseconds: 60000
  17. maxLifetimeMilliseconds: 1800000
  18. maxPoolSize: 200
  19. ds_2:
  20. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  21. username: test
  22. password:
  23. connectionTimeoutMilliseconds: 30000
  24. idleTimeoutMilliseconds: 60000
  25. maxLifetimeMilliseconds: 1800000
  26. maxPoolSize: 200
  27. ds_3:
  28. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  29. username: test
  30. password:
  31. connectionTimeoutMilliseconds: 30000
  32. idleTimeoutMilliseconds: 60000
  33. maxLifetimeMilliseconds: 1800000
  34. maxPoolSize: 200
  35. shardingRule:
  36. tables:
  37. tbl:
  38. actualDataNodes: ds_${0..3}.tbl${0..1023}
  39. tableStrategy:
  40. inline:
  41. shardingColumn: k
  42. algorithmExpression: tbl${k % 1024}
  43. keyGenerator:
  44. type: SNOWFLAKE
  45. column: id
  46. defaultDatabaseStrategy:
  47. inline:
  48. shardingColumn: id
  49. algorithmExpression: ds_${id % 4}
  50. defaultTableStrategy:
  51. none:
主从配置
  1. schemaName: sharding_db
  2. dataSources:
  3. master_ds:
  4. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  5. username: test
  6. password:
  7. connectionTimeoutMilliseconds: 30000
  8. idleTimeoutMilliseconds: 60000
  9. maxLifetimeMilliseconds: 1800000
  10. maxPoolSize: 200
  11. slave_ds_0:
  12. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  13. username: test
  14. password:
  15. connectionTimeoutMilliseconds: 30000
  16. idleTimeoutMilliseconds: 60000
  17. maxLifetimeMilliseconds: 1800000
  18. maxPoolSize: 200
  19. masterSlaveRule:
  20. name: ms_ds
  21. masterDataSourceName: master_ds
  22. slaveDataSourceNames:
  23. - slave_ds_0
主从+脱敏+分库分表配置
  1. schemaName: sharding_db
  2. dataSources:
  3. master_ds_0:
  4. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  5. username: test
  6. password:
  7. connectionTimeoutMilliseconds: 30000
  8. idleTimeoutMilliseconds: 60000
  9. maxLifetimeMilliseconds: 1800000
  10. maxPoolSize: 200
  11. slave_ds_0:
  12. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  13. username: test
  14. password:
  15. connectionTimeoutMilliseconds: 30000
  16. idleTimeoutMilliseconds: 60000
  17. maxLifetimeMilliseconds: 1800000
  18. maxPoolSize: 200
  19. master_ds_1:
  20. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  21. username: test
  22. password:
  23. connectionTimeoutMilliseconds: 30000
  24. idleTimeoutMilliseconds: 60000
  25. maxLifetimeMilliseconds: 1800000
  26. maxPoolSize: 200
  27. slave_ds_1:
  28. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  29. username: test
  30. password:
  31. connectionTimeoutMilliseconds: 30000
  32. idleTimeoutMilliseconds: 60000
  33. maxLifetimeMilliseconds: 1800000
  34. maxPoolSize: 200
  35. master_ds_2:
  36. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  37. username: test
  38. password:
  39. connectionTimeoutMilliseconds: 30000
  40. idleTimeoutMilliseconds: 60000
  41. maxLifetimeMilliseconds: 1800000
  42. maxPoolSize: 200
  43. slave_ds_2:
  44. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  45. username: test
  46. password:
  47. connectionTimeoutMilliseconds: 30000
  48. idleTimeoutMilliseconds: 60000
  49. maxLifetimeMilliseconds: 1800000
  50. maxPoolSize: 200
  51. master_ds_3:
  52. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  53. username: test
  54. password:
  55. connectionTimeoutMilliseconds: 30000
  56. idleTimeoutMilliseconds: 60000
  57. maxLifetimeMilliseconds: 1800000
  58. maxPoolSize: 200
  59. slave_ds_3:
  60. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  61. username: test
  62. password:
  63. connectionTimeoutMilliseconds: 30000
  64. idleTimeoutMilliseconds: 60000
  65. maxLifetimeMilliseconds: 1800000
  66. maxPoolSize: 200
  67. shardingRule:
  68. tables:
  69. tbl:
  70. actualDataNodes: ms_ds_${0..3}.tbl${0..1023}
  71. databaseStrategy:
  72. inline:
  73. shardingColumn: id
  74. algorithmExpression: ms_ds_${id % 4}
  75. tableStrategy:
  76. inline:
  77. shardingColumn: k
  78. algorithmExpression: tbl${k % 1024}
  79. keyGenerator:
  80. type: SNOWFLAKE
  81. column: id
  82. bindingTables:
  83. - tbl
  84. defaultDataSourceName: master_ds_1
  85. defaultTableStrategy:
  86. none:
  87. masterSlaveRules:
  88. ms_ds_0:
  89. masterDataSourceName: master_ds_0
  90. slaveDataSourceNames:
  91. - slave_ds_0
  92. loadBalanceAlgorithmType: ROUND_ROBIN
  93. ms_ds_1:
  94. masterDataSourceName: master_ds_1
  95. slaveDataSourceNames:
  96. - slave_ds_1
  97. loadBalanceAlgorithmType: ROUND_ROBIN
  98. ms_ds_2:
  99. masterDataSourceName: master_ds_2
  100. slaveDataSourceNames:
  101. - slave_ds_2
  102. loadBalanceAlgorithmType: ROUND_ROBIN
  103. ms_ds_3:
  104. masterDataSourceName: master_ds_3
  105. slaveDataSourceNames:
  106. - slave_ds_3
  107. loadBalanceAlgorithmType: ROUND_ROBIN
  108. encryptRule:
  109. encryptors:
  110. encryptor_aes:
  111. type: aes
  112. props:
  113. aes.key.value: 123456abc
  114. encryptor_md5:
  115. type: md5
  116. tables:
  117. sbtest:
  118. columns:
  119. c:
  120. plainColumn: c_plain
  121. cipherColumn: c_cipher
  122. encryptor: encryptor_aes
  123. pad:
  124. cipherColumn: pad_cipher
  125. encryptor: encryptor_md5
全路由
  1. schemaName: sharding_db
  2. dataSources:
  3. ds_0:
  4. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  5. username: test
  6. password:
  7. connectionTimeoutMilliseconds: 30000
  8. idleTimeoutMilliseconds: 60000
  9. maxLifetimeMilliseconds: 1800000
  10. maxPoolSize: 200
  11. ds_1:
  12. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  13. username: test
  14. password:
  15. connectionTimeoutMilliseconds: 30000
  16. idleTimeoutMilliseconds: 60000
  17. maxLifetimeMilliseconds: 1800000
  18. maxPoolSize: 200
  19. ds_2:
  20. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  21. username: test
  22. password:
  23. connectionTimeoutMilliseconds: 30000
  24. idleTimeoutMilliseconds: 60000
  25. maxLifetimeMilliseconds: 1800000
  26. maxPoolSize: 200
  27. ds_3:
  28. url: jdbc:mysql://***.***.***.***:****/ds?serverTimezone=UTC&useSSL=false
  29. username: test
  30. password:
  31. connectionTimeoutMilliseconds: 30000
  32. idleTimeoutMilliseconds: 60000
  33. maxLifetimeMilliseconds: 1800000
  34. maxPoolSize: 200
  35. shardingRule:
  36. tables:
  37. tbl:
  38. actualDataNodes: ds_${0..3}.tbl1
  39. tableStrategy:
  40. inline:
  41. shardingColumn: k
  42. algorithmExpression: tbl1
  43. keyGenerator:
  44. type: SNOWFLAKE
  45. column: id
  46. defaultDatabaseStrategy:
  47. inline:
  48. shardingColumn: id
  49. algorithmExpression: ds_${id % 4}
  50. defaultTableStrategy:
  51. none:

测试结果验证

压测语句

  1. INSERT+UPDATE+DELETE语句:
  2. INSERT INTO tbl(k, c, pad) VALUES(1, '###-###-###', '###-###');
  3. UPDATE tbl SET c='####-####-####', pad='####-####' WHERE id=?;
  4. DELETE FROM tbl WHERE id=?
  5. 全路由查询语句:
  6. SELECT max(id) FROM tbl WHERE id%4=1
  7. 单路由查询语句:
  8. SELECT id, k FROM tbl ignore index(`PRIMARY`) WHERE id=1 AND k=1
  9. INSERT+SELECT+DELETE语句:
  10. INSERT INTO tbl1(k, c, pad) VALUES(1, '###-###-###', '###-###');
  11. SELECT count(id) FROM tbl1;
  12. SELECT max(id) FROM tbl1 ignore index(`PRIMARY`);
  13. DELETE FROM tbl1 WHERE id=?

压测类

参考shardingsphere-benchmark实现,注意阅读其中的注释

编译

  1. git clone https://github.com/apache/incubator-shardingsphere-benchmark.git
  2. cd incubator-shardingsphere-benchmark/shardingsphere-benchmark
  3. mvn clean install

压测执行

  1. cp target/shardingsphere-benchmark-1.0-SNAPSHOT-jar-with-dependencies.jar apache-jmeter-4.0/lib/ext
  2. jmeter n t test_plan/test.jmx
  3. test.jmx参考https://github.com/apache/incubator-shardingsphere-benchmark/tree/master/report/script/test_plan/test.jmx

压测结果处理

注意修改为上一步生成的result.jtl的位置。

  1. sh shardingsphere-benchmark/report/script/gen_report.sh

历史压测数据展示

Benchmark性能平台是数据以天粒度展示