sysbench的框架實現介紹

sysbench是一個很是經典的綜合性能測試工具,它支持CPU,IO,內存,尤爲是數據庫的性能測試。那它是怎麼作到通用性的呢,總結一句話是大量運用了重載的方法。數據庫

sysbench整體架構
/* 某個測試用例的總體結構 */
typedef struct sb_test
{
  const char        *sname;
  const char        *lname;
  /* 下面有具體說明 */
  sb_operations_t   ops;
  sb_builtin_cmds_t builtin_cmds;
  sb_arg_t          *args;

  sb_list_item_t    listitem;
} sb_test_t;
/* 某個測試用例的具體操做實現結構 */
typedef struct
{
  sb_op_init            *init;            /* initialization function */
  sb_op_prepare         *prepare;         /* called after timers start,  but
                                             before thread execution */
  sb_op_thread_init     *thread_init;     /* thread initialization
                                             (called when each thread starts) */
  sb_op_print_mode      *print_mode;      /* print mode function */
  sb_op_next_event      *next_event;      /* event generation function */
  sb_op_execute_event   *execute_event;   /* event execution function */
  sb_op_report          *report_intermediate; /* intermediate reports handler */
  sb_op_report          *report_cumulative;   /* cumulative reports handler */
  sb_op_thread_run      *thread_run;      /* main thread loop */
  sb_op_thread_done     *thread_done;     /* thread finalize function */
  sb_op_cleanup         *cleanup;         /* called after exit from thread,
                                             but before timers stop */ 
  sb_op_done            *done;            /* finalize function */
} sb_operations_t;
/* 某個測試用例的三階段實現結構 */
typedef struct
{
  sb_builtin_cmd_func_t *help;  /* print help */
  sb_builtin_cmd_func_t *prepare; /* prepare for the test */
  sb_builtin_cmd_func_t *run;   /* run the test */
  sb_builtin_cmd_func_t *cleanup; /* cleanup the test database, files, etc. */
} sb_builtin_cmds_t;
static sb_test_t cpu_test =
{
  .sname = "cpu", /*case簡稱*/
  .lname = "CPU performance test",/*case全稱*/
  .ops = {
    .init = cpu_init, /* 初始化case */
    .print_mode = cpu_print_mode, /* case啓動前,作說明 */
    .next_event = cpu_next_event, /* 拿到下一個event的數據 */
    .execute_event = cpu_execute_event, /* 具體執行這個event */
    .report_cumulative = cpu_report_cumulative,  /* 階段性報告輸出 */
    .done = cpu_done /* case結束後,處理乾淨 */
  },
  .args = cpu_args /*子case須要的參數說明*/
};複製代碼

那sysbench的完整流程是怎樣呢?黃色部分是測試用例須要實現的。
sysbench
上面struct裏面有個event概念,不一樣的測試event的定義都不同:好比CPU的測試case,一個event是完成求得小於某數(默認10000)的全部質數。好比fileio的測試case,一次read或者一次write操做就是一個event。
sysbench的線程介紹bash

  1. worker_thread具體實現是怎樣呢:欣賞下sysbench.c裏面某子線程是如何執行的,代碼很是清晰易懂:

static int thread_run(sb_test_t *test, int thread_id)
{
  sb_event_t        event;
  int               rc = 0;

  while (sb_more_events(thread_id) && rc == 0)
  {
    event = test->ops.next_event(thread_id);
    if (event.type == SB_REQ_TYPE_NULL)
      break;

    sb_event_start(thread_id);

    rc = test->ops.execute_event(&event, thread_id);

    sb_event_stop(thread_id);
  }

  return rc;
}複製代碼

  1. intermediate_report線程:週期性輸出性能數據,參數項爲:--report-interval=N,對CPU的測試用例舉例:sysbench cpu --report-interval=1,截取部分輸出結果以下:

Threads started!

[ 1s ] thds: 1 eps: 922.10 lat (ms,95%): 1.08
[ 2s ] thds: 1 eps: 925.19 lat (ms,95%): 1.08
[ 3s ] thds: 1 eps: 926.00 lat (ms,95%): 1.08
[ 4s ] thds: 1 eps: 926.00 lat (ms,95%): 1.08
[ 5s ] thds: 1 eps: 926.00 lat (ms,95%): 1.08
[ 6s ] thds: 1 eps: 926.00 lat (ms,95%): 1.08
[ 7s ] thds: 1 eps: 925.00 lat (ms,95%): 1.08
[ 8s ] thds: 1 eps: 926.02 lat (ms,95%): 1.08
[ 9s ] thds: 1 eps: 925.99 lat (ms,95%): 1.08
[ 10s ] thds: 1 eps: 924.98 lat (ms,95%): 1.08複製代碼

  1. checkpoints_report線程:若是嫌週期性輸出不夠多,那麼能夠在某幾個時間點總體輸出,參數項爲: --report-checkpoints=[LIST,...]
  1. 仍是對CPU測試用例舉例:sysbench cpu --report-checkpoints=3,8 run,截取部分輸出結果以下:

Threads started!

[ 3s ] Checkpoint report:
CPU speed:
    events per second:   923.01

General statistics:
    total time:                          3.0001s
    total number of events:              2771

Latency (ms):
         min:                                  1.08
         avg:                                  1.08
         max:                                  1.22
         95th percentile:                      1.08
         sum:                               3000.88

Threads fairness:
    events (avg/stddev):           2773.0000/0.00
    execution time (avg/stddev):   3.0009/0.00

[ 8s ] Checkpoint report:
CPU speed:
    events per second:   924.47

General statistics:
    total time:                          8.0001s
    total number of events:              4622

Latency (ms):
         min:                                  1.08
         avg:                                  1.08
         max:                                  1.16
         95th percentile:                      1.08
         sum:                               4998.04

Threads fairness:
    events (avg/stddev):           4621.0000/0.00
    execution time (avg/stddev):   4.9980/0.00複製代碼

  1. tx_rate_controll線程,控制每秒輸出量的一個線程:參數項爲: --rate=N,默認是不作控制的。
  1. 仍是拿CPU測試用例舉例,控制每秒跑10個event:sysbench cpu run --rate=10,截取部分輸出結果以下:

Running the test with following options:
Number of threads: 1
Target transaction rate: 10/sec
Initializing random number generator from current time

Prime numbers limit: 10000

Initializing worker threads...

Threads started!

CPU speed:
    events per second:     8.87 #沒那麼精準哈複製代碼

  1. 判斷是否超時,默認是10秒
  1. 判斷是否到達最大event數,若是設置了的話
  1. 就是速率控制。

sysbench是一個整體框架,它用來操做各個測性能的計算,那各個部門只須要作的一件事情是聲明須要的實現。只要理解了這三個struct就能夠了:架構

拿最簡單的CPU性能計算舉例,它須要實現的是:
看到這個後,把一個case須要作的事情描述很清楚了,從須要什麼參數,到初始化,逐個event執行,函數定義很清晰。sysbench的其餘case也都這樣須要一個完整的結構說明,如io操做,則須要多一個case的prepare和cleandown聲明。
至此,能夠清晰地看到sysbench的框架仍是很是好理解。
每一秒輸出一個結果,eps是每一秒的event數,lat單位是毫秒,95分位延遲數據是1.08
輸出速率控制在哪裏呢?眼尖的人立刻能夠看到是在sb_more_events函數。那sb_more_events函數主要是作什麼呢:

綜上,大概介紹了sysbench框架的整體實現,關於數據庫性能測試容下篇再介紹。

相關文章
相關標籤/搜索