TCMalloc 對MYSQL 性能 優化的分析

雖然經過研究發現TCMalloc不適合我們現有的遊戲框架,但意外收獲發現TCMalloc可以大幅度提高MYSQL 性能及內存占用,這裡給出配置及測試的結果:

 

1.配置

 

關於TCMalloc的安裝,在《Google perftools 安裝手記(TCMalloc)》 一文中已經詳細給出,下面給出將TCMalloc配置到MYSQL的步驟:

 

1.1 修改MySQL啟動腳本(依據MySQL安裝位置):

1 vi /usr/local/mysql/bin/mysqld_safe

  在# executing mysqld_safe的下一行,添加:

1 export LD_PRELOAD=/usr/local/lib/libtcmalloc.so

 保存退出,並重啟MySQL

1.2 依據lsof驗證命令查看TCMalloc是否起效: 

1 # lsof |grep -i libtcmalloc.so

 如果發現以下信息,說明tcmalloc已經起效:

1 mysqld  13961   mysql  memREG  253,0  1948990 196421/usr/local/lib/libtcmalloc.so.4.1.2

 

2.性能測試工具sysbench

 

sysbench是一個開源的、模塊化的、跨平臺的多線程性能測試工具,可以用來進行CPU、內存、磁盤I/O、線程、資料庫的性能測試。目前支持的資料庫有MySQL、Oracle和PostgreSQL。以下操作都將以支持MySQL資料庫為例進行。sourceforge已掛,下載地址:https://download.csdn.net/detail/chen19870707/8060033,安裝步驟如下:

 

1 tar zxf sysbench-0.4.10.tar.gz 

2 cd sysbench-0.4.10

3 ./configure && make && make install 

4 strip /usr/local/bin/sysbench 

 

 

3.性能分析:

  數據準備:

1 sysbench –test=oltp –mysql-table-engine=innodb –oltp-table-size=10000 –max-requests=10000 –num-threads=16 –mysql-host=127.0.0.1 –mysql-port=3306 –mysql-user=root –mysql-password=root –mysql-db=test –mysql-socket=/tmp/mysql.sock prepare

  性能測試:

1 sysbench –test=oltp –mysql-table-engine=innodb –oltp-table-size=10000 –max-requests=10000 –num-threads=16 –mysql-host=127.0.0.1  –mysql-port=3306 –mysql-user=root –mysql-password=root –mysql-db=test –mysql-socket=/tmp/mysql.sock run >> report.txt

  數據清理:

1 /usr/local/bin/sysbench –test=oltp –mysql-table-engine=innodb –oltp-table-size=10000 –max-requests=10000 –num-threads=16 –mysql-host=127.0.01  –mysql-port=3306 –mysql-user=root –mysql-password=root –mysql-db=test –mysql-socket=/tmp/mysql.sock cleanup

  參數說明:

 

1 –oltp-table-size=N   測試表的記錄數。默認是10000 –max-requests=N limit for total number of requests [10000] #請求的最大數目。默認為10000,0代表不限制。

2 –max-requests=N limit for total number of requests [10000] #請求的最大數目。默認為10000,0代表不限制。

3 –num-threads=Nnumber of threads to use [1] #創建測試線程的數目。默認為1.

4 –mysql-host=[LIST,…] MySQL server host [localhost]

5 –mysql-port=NMySQL server port [3306]

6 –mysql-password=STRING MySQL password []

7 –mysql-db=STRING MySQL database name [sbtest]

8 –mysql-socket=STRING   MySQL socket

 

測試結果:

    未使用TCMalloc

OLTP test statistics: 
    queries performed: 
        read:                            140112 
        write:                           50019 
        other:                           20008 
        total:                           210139 
    transactions:                        10000  (756.11 per sec.) 
    deadlocks:                           8      (0.60 per sec.) 
    read/write requests:                 190131 (14376.02 per sec.) 
    other operations:                    20008  (1512.83 per sec.)

Test execution summary: 
    total time:                          13.2256s 
    total number of events:              10000 
    total time taken by event execution: 211.4342 
    per-request statistics: 
         min:                                  2.96ms 
         avg:                                 21.14ms 
         max:                                423.52ms 
         approx.  95 percentile:     56.25ms

                                使用TCMalloc

OLTP test statistics: 
    queries performed: 
        read:                            140084 
        write:                           50017 
        other:                           20006 
        total:                           210107 
    transactions:                        10000  (862.83 per sec.) 
    deadlocks:                           6      (0.52 per sec.) 
    read/write requests:                 190101 (16402.39 per sec.) 
    other operations:                    20006  (1726.17 per sec.)

Test execution summary: 
    total time:                          11.5898s 
    total number of events:              10000 
    total time taken by event execution: 185.2397 
    per-request statistics: 
         min:                                  2.81ms 
         avg:                                 18.52ms 
         max:                                430.03ms 
         approx.  95 percentile:              36.49ms

 

 

可以看到使用TCMalloc性能明顯優於未使用,這裡主要原因是mysql是多線程小內存分配,TCMalloc由於每個線程均有線程緩沖區,所以對這樣的小對象分配無競爭,效率非常好,可以看到TCMalloc對MYSQL優化效果不錯 ,建議使用。

You May Also Like