TiUP bench 概述
TiUP 集成了 bench 组件,满足对数据库进行压测的需求。
TiUP bench 组件提供多种压测的 workloads,命令分别如下:
tiup bench tpcc # 以 TPC-C 作为 workload 压测
tiup bench tpch # 以 TPC-H 作为 workload 压测
tiup bench ch # 以 CH-benCHmark 作为 workload 压测
tiup bench ycsb # 以 YCSB 作为 workload 压测
tiup bench rawsql # 以自定义 SQL 文件作为 workload 压测
其中 tpcc, tpch, ch, rawsql 支持如下命令行参数。
ycsb 使用方法较为不同,它主要通过 properties 文件进行配置,详见 go-ycsb 使用说明。
-t, --acThreads int OLAP 并发线程数,仅适用于 CH-benCHmark (默认 1)
--conn-params string 数据库连接参数,例如:
`--conn-params tidb_isolation_read_engines='tiflash'` 设置 TiDB 通过 TiFlash 进行查询
`--conn-params sslmode=disable` 设置连接 PostgreSQL 不启用加密
--count int 总执行次数,0 表示无限次
-D, --db string 被压测的数据库名 (默认为 "test")
-d, --driver string 数据库驱动: mysql, postgres (默认 "mysql")
--dropdata 在 prepare 数据之前清除历史数据
-h, --help 输出 bench 命令的帮助信息
-H, --host strings 数据库的主机地址 (默认 ["127.0.0.1"])
--ignore-error 忽略压测时数据库报出的错误
--interval duration 两次报告输出时间的间隔 (默认 10s)
--isolation int 隔离级别 0:Default,1:ReadUncommitted,
2:ReadCommitted,3:WriteCommitted,4:RepeatableRead,
5:Snapshot,6:Serializable,7:Linerizable
--max-procs int Go Runtime 能够使用的最大系统线程数
--output string 输出格式 plain,table,json (默认为 "plain")
-p, --password string 数据库密码
-P, --port ints 数据库端口 (默认 [4000])
--pprof string pprof 地址
--silence 压测过程中不打印错误信息
-S, --statusPort int TiDB 状态端口 (默认 10080)
-T, --threads int 压测并发线程数 (默认 16)
--time duration 总执行时长 (默认 2562047h47m16.854775807s)
-U, --user string 压测时使用的数据库用户 (默认 "root")
-
--host和--port支持以逗号分隔传入多个值,以启用客户端负载均衡。例如,当指定--host 172.16.4.1,172.16.4.2 --port 4000,4001时,负载程序将以轮询调度的方式连接到 172.16.4.1:4000, 172.16.4.1:4001, 172.16.4.2:4000, 172.16.4.2:4001 这 4 个实例上。 -
--conn-params需要符合 query string 格式,不同数据库支持不同参数,如:-
--conn-params tidb_isolation_read_engines='tiflash'设置 TiDB 通过 TiFlash 进行查询。 -
--conn-params sslmode=disable设置连接 PostgreSQL 不启用加密。
-
- 当运行 CH-benCHmark 时,可以通过
--ap-host,--ap-port,--ap-conn-params来指定独立的 TiDB 实例用于 OLAP 查询。
TPC-C 测试概述
TPC-C 是一个对 OLTP(联机交易处理)系统进行测试的规范,使用一个商品销售模型对 OLTP 系统进行测试,其中包含五类事务:
- NewOrder – 新订单的生成
- Payment – 订单付款
- OrderStatus – 最近订单查询
- Delivery – 配送
- StockLevel – 库存缺货状态分析
在测试开始前,TPC-C Benchmark 规定了数据库的初始状态,也就是数据库中数据生成的规则,其中 ITEM 表中固定包含 10 万种商品,仓库的数量可进行调整,假设 WAREHOUSE 表中有 W 条记录,那么:
- STOCK 表中应有 W * 10 万条记录(每个仓库对应 10 万种商品的库存数据)
- DISTRICT 表中应有 W * 10 条记录(每个仓库为 10 个地区提供服务)
- CUSTOMER 表中应有 W * 10 * 3000 条记录(每个地区有 3000 个客户)
- HISTORY 表中应有 W * 10 * 3000 条记录(每个客户一条交易历史)
- ORDER 表中应有 W * 10 * 3000 条记录(每个地区 3000 个订单),并且最后生成的 900 个订单被添加到 NEW-ORDER 表中,每个订单随机生成 5 ~ 15 条 ORDER-LINE 记录。
我们将以 1000 WAREHOUSE 为例进行测试。
TPC-C 使用 tpmC 值 (Transactions per Minute) 来衡量系统最大有效吞吐量 (MQTh, Max Qualified Throughput),其中 Transactions 以 NewOrder Transaction 为准,即最终衡量单位为每分钟处理的新订单数。
本文使用 go-tpc 作为 TPC-C 测试实现,可以通过 TiUP 命令下载测试程序:
su - tidb
tiup install bench
TiUP bench 组件支持如下运行 TPC-C 测试的命令和参数:
Available Commands:
check 检查数据一致性
cleanup 清除数据
prepare 准备数据
run 开始压测
Flags:
--check-all 运行所有的一致性检测
-h, --help 输出 TPC-C 的帮助信息
--partition-type int 分区类型 (默认为 1)
1 代表 HASH 分区类型
2 代表 RANGE 分区类型
3 代表 LIST 分区类型并按 HASH 方式划分
4 代表 LIST 分区类型并按 RANGE 方式划分
--parts int 分区仓库的数量 (默认为 1)
--warehouses int 仓库的数量 (默认为 10)
关于 TiUP Bench 组件的详细用法可参考 TiUP Bench。
假设已部署 TiDB 集群,其中 TiDB 节点部署在 172.16.5.140、 172.16.5.141 实例上,端口都为 4000,可按如下步骤进行 TPC-C 测试。
导入数据
导入数据通常是整个 TPC-C 测试中最耗时,也是最容易出问题的阶段。
在 shell 中运行 TiUP 命令:
tiup bench tpcc -H 172.16.5.140,172.16.5.141 -P 4000 -D tpcc --warehouses 1000 --threads 20 prepare
基于不同的机器配置,这个过程可能会持续几个小时。如果是小型集群,可以使用较小的 WAREHOUSE 值进行测试。
数据导入完成后,可以通过命令 tiup bench tpcc -H 172.16.5.140 -P 4000 -D tpcc --warehouses 4 check 验证数据正确性。
在我们的测试环境执行,先创建测试库并创建测试账号:
create database tpcc;
CREATE USER 'tpcc'@'%' IDENTIFIED BY 'tpcc';
GRANT all privileges on *.* to 'tpcc'@'%';
如果不提前授权好会报如下错误:
panic: failed to connect to database when loading data
然后开始prepare数据:
/home/tidb/.tiup/components/bench/v1.12.0/tiup-bench tpcc -H 10.0.8.86 -P 4000 -D tpcc -U tpcc -p tpcc --warehouses 10 --threads 4 prepare
之后会在数据库里查到新建的表和数据。
验证数据正确性:
tiup bench tpcc -H 10.0.8.86 -P 4000 -D tpcc -U tpcc -p tpcc --warehouses 4 check
运行测试
运行测试的命令参考:
tiup bench tpcc -H 172.16.5.140,172.16.5.141 -P 4000 -D tpcc --warehouses 1000 --threads 100 --time 10m run
在我们的测试环境里执行如下命令,参数与上方prepare相同即可:
tiup bench tpcc -H 10.0.8.86 -P 4000 -D tpcc -U tpcc -p tpcc --warehouses 10 --threads 100 --time 1m run
运行过程中控制台上会持续打印测试结果:
[Current] DELIVERY - Takes(s): 7.4, Count: 32, TPM: 260.5, Sum(ms): 30157.4, Avg(ms): 942.4, 50th(ms): 838.9, 90th(ms): 1744.8, 95th(ms): 1744.8, 99th(ms): 2415.9, 99.9th(ms): 2415.9, Max(ms): 2415.9
[Current] NEW_ORDER - Takes(s): 8.5, Count: 362, TPM: 2549.7, Sum(ms): 291035.1, Avg(ms): 803.9, 50th(ms): 570.4, 90th(ms): 1811.9, 95th(ms): 2147.5, 99th(ms): 2550.1, 99.9th(ms): 2952.8, Max(ms): 2952.8
[Current] ORDER_STATUS - Takes(s): 8.9, Count: 28, TPM: 188.0, Sum(ms): 2855.6, Avg(ms): 102.1, 50th(ms): 75.5, 90th(ms): 201.3, 95th(ms): 243.3, 99th(ms): 352.3, 99.9th(ms): 352.3, Max(ms): 352.3
[Current] PAYMENT - Takes(s): 8.6, Count: 360, TPM: 2503.7, Sum(ms): 498930.8, Avg(ms): 1386.7, 50th(ms): 1409.3, 90th(ms): 2415.9, 95th(ms): 2684.4, 99th(ms): 3087.0, 99.9th(ms): 3489.7, Max(ms): 3489.7
[Current] STOCK_LEVEL - Takes(s): 8.4, Count: 24, TPM: 171.6, Sum(ms): 17859.8, Avg(ms): 741.8, 50th(ms): 805.3, 90th(ms): 1409.3, 95th(ms): 1476.4, 99th(ms): 1610.6, 99.9th(ms): 1610.6, Max(ms): 1610.6
[Current] DELIVERY - Takes(s): 9.9, Count: 74, TPM: 447.6, Sum(ms): 48456.0, Avg(ms): 655.6, 50th(ms): 604.0, 90th(ms): 1275.1, 95th(ms): 1610.6, 99th(ms): 1744.8, 99.9th(ms): 1879.0, Max(ms): 1879.0
[Current] NEW_ORDER - Takes(s): 10.0, Count: 619, TPM: 3727.0, Sum(ms): 344935.8, Avg(ms): 557.1, 50th(ms): 260.0, 90th(ms): 1610.6, 95th(ms): 2281.7, 99th(ms): 3221.2, 99.9th(ms): 3489.7, Max(ms): 3623.9
[Current] ORDER_STATUS - Takes(s): 9.8, Count: 51, TPM: 313.2, Sum(ms): 2506.0, Avg(ms): 49.0, 50th(ms): 48.2, 90th(ms): 79.7, 95th(ms): 88.1, 99th(ms): 125.8, 99.9th(ms): 134.2, Max(ms): 134.2
[Current] PAYMENT - Takes(s): 10.0, Count: 614, TPM: 3692.0, Sum(ms): 570598.7, Avg(ms): 928.8, 50th(ms): 604.0, 90th(ms): 2415.9, 95th(ms): 2818.6, 99th(ms): 3489.7, 99.9th(ms): 3623.9, Max(ms): 3623.9
...
运行结束后,会打印测试统计结果:
Finished
[Summary] DELIVERY - Takes(s): 57.4, Count: 365, TPM: 381.3, Sum(ms): 298490.3, Avg(ms): 818.5, 50th(ms): 671.1, 90th(ms): 1543.5, 95th(ms): 1879.0, 99th(ms): 2818.6, 99.9th(ms): 4295.0, Max(ms): 4295.0
[Summary] DELIVERY_ERR - Takes(s): 57.4, Count: 3, TPM: 3.1, Sum(ms): 782.8, Avg(ms): 260.3, 50th(ms): 268.4, 90th(ms): 520.1, 95th(ms): 520.1, 99th(ms): 520.1, 99.9th(ms): 520.1, Max(ms): 520.1
[Summary] NEW_ORDER - Takes(s): 58.6, Count: 3771, TPM: 3862.0, Sum(ms): 2122544.5, Avg(ms): 563.4, 50th(ms): 243.3, 90th(ms): 1476.4, 95th(ms): 2415.9, 99th(ms): 4026.5, 99.9th(ms): 5368.7, Max(ms): 5368.7
[Summary] NEW_ORDER_ERR - Takes(s): 58.6, Count: 34, TPM: 34.8, Sum(ms): 11935.4, Avg(ms): 349.8, 50th(ms): 176.2, 90th(ms): 838.9, 95th(ms): 872.4, 99th(ms): 1409.3, 99.9th(ms): 1409.3, Max(ms): 1409.3
[Summary] ORDER_STATUS - Takes(s): 59.0, Count: 355, TPM: 361.0, Sum(ms): 23330.8, Avg(ms): 65.7, 50th(ms): 58.7, 90th(ms): 109.1, 95th(ms): 142.6, 99th(ms): 218.1, 99.9th(ms): 352.3, Max(ms): 352.3
[Summary] PAYMENT - Takes(s): 58.7, Count: 3745, TPM: 3828.3, Sum(ms): 3363910.8, Avg(ms): 898.4, 50th(ms): 486.5, 90th(ms): 2415.9, 95th(ms): 3489.7, 99th(ms): 4831.8, 99.9th(ms): 5637.1, Max(ms): 5637.1
[Summary] PAYMENT_ERR - Takes(s): 58.7, Count: 53, TPM: 54.2, Sum(ms): 33377.8, Avg(ms): 631.4, 50th(ms): 536.9, 90th(ms): 1409.3, 95th(ms): 1409.3, 99th(ms): 1543.5, 99.9th(ms): 1543.5, Max(ms): 1543.5
[Summary] STOCK_LEVEL - Takes(s): 58.5, Count: 274, TPM: 281.2, Sum(ms): 42555.3, Avg(ms): 155.3, 50th(ms): 71.3, 90th(ms): 352.3, 95th(ms): 704.6, 99th(ms): 1275.1, 99.9th(ms): 1610.6, Max(ms): 1610.6
tpmC: 3862.0, tpmTotal: 8713.8, efficiency: 3003.1%
官网给出的warehouses 1000、threads 100、10分钟测试统计结果为:
[Summary] DELIVERY - Takes(s): 455.2, Count: 32, TPM: 4.2, Sum(ms): 44376, Avg(ms): 1386, 90th(ms): 2000, 99th(ms): 4000, 99.9th(ms): 4000
[Summary] DELIVERY_ERR - Takes(s): 455.2, Count: 1, TPM: 0.1, Sum(ms): 953, Avg(ms): 953, 90th(ms): 1000, 99th(ms): 1000, 99.9th(ms): 1000
[Summary] NEW_ORDER - Takes(s): 487.8, Count: 314, TPM: 38.6, Sum(ms): 282377, Avg(ms): 899, 90th(ms): 1500, 99th(ms): 1500, 99.9th(ms): 1500
[Summary] ORDER_STATUS - Takes(s): 484.6, Count: 29, TPM: 3.6, Sum(ms): 8423, Avg(ms): 290, 90th(ms): 512, 99th(ms): 1500, 99.9th(ms): 1500
[Summary] PAYMENT - Takes(s): 490.1, Count: 321, TPM: 39.3, Sum(ms): 144708, Avg(ms): 450, 90th(ms): 512, 99th(ms): 1000, 99.9th(ms): 1500
[Summary] STOCK_LEVEL - Takes(s): 487.6, Count: 41, TPM: 5.0, Sum(ms): 9318, Avg(ms): 227, 90th(ms): 512, 99th(ms): 1000, 99.9th(ms): 1000
测试完成之后,也可以运行 tiup bench tpcc -H 172.16.5.140 -P 4000 -D tpcc --warehouses 4 check 进行数据正确性验证。我们测试环境执行:
tiup bench tpcc -H 10.0.8.86 -P 4000 -D tpcc -U tpcc -p tpcc --warehouses 4 check
清理测试数据
命令如下:
tiup bench tpcc -H 10.0.8.86 -P 4000 -D tpcc -U tpcc -p tpcc --warehouses 4 cleanup











网友评论