<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          最牛逼的數(shù)據(jù)庫性能測試工具,沒有之一!

          共 22428字,需瀏覽 45分鐘

           ·

          2021-04-12 14:16

          點擊關(guān)注公眾號,Java干貨及時送達

          作者:駿馬金龍
          出處:www.cnblogs.com/f-ck-need-u/p/9279703.html

          1.簡介和安裝

          sysbench是一個很不錯的數(shù)據(jù)庫性能測試工具。

          官方站點:

          https://github.com/akopytov/sysbench/

          rpm包下載:

          https://packagecloud.io/akopytov/sysbench/packages/el/7/sysbench-1.0.15-1.el7.centos.x86_64.rpm

          源碼包下載:

          https://github.com/akopytov/sysbench/archive/1.0.15.tar.gz

          如果是編譯安裝,需要先安裝好mysql的開發(fā)包(盡管編譯錯誤時提示的是缺少Mysql庫文件)。

          yum -y install mysql-community-devel

          tar xf 1.0.15.tar.gz
          cd sysbench-1.0.15
          ./autogen.sh
          ./configure
          make -j
          make install

          安裝后,只有一個二進制文件sysbench,還提供了很多個lua腳本。

          [root@s1 ~]# rpm -ql sysbench | grep 'bin\|lua'  
          /usr/bin/sysbench
          /usr/share/sysbench/bulk_insert.lua
          /usr/share/sysbench/oltp_common.lua
          /usr/share/sysbench/oltp_delete.lua
          /usr/share/sysbench/oltp_insert.lua
          /usr/share/sysbench/oltp_point_select.lua
          /usr/share/sysbench/oltp_read_only.lua
          /usr/share/sysbench/oltp_read_write.lua
          /usr/share/sysbench/oltp_update_index.lua
          /usr/share/sysbench/oltp_update_non_index.lua
          /usr/share/sysbench/oltp_write_only.lua
          /usr/share/sysbench/select_random_points.lua
          /usr/share/sysbench/select_random_ranges.lua
          /usr/share/sysbench/tests/include/inspect.lua
          /usr/share/sysbench/tests/include/oltp_legacy/bulk_insert.lua
          /usr/share/sysbench/tests/include/oltp_legacy/common.lua
          /usr/share/sysbench/tests/include/oltp_legacy/delete.lua
          /usr/share/sysbench/tests/include/oltp_legacy/insert.lua
          /usr/share/sysbench/tests/include/oltp_legacy/oltp.lua
          /usr/share/sysbench/tests/include/oltp_legacy/oltp_simple.lua
          /usr/share/sysbench/tests/include/oltp_legacy/parallel_prepare.lua
          /usr/share/sysbench/tests/include/oltp_legacy/select.lua
          /usr/share/sysbench/tests/include/oltp_legacy/select_random_points.lua
          /usr/share/sysbench/tests/include/oltp_legacy/select_random_ranges.lua
          /usr/share/sysbench/tests/include/oltp_legacy/update_index.lua
          /usr/share/sysbench/tests/include/oltp_legacy/update_non_index.lua

          本文介紹的是新版本sysbench oltp lua腳本的用法(/usr/share/sysbench/*.lua),所以不涉及傳統(tǒng)的lua(tests/include/oltp_legacy/*.lua),如果想要了解這些傳統(tǒng)Lua腳本的用法,網(wǎng)上隨便找。常用的 7 款 MySQL 客戶端工具,也推薦看下。

          2.sysbench使用方法

          以下列出了sysbench對測試mysql時常用的選項。

          [root@xuexi ~]# sysbench --help
          Usage:
            sysbench [options]... [test_lua] [lua_options] [command]

          Commands implemented by most tests: prepare run cleanup help

          通用選項:以下中括號中的值表示默認值
            --threads=N                     指定線程數(shù)[1]
            --events=N                      限制最大請求數(shù),0表示不限制[0]
            --time=N                        限制最長執(zhí)行時間,0表示不限制[10]
                              --events和--time二者選一即可
            --forced-shutdown=STRING        達到最長執(zhí)行時間后還需等待多久關(guān)閉sysbench
                                            off表示禁用該功能[off]
            --thread-stack-size=SIZE        每個線程使用的棧空間大小[64K]
            --rate=N                        平均事務(wù)處理速率,0表示不限制[0]
            --report-interval=N             每隔幾秒報告一次結(jié)果,0表示禁用間隔報告[0]
            --config-file=FILENAME          從文件中讀取命令行選項
            --tx-rate=N                     已廢棄,是--rate的別名[0]
            --max-requests=N                已廢棄,是--events的別名[0]
            --max-time=N                    已廢棄,是--time的別名[0]
            --num-threads=N                 已廢棄,是--threads的別名[1]
            --db-ps-mode=STRING             是否使用prepare模式的語句 {auto, disable} [auto]

          mysql相關(guān)選項:
            --mysql-host=[LIST,...]          MySQL server host [localhost]
            --mysql-port=[LIST,...]          MySQL server port [3306]
            --mysql-socket=[LIST,...]        MySQL socket
            --mysql-user=STRING              MySQL user [sbtest]
            --mysql-password=STRING          MySQL password []
            --mysql-db=STRING                MySQL database name [sbtest]
            --mysql-ignore-errors=[LIST,...] 要忽略的錯誤代碼,值可為"all" [1213,1020,1205]

          Compiled-in tests:
            fileio - File I/O test
            cpu - CPU performance test
            memory - Memory functions speed test
            threads - Threads subsystem performance test
            mutex - Mutex performance test

          其中,command部分有4類:prepare run cleanup和help:

          • prepare:準(zhǔn)備數(shù)據(jù)的命令。例如,在sysbench壓力測試之前,需要先準(zhǔn)備好測試庫、測試表以及測試表中的數(shù)據(jù)。具體用法見后文。
          • run:表示進行壓力測試。
          • cleanup:清除測試時產(chǎn)生的數(shù)據(jù)。
          • help:輸出給定lua腳本的幫助信息。

          test_lua是想要使用的lua腳本,如果是rpm包安裝的sysbench,則這些腳本都是/usr/share/sysbench目錄下。對于一般的數(shù)據(jù)庫測試,只需使用和oltp有關(guān)的lua腳本就足夠。

          options和lua_options是不同的,options是sysbench的選項,lua_options是lua腳本的選項,lua_options應(yīng)該要放在test_lua的后面(非必需,但建議)。MySQL 系列面試題及答案我都整理好了,關(guān)注公眾號Java技術(shù)棧回復(fù)面試獲取。

          例如,要查看oltp_common.lua的用法,可以:

          sysbench /usr/share/sysbench/oltp_common.lua help

          3.準(zhǔn)備測試數(shù)據(jù)

          首先創(chuàng)建sysbench所需數(shù)據(jù)庫sbtest(這是sysbench默認使用的庫名,必須創(chuàng)建測試庫)。

          mysqladmin -h127.0.0.1 -uroot -pP@ssword1! -P3306 create sbtest;

          然后,準(zhǔn)備測試所用的表,這些測試表放在測試庫sbtest中。這里使用的lua腳本為/usr/share/sysbench/oltp_common.lua

          sysbench --mysql-host=127.0.0.1 \
                   --mysql-port=3306 \
                   --mysql-user=root \
                   --mysql-password=P@ssword1! \
                   /usr/share/sysbench/oltp_common.lua \
                   --tables=10 \
                   --table_size=100000 \
                   prepare

          其中--tables=10表示創(chuàng)建10個測試表,--table_size=100000表示每個表中插入10W行數(shù)據(jù),prepare表示這是準(zhǔn)備數(shù)的過程。

          mysql> show tables from sbtest;
          +------------------+
          | Tables_in_sbtest |
          +------------------+
          | sbtest1 |
          | sbtest10 |
          | sbtest2 |
          | sbtest3 |
          | sbtest4 |
          | sbtest5 |
          | sbtest6 |
          | sbtest7 |
          | sbtest8 |
          | sbtest9 |
          +------------------+

          mysql> select count(*) from sbtest.sbtest1;
          +----------+
          | count(*) |
          +----------+
          | 100000 |
          +----------+

          如果想要清除這10個表,可使用cleanup命令。

          sysbench --mysql-host=127.0.0.1 \
                   --mysql-port=3306 \
                   --mysql-user=root \
                   --mysql-password=P@ssword1! \
                   /usr/share/sysbench/oltp_common.lua \
                   --tables=10 \
                   cleanup

          4.數(shù)據(jù)庫測試和結(jié)果分析

          稍微修改下之前準(zhǔn)備數(shù)據(jù)的語句,就可以拿來測試了。

          需要注意的是,之前使用的lua腳本為oltp_common.lua,它是一個通用腳本,是被其它lua腳本調(diào)用的,它不能直接拿來測試。

          所以,我這里用oltp_read_write.lua腳本來做讀、寫測試。還有很多其它類型的測試,比如只讀測試、只寫測試、刪除測試、大批量插入測試等等。可找到對應(yīng)的lua腳本進行調(diào)用即可。

          sysbench --threads=4 \
                   --time=20 \
                   --report-interval=5 \
                   --mysql-host=127.0.0.1 \
                   --mysql-port=3306 \
                   --mysql-user=root \
                   --mysql-password=P@ssword1! \
                   /usr/share/sysbench/oltp_read_write.lua \
                   --tables=10 \
                   --table_size=100000 \
                   run

          以下是測試返回的結(jié)果:

          Initializing worker threads...

          Threads started!


          ####以下是每5秒返回一次的結(jié)果,統(tǒng)計的指標(biāo)包括:
          #### 線程數(shù)、tps(每秒事務(wù)數(shù))、qps(每秒查詢數(shù))、
          #### 每秒的讀/寫/其它次數(shù)、延遲、每秒錯誤數(shù)、每秒重連次數(shù)
          [ 5s ] thds: 4 tps: 130.16 qps: 2606.30 (r/w/o: 1824.51/520.66/261.13) lat (ms,95%): 104.84 err/s: 0.00 reconn/s: 0.00
          [ 10s ] thds: 4 tps: 126.74 qps: 2539.17 (r/w/o: 1778.17/507.52/253.47) lat (ms,95%): 108.68 err/s: 0.00 reconn/s: 0.00
          [ 15s ] thds: 4 tps: 136.54 qps: 2736.34 (r/w/o: 1915.25/548.01/273.07) lat (ms,95%): 102.97 err/s: 0.00 reconn/s: 0.00
          [ 20s ] thds: 4 tps: 107.44 qps: 2148.65 (r/w/o: 1505.60/428.17/214.89) lat (ms,95%): 132.49 err/s: 0.00 reconn/s: 0.00

          SQL statistics:
              queries performed:
                  read:          35098   # 執(zhí)行的讀操作數(shù)量
                  write:         10028   # 執(zhí)行的寫操作數(shù)量
                  other:         5014    # 執(zhí)行的其它操作數(shù)量
                  total:         50140
              transactions:      2507   (124.29 per sec.)    # 執(zhí)行事務(wù)的平均速率
              queries:           50140  (2485.82 per sec.)   # 平均每秒能執(zhí)行多少次查詢
              ignored errors:    0      (0.00 per sec.)
              reconnects:        0      (0.00 per sec.)

          General statistics:
              total time:                  20.1694s     # 總消耗時間
              total number of events:      2507         # 總請求數(shù)量(讀、寫、其它)

          Latency (ms):
                   min:                            2.32
                   avg:                           32.13
                   max:                          575.78
                   95th percentile:              118.92    # 采樣計算的平均延遲
                   sum:                        80554.96

          Threads fairness:
              events (avg/stddev):           626.7500/2.49
              execution time (avg/stddev):   20.1387/0.04

          5.cpu/io/內(nèi)存等測試

          sysbench內(nèi)置了幾個測試指標(biāo)。

          Compiled-in tests:
            fileio - File I/O test
            cpu - CPU performance test
            memory - Memory functions speed test
            threads - Threads subsystem performance test
            mutex - Mutex performance test

          可直接help輸出測試方法。例如,fileio測試。

          [root@xuexi ~]# sysbench fileio help
          sysbench 1.0.15 (using bundled LuaJIT 2.1.0-beta2)

          fileio options:
            --file-num=N                  number of files to create [128]
            --file-block-size=N           block size to use in all IO operations [16384]
            --file-total-size=SIZE        total size of files to create [2G]
            --file-test-mode=STRING       test mode {seqwr, seqrewr, seqrd, rndrd, rndwr, rndrw}
            --file-io-mode=STRING         file operations mode {sync,async,mmap} [sync]
            --file-async-backlog=N        number of asynchronous operatons to queue per thread [128]
            --file-extra-flags=[LIST,...] list of additional flags to use to open files {sync,dsync,direct} []
            --file-fsync-freq=N           do fsync() after this number of requests (0 - don't use fsync()) [100]
            --file-fsync-all[=on|off]     do fsync() after each write operation [off]
            --file-fsync-end[=on|off]     do fsync() at the end of test [on]
            --file-fsync-mode=STRING      which method to use for synchronization {fsync, fdatasync} [fsync]
            --file-merged-requests=N      merge at most this number of IO requests if possible (0 - don'
          t merge) [0]
            --file-rw-ratio=N             reads/writes ratio for combined test [1.5]

          例如,創(chuàng)建5個文件,總共1G,每個文件大概200M。

          sysbench fileio --file-num=5 --file-total-size=1G prepare

          [root@xuexi ~]# ls -lh test*
          -rw------- 1 root root 205M Jul  8 12:15 test_file.0
          -rw------- 1 root root 205M Jul  8 12:15 test_file.1
          -rw------- 1 root root 205M Jul  8 12:15 test_file.2
          -rw------- 1 root root 205M Jul  8 12:15 test_file.3
          -rw------- 1 root root 205M Jul  8 12:15 test_file.4

          然后,運行測試。

          sysbench --events=5000 \
                   --threads=16 \
                   fileio \
                   --file-num=5 \
                   --file-total-size=1G \
                   --file-test-mode=rndrw \
                   --file-fsync-freq=0 \
                   --file-block-size=16384 \
                   run

          結(jié)果:

          File operations:
              reads/s:                      98.67
              writes/s:                     66.85
              fsyncs/s:                     6.26

          Throughput:     # 吞吐量
              read, MiB/s:                  1.54  # 表示讀的帶寬
              written, MiB/s:               1.04  # 表示讀的帶寬

          General statistics:
              total time:                   12.7426s
              total number of events:       2117

          Latency (ms):
                   min:                          0.00
                   avg:                         86.66
                   max:                       2919.41
                   95th percentile:            646.19
                   sum:                     183460.80

          Threads fairness:
              events (avg/stddev):          132.3125/24.19
              execution time (avg/stddev):  11.4663/1.09

          再比例cpu性能測試:

          [root@xuexi ~]# sysbench cpu --threads=40 --events=10000 --cpu-max-prime=20000 run
          sysbench 1.0.15 (using bundled LuaJIT 2.1.0-beta2)

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


          Prime numbers limit: 20000

          Initializing worker threads...

          Threads started!

          CPU speed:
              events per second:  2127.81

          General statistics:
              total time:                          4.6986s
              total number of events:              10000

          Latency (ms):
                   min:                                    1.72
                   avg:                                   18.16
                   max:                                  302.17
                   95th percentile:                      110.66
                   sum:                               181628.49

          Threads fairness:
              events (avg/stddev):           250.0000/30.81
              execution time (avg/stddev):   4.5407/0.10







          關(guān)注Java技術(shù)棧看更多干貨



          獲取 Spring Boot 實戰(zhàn)筆記!
          瀏覽 40
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲不卡一区二区三区 | 激情日逼| 成人国产片女人爽到高潮 | 国产亚洲精品美女 | 午夜三级电影 |