前言

SysBench是一个常用的基准测试和压力测试工具,广泛用于测试各种系统的性能。在本文中,我们将介绍SysBench的工作原理和用法,以及如何使用它进行基准测试和压力测试。我们还将讨论SysBench的各种测试模式,包括CPU、内存、文件IO和数据库测试。通过本文,您将学会如何使用SysBench评估系统的性能,并通过分析测试结果来进行性能调优。

关于SysBench

SysBench是一个用于系统性能基准测试和负载生成的开源工具。它支持多种测试模式,包括CPU、内存、I/O、线程和数据库性能测试。

SysBench最初是由Alexey Kopytov编写的,其目的是为了测试MySQL数据库的性能。现在,SysBench已经扩展到支持其他数据库和系统组件的性能测试,例如CPU、内存、磁盘I/O等。

SysBench具有可扩展性和灵活性,可以通过编写脚本来自定义测试。它还提供了多种不同的输出格式和结果分析工具,以便于用户分析和理解测试结果。

安装SysBench

  1. yum安装

    curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh | sudo bash
    
  2. 可以看到一些lua脚本

    [root@localhost ~]# ll /usr/share/sysbench/
    总用量 60
    -rwxr-xr-x. 1 root root  1452 4月  25 2020 bulk_insert.lua
    -rw-r--r--. 1 root root 14369 4月  25 2020 oltp_common.lua
    -rwxr-xr-x. 1 root root  1290 4月  25 2020 oltp_delete.lua
    -rwxr-xr-x. 1 root root  2415 4月  25 2020 oltp_insert.lua
    -rwxr-xr-x. 1 root root  1265 4月  25 2020 oltp_point_select.lua
    -rwxr-xr-x. 1 root root  1649 4月  25 2020 oltp_read_only.lua
    -rwxr-xr-x. 1 root root  1824 4月  25 2020 oltp_read_write.lua
    -rwxr-xr-x. 1 root root  1118 4月  25 2020 oltp_update_index.lua
    -rwxr-xr-x. 1 root root  1127 4月  25 2020 oltp_update_non_index.lua
    -rwxr-xr-x. 1 root root  1440 4月  25 2020 oltp_write_only.lua
    -rwxr-xr-x. 1 root root  1919 4月  25 2020 select_random_points.lua
    -rwxr-xr-x. 1 root root  2118 4月  25 2020 select_random_ranges.lua
    

测试

磁盘IO性能测试

[root@localhost ~]# sysbench fileio --threads=4 --file-total-size=1G --file-test-mode=rndrw prepare
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

128 files, 8192Kb each, 1024Mb total
Creating files for the test...
Extra file open flags: (none)
Reusing existing file test_file.0
Reusing existing file test_file.1
Reusing existing file test_file.2
Reusing existing file test_file.3
Reusing existing file test_file.4
Reusing existing file test_file.5
Reusing existing file test_file.6
Reusing existing file test_file.7
Reusing existing file test_file.8
Reusing existing file test_file.9
Reusing existing file test_file.10
Reusing existing file test_file.11
Reusing existing file test_file.12
Reusing existing file test_file.13
Reusing existing file test_file.14
Reusing existing file test_file.15
Reusing existing file test_file.16
Reusing existing file test_file.17
Reusing existing file test_file.18
Reusing existing file test_file.19
Reusing existing file test_file.20
Reusing existing file test_file.21
Reusing existing file test_file.22
Reusing existing file test_file.23
Reusing existing file test_file.24
Reusing existing file test_file.25
Reusing existing file test_file.26
Reusing existing file test_file.27
Reusing existing file test_file.28
Reusing existing file test_file.29
Reusing existing file test_file.30
Reusing existing file test_file.31
Reusing existing file test_file.32
Reusing existing file test_file.33
Reusing existing file test_file.34
Reusing existing file test_file.35
Reusing existing file test_file.36
Reusing existing file test_file.37
Reusing existing file test_file.38
Reusing existing file test_file.39
Reusing existing file test_file.40
Reusing existing file test_file.41
Reusing existing file test_file.42
Reusing existing file test_file.43
Reusing existing file test_file.44
Reusing existing file test_file.45
Reusing existing file test_file.46
Reusing existing file test_file.47
Reusing existing file test_file.48
Reusing existing file test_file.49
Reusing existing file test_file.50
Reusing existing file test_file.51
Reusing existing file test_file.52
Reusing existing file test_file.53
Reusing existing file test_file.54
Reusing existing file test_file.55
Reusing existing file test_file.56
Reusing existing file test_file.57
Reusing existing file test_file.58
Reusing existing file test_file.59
Reusing existing file test_file.60
Reusing existing file test_file.61
Reusing existing file test_file.62
Reusing existing file test_file.63
Reusing existing file test_file.64
Reusing existing file test_file.65
Reusing existing file test_file.66
Reusing existing file test_file.67
Reusing existing file test_file.68
Reusing existing file test_file.69
Reusing existing file test_file.70
Reusing existing file test_file.71
Reusing existing file test_file.72
Reusing existing file test_file.73
Reusing existing file test_file.74
Reusing existing file test_file.75
Reusing existing file test_file.76
Reusing existing file test_file.77
Reusing existing file test_file.78
Reusing existing file test_file.79
Reusing existing file test_file.80
Reusing existing file test_file.81
Reusing existing file test_file.82
Reusing existing file test_file.83
Reusing existing file test_file.84
Reusing existing file test_file.85
Reusing existing file test_file.86
Reusing existing file test_file.87
Reusing existing file test_file.88
Reusing existing file test_file.89
Reusing existing file test_file.90
Reusing existing file test_file.91
Reusing existing file test_file.92
Reusing existing file test_file.93
Reusing existing file test_file.94
Reusing existing file test_file.95
Reusing existing file test_file.96
Reusing existing file test_file.97
Reusing existing file test_file.98
Reusing existing file test_file.99
Reusing existing file test_file.100
Reusing existing file test_file.101
Reusing existing file test_file.102
Reusing existing file test_file.103
Reusing existing file test_file.104
Reusing existing file test_file.105
Reusing existing file test_file.106
Reusing existing file test_file.107
Reusing existing file test_file.108
Reusing existing file test_file.109
Reusing existing file test_file.110
Reusing existing file test_file.111
Reusing existing file test_file.112
Reusing existing file test_file.113
Reusing existing file test_file.114
Reusing existing file test_file.115
Reusing existing file test_file.116
Reusing existing file test_file.117
Reusing existing file test_file.118
Reusing existing file test_file.119
Reusing existing file test_file.120
Reusing existing file test_file.121
Reusing existing file test_file.122
Reusing existing file test_file.123
Reusing existing file test_file.124
Reusing existing file test_file.125
Reusing existing file test_file.126
Reusing existing file test_file.127
No bytes written.

[root@localhost ~]# sysbench fileio --threads=4 --file-total-size=1G --file-test-mode=rndrw run
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

Running the test with following options:
Number of threads: 4
Initializing random number generator from current time


Extra file open flags: (none)
128 files, 8MiB each
1GiB total file size
Block size 16KiB
Number of IO requests: 0
Read/Write ratio for combined random IO test: 1.50
Periodic FSYNC enabled, calling fsync() each 100 requests.
Calling fsync() at the end of test, Enabled.
Using synchronous I/O mode
Doing random r/w test
Initializing worker threads...

Threads started!


File operations:
    reads/s:                      5635.88
    writes/s:                     3757.26
    fsyncs/s:                     12067.96

Throughput:
    read, MiB/s:                  88.06
    written, MiB/s:               58.71

General statistics:
    total time:                          10.1013s
    total number of events:              216312

Latency (ms):
         min:                                    0.00
         avg:                                    0.18
         max:                                  180.36
         95th percentile:                        0.49
         sum:                                39831.28

Threads fairness:
    events (avg/stddev):           54078.0000/681.46
    execution time (avg/stddev):   9.9578/0.01

CPU测试

[root@localhost ~]#  [root@localhost ~]# sysbench cpu --cpu-max-prime=100000 run
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

Running the test with following options:
Number of threads: 1
Initializing random number generator from current time


Prime numbers limit: 100000

Initializing worker threads...

Threads started!

CPU speed:
    events per second:    39.98

General statistics:
    total time:                          10.0025s
    total number of events:              400

Latency (ms):
         min:                                   23.92
         avg:                                   25.00
         max:                                   31.36
         95th percentile:                       26.20
         sum:                                 9998.35

Threads fairness:
    events (avg/stddev):           400.0000/0.00
    execution time (avg/stddev):   9.9984/0.00

线程测试

[root@localhost ~]# sysbench threads --threads=32 --thread-yields=48 --thread-locks=2 run
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

Running the test with following options:
Number of threads: 32
Initializing random number generator from current time


Initializing worker threads...

Threads started!


General statistics:
    total time:                          10.0033s
    total number of events:              111275

Latency (ms):
         min:                                    0.03
         avg:                                    2.88
         max:                                   97.06
         95th percentile:                       16.12
         sum:                               319937.48

Threads fairness:
    events (avg/stddev):           3477.3438/227.18
    execution time (avg/stddev):   9.9980/0.00

内存测试

[root@localhost ~]# sysbench memory --memory-block-size=8k --memory-total-size=1G run
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

Running the test with following options:
Number of threads: 1
Initializing random number generator from current time


Running memory speed test with the following options:
  block size: 8KiB
  total size: 1024MiB
  operation: write
  scope: global

Initializing worker threads...

Threads started!

Total operations: 131072 (1462500.38 per second)

1024.00 MiB transferred (11425.78 MiB/sec)


General statistics:
    total time:                          0.0874s
    total number of events:              131072

Latency (ms):
         min:                                    0.00
         avg:                                    0.00
         max:                                    0.31
         95th percentile:                        0.00
         sum:                                   69.56

Threads fairness:
    events (avg/stddev):           131072.0000/0.00
    execution time (avg/stddev):   0.0696/0.00