使用支持 Lua 的 sysbench

对于 MySQL 来说,一个相当常见的基准测试工具是 sysbench。它是由 Alexey Kopytov 在将近 10 年前编写的。

Sysbench 有多种模式,可以用来测试原始 CPU 性能、互斥锁速度、调度器开销和文件 IO 性能。可能最常用的 sysbench 模式是 OLTP。这个基准测试模仿了一个 OLTP 场景,其中小事务命中一个优化的数据库。有很多变量可以调整,最重要的是模拟的应用线程数量(选项 --num-threads)。OLTP 基准测试可以以只读模式运行,此时每个事务执行 14 个 SELECT 查询。或者可以以读写模式运行,此时会额外增加 2 个 UPDATE 和一个 INSERT 和 DELETE。

这个官方 sysbench 树的最新版本是 0.4.12。许多 Linux 发行版都提供了它的软件包。

然而,还有一个更新版本的 sysbench,版本号为 0.5。
它只能从 Launchpad 的源代码树获取。构建它很简单

bzr branch lp:sysbench sysbench-trunk
cd sysbench-trunk
./autogen.sh
./configure
make

sysbench-0.5 中最重要的变化是 OLTP 基准测试现在是用 Lua 编写的。Lua 脚本可以在 sysbench 源代码目录下的 sysbench/tests/db 中找到。现在要运行 OLTP 基准测试,你需要提供要运行的 Lua 脚本的路径

sysbench --test=.../tests/db/oltp.lua ... prepare
sysbench --test=.../tests/db/oltp.lua ... run

通过编写自己的 Lua 脚本,你现在可以使用同一个工具运行任意工作负载!但这还不止如此。

sysbench-0.5 的一个新选项是 --report-interval。如果你将此选项设置为非零(默认值),则 sysbench 将每隔 –report-interval 秒报告中间状态消息。这对于检测不规律的行为(例如写入停顿)非常有帮助。

下面是一个示例会话

sysbench-trunk/sysbench$ ./sysbench --test=tests/db/oltp.lua 
 --mysql-socket=/tmp/mysql.sock --mysql-user=root --num-threads=4 
 --max-time=30 --report-interval=5 run

sysbench 0.5:  multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 4
Report intermediate results every 5 second(s)
Random number generator seed is 0 and will be ignored

Threads started!

[   5s] threads: 4, tps: 345.89, reads/s: 4847.81, writes/s: 1384.35, response time: 17.59ms (95%)
[  10s] threads: 4, tps: 338.20, reads/s: 4738.40, writes/s: 1354.40, response time: 17.89ms (95%)
[  15s] threads: 4, tps: 342.20, reads/s: 4792.01, writes/s: 1368.80, response time: 17.97ms (95%)
[  20s] threads: 4, tps: 338.40, reads/s: 4738.59, writes/s: 1354.40, response time: 17.96ms (95%)
[  25s] threads: 4, tps: 333.40, reads/s: 4664.98, writes/s: 1331.99, response time: 17.91ms (95%)
OLTP test statistics:
    queries performed:
        read:                            141862
        write:                           40532
        other:                           20266
        total:                           202660
    transactions:                        10133  (338.75 per sec.)
    deadlocks:                           0      (0.00 per sec.)
    read/write requests:                 182394 (6097.54 per sec.)
    other operations:                    20266  (677.50 per sec.)

General statistics:
    total time:                          29.9127s
    total number of events:              10133
    total time taken by event execution: 119.5746s
    response time:
         min:                                  4.14ms
         avg:                                 11.80ms
         max:                                 43.79ms
         approx.  95 percentile:              17.90ms

Threads fairness:
    events (avg/stddev):           2533.2500/5.76
    execution time (avg/stddev):   29.8936/0.00

事物总是有利有弊。在 sysbench 的例子中,这意味着 sysbench 本身现在会消耗更多的 CPU 周期。也就是说,如果你运行等效的 OLTP 基准测试,sysbench-0.5 报告的数值会比 sysbench-0.4.12 小。原因之一是 sysbench 本身增加了延迟,另一个原因是竞争 CPU 周期(假设你在与你测试的数据库服务器同一台机器上运行 sysbench)。

最终,这只意味着 sysbench-0.4 和 sysbench-0.5 的结果是不可比较的。在我看来,对于新的灵活性来说,这是一个小小的代价。

未完待续….