前言
kbbench是一种在KingbaseES上运行基准测试的简单程序。它可能在并发的数据库会话中一遍一遍地运行相同序列的 SQL 命令,并且计算平均事务率(每秒的事务数)。默认情况下,kbbench会测试一种基于 TPC-B 但是要更宽松的场景,其中在每个事务中涉及五个SELECT、UPDATE以及INSERT命令。但是,通过编写自己的事务脚本文件很容易用来测试其他情况。
本文记录kbbench的参数介绍以及使用kbbench进行简单的数据库压力测试
参数介绍
直接通过help命令查看
[cli@localhost bin]$ ./kbbench --help kbbench is a benchmarking tool for Kingbase. Usage: kbbench [OPTION]... [DBNAME] Initialization options: -i, --initialize invokes initialization mode -F, --fillfactor=NUM set fill factor -n, --no-vacuum do not run VACUUM after initialization -q, --quiet quiet logging (one message each 5 seconds) -s, --scale=NUM scaling factor --foreign-keys create foreign key constraints between tables --index-tablespace=TABLESPACE create indexes in the specified tablespace --tablespace=TABLESPACE create tables in the specified tablespace --unlogged-tables create tables as unlogged tables Options to select what to run: -b, --builtin=NAME[@W] add builtin script NAME weighted at W (default: 1) (use "-b list" to list available scripts) -f, --file=FILENAME[@W] add script FILENAME weighted at W (default: 1) -N, --skip-some-updates skip updates of kbbench_tellers and kbbench_branches (same as "-b simple-update") -S, --select-only perform SELECT-only transactions (same as "-b select-only") Benchmarking options: -c, --client=NUM number of concurrent database clients (default: 1) -C, --connect establish new connection for each transaction -D, --define=VARNAME=VALUE define variable for use by custom script -j, --jobs=NUM number of threads (default: 1) -l, --log write transaction times to log file -L, --latency-limit=NUM count transactions lasting more than NUM ms as late -M, --protocol=simple|extended|prepared protocol for submitting queries (default: simple) -n, --no-vacuum do not run VACUUM before tests -P, --progress=NUM show thread progress report every NUM seconds -r, --report-latencies report average latency per command -R, --rate=NUM target rate in transactions per second -s, --scale=NUM report this scale factor in output -t, --transactions=NUM number of transactions each client runs (default: 10) -T, --time=NUM duration of benchmark test in seconds -v, --vacuum-all vacuum all four standard tables before tests --aggregate-interval=NUM aggregate data over NUM seconds --progress-timestamp use Unix epoch timestamps for progress --sampling-rate=NUM fraction of transactions to log (e.g., 0.01 for 1%) Common options: -d, --debug print debugging output -h, --host=HOSTNAME database server host or socket directory -p, --port=PORT database server port number -U, --username=USERNAME connect as specified database user -V, --version output version information, then exit -?, --help show this help, then exit Report bugs to <kingbase-bugs@kingbase.com.cn>
数据准备
使用命令:kbbench -i [ other-options ] dbname
如:
./kbbench -USYSTEM -p50001 -i TEST -s20
-i
--initialize
要求调用初始化模式。
-s
-- scale_factor
--scale=scale_factor
将生成的行数乘以比例因子。例如,-s 100将在kbbench_accounts表中创建 10,000,000 行。默认为 1。当比例为 20,000 或更高时,用来保存账号标识符的列(aid列)将切换到使用更大的整数(bigint),这样才能足以保存账号标识符。
[cli@localhost bin]$ ./kbbench -USYSTEM -p50001 -i TEST -s20 Password: creating tables... 100000 of 2000000 tuples (5%) done (elapsed 0.27 s, remaining 5.17 s) 200000 of 2000000 tuples (10%) done (elapsed 0.54 s, remaining 4.85 s) 300000 of 2000000 tuples (15%) done (elapsed 0.82 s, remaining 4.64 s) 400000 of 2000000 tuples (20%) done (elapsed 1.13 s, remaining 4.50 s) 500000 of 2000000 tuples (25%) done (elapsed 1.41 s, remaining 4.23 s) 600000 of 2000000 tuples (30%) done (elapsed 1.66 s, remaining 3.88 s) 700000 of 2000000 tuples (35%) done (elapsed 1.96 s, remaining 3.64 s) 800000 of 2000000 tuples (40%) done (elapsed 2.22 s, remaining 3.32 s) 900000 of 2000000 tuples (45%) done (elapsed 2.50 s, remaining 3.06 s) 1000000 of 2000000 tuples (50%) done (elapsed 2.78 s, remaining 2.78 s) 1100000 of 2000000 tuples (55%) done (elapsed 3.07 s, remaining 2.51 s) 1200000 of 2000000 tuples (60%) done (elapsed 3.38 s, remaining 2.25 s) 1300000 of 2000000 tuples (65%) done (elapsed 3.67 s, remaining 1.97 s) 1400000 of 2000000 tuples (70%) done (elapsed 4.04 s, remaining 1.73 s) 1500000 of 2000000 tuples (75%) done (elapsed 4.31 s, remaining 1.44 s) 1600000 of 2000000 tuples (80%) done (elapsed 4.56 s, remaining 1.14 s) 1700000 of 2000000 tuples (85%) done (elapsed 4.85 s, remaining 0.86 s) 1800000 of 2000000 tuples (90%) done (elapsed 5.19 s, remaining 0.58 s) 1900000 of 2000000 tuples (95%) done (elapsed 5.53 s, remaining 0.29 s) 2000000 of 2000000 tuples (100%) done (elapsed 5.82 s, remaining 0.00 s) vacuum... set primary keys... done.
执行完成后,进入测试库TEST,发现创建了4个表:
TEST=# dt List of relations Schema | Name | Type | Owner --------+------------------+-------+-------- PUBLIC | KBBENCH_ACCOUNTS | table | SYSTEM PUBLIC | KBBENCH_BRANCHES | table | SYSTEM PUBLIC | KBBENCH_HISTORY | table | SYSTEM PUBLIC | KBBENCH_TELLERS | table | SYSTEM
压力测试
使用命令:
./kbbench -USYSTEM -p50001 -r -j 2 -c 4 -T 30 TEST
-r
--report-latencies
在基准结束后,报告平均的每个命令的每语句等待时间(从客户端的角度来说是执行时间)。
-j
threads
--jobs=threads
pgbench中的工作者线程数量。在多 CPU 机器上使用多于一个线程会有用。客户端会尽可能均匀地分布到可用的线程上。默认为 1。
-c
clients
--client=clients
模拟的客户端数量,也就是并发数据库会话数量。默认为 1。
-T
seconds
--time=seconds
运行测试这么多秒,而不是为每个客户端运行固定数量的事务。-t和-T是互斥的。
[cli@localhost bin]$ ./kbbench -USYSTEM -p50001 -r -j 2 -c 4 -T 30 TEST Password: starting vacuum...end. transaction type: <builtin: TPC-B (sort of)> scaling factor: 20 query mode: simple number of clients: 4 number of threads: 2 duration: 30 s number of transactions actually processed: 24657 latency average = 4.867 ms tps = 821.800590 (including connections establishing) tps = 822.087874 (excluding connections establishing) script statistics: - statement latencies in milliseconds: 0.004 set aid random(1, 100000 * :scale) 0.001 set bid random(1, 1 * :scale) 0.001 set tid random(1, 10 * :scale) 0.001 set delta random(-5000, 5000) 0.299 BEGIN; 0.745 UPDATE kbbench_accounts SET abalance = abalance + :delta WHERE aid = :aid; 0.605 SELECT abalance FROM kbbench_accounts WHERE aid = :aid; 0.746 UPDATE kbbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid; 0.737 UPDATE kbbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid; 0.578 INSERT INTO kbbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP); 1.127 END;