使用 TiUP bench 组件压测 TiDB
在测试数据库性能时,经常需要对数据库进行压测,为了满足这一需求,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 个实例上。- 本地部署的情况下,数据库的主机地址默认 
127.0.0.1。如果没有部署到本地,请使用完整的tiup bench命令指定相关参数,例如:tiup bench tpcc -H 192.168.169.31 -P 4000 -D tpcc -U root -P tidb --warehouses 4 --parts 4 prepare --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 查询。 
下文分别介绍如何使用 TiUP 运行 TPC-C, TPC-H 以及 YCSB 测试。
使用 TiUP 运行 TPC-C 测试
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)
TPC-C 测试步骤
以下为简化后的关键步骤。完整的测试流程可以参考如何对 TiDB 进行 TPC-C 测试
通过 HASH 使用 4 个分区创建 4 个仓库:
tiup bench tpcc --warehouses 4 --parts 4 prepare运行 TPC-C 测试:
tiup bench tpcc --warehouses 4 --time 10m run检查一致性:
tiup bench tpcc --warehouses 4 check清理数据:
tiup bench tpcc --warehouses 4 cleanup
当需要测试大数据集时,直接写入数据通常较慢,此时可以使用如下命令生成 CSV 数据集,然后通过 TiDB Lightning 导入数据。
生成 CSV 文件:
tiup bench tpcc --warehouses 4 prepare --output-dir data --output-type=csv为指定的表生成 CSV 文件:
tiup bench tpcc --warehouses 4 prepare --output-dir data --output-type=csv --tables history,orders
使用 TiUP 运行 TPC-H 测试
TiUP bench 组件支持如下运行 TPC-H 测试的命令和参数:
Available Commands:
  cleanup     清除数据
  prepare     准备数据
  run         开始压测
Flags:
      --check            检查输出数据,只有 scale 因子为 1 时有效
  -h, --help             tpch 的帮助信息
      --queries string   所有的查询语句 (默认 "q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,q13,q14,q15,q16,q17,q18,q19,q20,q21,q22")
      --sf int           scale 因子
TPC-H 测试步骤
准备数据:
tiup bench tpch --sf=1 prepare运行 TPC-H 测试,根据是否检查结果执行相应命令:
检查结果:
tiup bench tpch --count=22 --sf=1 --check=true run不检查结果:
tiup bench tpch --count=22 --sf=1 run
清理数据:
tiup bench tpch cleanup
使用 TiUP 运行 YCSB 测试
你可以使用 TiUP 对 TiDB 和 TiKV 节点分别进行 YCSB 测试。
测试 TiDB
准备数据:
tiup bench ycsb load tidb -p tidb.instances="127.0.0.1:4000" -p recordcount=10000运行 YCSB 测试:
# 默认读写比例为 95:5 tiup bench ycsb run tidb -p tidb.instances="127.0.0.1:4000" -p operationcount=10000
测试 TiKV
准备数据:
tiup bench ycsb load tikv -p tikv.pd="127.0.0.1:2379" -p recordcount=10000运行 YCSB 测试:
# 默认读写比例为 95:5 tiup bench ycsb run tikv -p tikv.pd="127.0.0.1:2379" -p operationcount=10000
使用 TiUP 运行 RawSQL 测试
你可以将 OLAP 查询写到 SQL 文件中,通过 tiup bench rawsql 执行测试,步骤如下:
准备数据和需要执行的查询:
-- 准备数据 CREATE TABLE t (a int); INSERT INTO t VALUES (1), (2), (3); -- 构造查询,保存为 demo.sql SELECT a, sleep(rand()) FROM t WHERE a < 4*rand();运行 RawSQL 测试:
tiup bench rawsql run --count 60 --query-files demo.sql