前段時間搞優化,最後瓶頸發現都在數據庫單點上。 問DBA,給個人寫入答案是在1W(機械硬盤)左右。java
聯想起前幾天infoQ上一篇文章說他們最好的硬件寫入速度在2W後也沒法提升(SSD硬盤)mysql
但這東西感受歷來沒證明過,故一時興起,弄臺虛擬機壓測起來。git
想搞清下面的問題:github
1,mysql支撐多少鏈接數?sql
2,寫入瓶頸究竟是多少?數據庫
3,求QPSapache
暢想: ubuntu
足夠的CPU, load>遠小於核數*2服務器
足夠的內存, 基本只用到物理內存eclipse
瓶頸在硬盤,寫入速度應該能到90-100M/S(機械硬盤,7200轉)。 故:預計kB_wrtn/s在90M左右
準備階段:
硬件:
壓測機I5雙核,8G內存, 開一個虛擬機+mysql,一個eclipse,一個jmeter
MYSQL準備
mysql基本命令
中止:
root@ubuntu:/home/hejinbin# /etc/init.d/mysql stop
Stopping mysql (via systemctl): mysql.service.
啓動:
root@ubuntu:/home/hejinbin# /etc/init.d/mysql start
Starting mysql (via systemctl): mysql.service.
重啓:
/etc/init.d/mysql restart 重啓mysql
查看當前mysq端口的鏈接數
netstat -apn|grep "3306"
查看mysql基本狀態
show processlist; 查看當前鏈接
show status; 查看當前數據庫狀態
show variables like 'max_connections' mysql當前最大鏈接數
set global max_connections=1000; 設置當前最大鏈接數爲1000
這個設置會立刻生效,可是當mysql重啓時這個設置會失效,須要長期生效在my.ini 添加 max_connections=1000
jmeter準備:
1,測試空壓測, 測試壓測客戶端能承受的線程數,
只開聚合報告,其它圖形和結果樹輸出關閉。
通過測試,打開監視器圖形結果和結果樹輸出,本機空壓測也只能到1500附近,因此特別注意關閉這兩個,只開聚合報告,個人機器100個線程空壓測能夠到壓測到6W吞吐量(throughput/sec)
後面也用100個線程進行mysql的插入壓測
JAVA測試類準備
用C3P0進行壓測:
C3P0:
1, 在classpath下放入 c3p0-config.xml 配置以下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
2,JAVA測試代碼
1 package org.test; 2 3 import java.sql.Connection; 4 import java.sql.ResultSet; 5 import java.util.concurrent.atomic.AtomicLong; 6 7 import com.mchange.v2.c3p0.ComboPooledDataSource; 8 import com.mchange.v2.c3p0.impl.NewProxyPreparedStatement; 9 10 /** 11 * hejinbin , QQ 107966750, C3P0的測試用例 2016-10-13 12 */ 13 public class C3P0Test { 14 public static AtomicLong id = new AtomicLong(1000000); 15 private static ComboPooledDataSource cpds = new ComboPooledDataSource(); 16 17 public static Long queryTest() throws Exception { 18 Long aid = null; 19 Connection con = null; 20 try { 21 // 從C3P0獲取鏈接 22 con = cpds.getConnection(); 23 NewProxyPreparedStatement prepare = (NewProxyPreparedStatement) con 24 .prepareStatement("select * from tbl_account_info where account_id = ?"); 25 prepare.setLong(1, 1001203); 26 ResultSet result = prepare.executeQuery(); 27 if (result.next()) { 28 aid = (result.getLong("aid")); 29 } 30 } catch (Exception e) { 31 throw e; 32 } finally { 33 // 此處關閉已經被C3P0重寫, 只是返還鏈接池 34 con.close(); 35 } 36 37 return aid; 38 } 39 40 public static void insertTest() throws Exception { 41 42 Connection con = null; 43 try { 44 // 從C3P0獲取鏈接 45 con = cpds.getConnection(); 46 NewProxyPreparedStatement prepare = (NewProxyPreparedStatement) con.prepareStatement( 47 "INSERT INTO `tbl_account_info` VALUES (?, '1231232132', '何錦彬測試', '12312312', '15018711111', 'hejinbin@qq.com', '1', 'hjb_recharge', 'yyUid:12312312', '1', '', '', '', '1399882974', '1429785062', '183.60.177.229', '1464658277', '14.29.83.74', '0', '0', '1', '0', '2', '1', '0', '1', '1', '1', '1', '1', '1', '1', '', '', '2000', '0', '0', '0', '2000', '6', '0', '0', '1', '0', '1456308697', '658', null, '廣東廣州', '0', '0', '0', '0', '20', null, null, '1', null, '');"); 48 prepare.setLong(1, id.incrementAndGet()); 49 prepare.execute(); 50 } catch (Exception e) { 51 throw e; 52 } finally { 53 // 此處關閉已經被C3P0重寫, 只是返還鏈接池 54 55 con.close(); 56 } 57 58 } 59 60 public static void main(String[] args) throws Exception { 61 for (int i = 0; i < 100000; i++) 62 insertTest(); 63 } 64 }
3,jmeter壓測相關代碼
package org.test.bin; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; import org.test.C3P0Test; import org.test.JDBCWthoutDBPoolTest; /** * hejinbin , QQ 107966750, C3P0的測試用例,放入jmeter中 2016-10-13 */ public class JDBWithC3P0JmertRuning extends AbstractJavaSamplerClient { // 填參數 public Arguments getDefaultParameters() { Arguments params = new Arguments(); return params; } // 測試查詢 // public SampleResult runTest(JavaSamplerContext arg0) { // SampleResult sr = new SampleResult(); // try { // sr.sampleStart(); // Long aid = C3P0Test.queryTest(); // // Long aid = 741l; // // 逾期值 // if (aid == 741) { // // 只有是預期值才判斷查詢成功 // sr.setSuccessful(true); // } else { // sr.setSuccessful(false); // } // } catch (Exception e) { // sr.setSuccessful(false); // e.printStackTrace(); // } finally { // sr.sampleEnd(); // } // return sr; // } public SampleResult runTest(JavaSamplerContext arg0) { SampleResult sr = new SampleResult(); try { sr.sampleStart(); C3P0Test.insertTest(); // 只有是預期值才判斷查詢成功 sr.setSuccessful(true); } catch (Exception e) { sr.setSuccessful(false); e.printStackTrace(); } finally { sr.sampleEnd(); } return sr; } }
整個工程地址 https://github.com/bensonHe/Test4DBInsert
一切就緒, 開始死磕
第一次壓測配置(虛擬機):
數據庫服務器:1G內存+20G硬盤+1個CPU,單核
jmeter本地
壓測結果
3100 左右
負載在6.77附近,判斷是CPU問題,加4核
第二次壓測配置(虛擬機):
1G內存+20G硬盤+1個CPU,4核
直接到了5700附近,
total used free shared buffers cached
Mem: 984 974 9 1 132 455
-/+ buffers/cache: 386 597
Swap: 2044 31 2013
只有9M剩餘,預測增長物理內存能夠繼續提升
第三次發現物理內存使用
4G內存+20G硬盤+1個CPU,4核
直接壓測到了 7500附近,IOWA% 在10附近,使用CPU 30%
此時服務器狀態, 9的WA
top - 00:55:47 up 15 min, 1 user, load average: 7.63, 2.61, 1.09
Tasks: 175 total, 1 running, 174 sleeping, 0 stopped, 0 zombie
%Cpu(s): 15.1 us, 40.6 sy, 0.0 ni, 28.3 id, 9.0 wa, 0.0 hi, 7.0 si, 0.0 st
KiB Mem: 4038340 total, 1837716 used, 2200624 free, 151648 buffers
KiB Swap: 2094076 total, 0 used, 2094076 free. 1224640 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2345 mysql 20 0 2539240 274992 13540 S 279.5 6.8 4:48.13 mysqld
機器信息:
top - 01:02:07 up 21 min, 1 user, load average: 6.48, 6.91, 3.77
Tasks: 175 total, 1 running, 174 sleeping, 0 stopped, 0 zombie
%Cpu(s): 14.2 us, 33.2 sy, 0.0 ni, 35.0 id, 11.3 wa, 0.0 hi, 6.3 si, 0.0 st
KiB Mem: 4038340 total, 2605868 used, 1432472 free, 152740 buffers
KiB Swap: 2094076 total, 0 used, 2094076 free. 1965808 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2345 mysql 20 0 2540260 278508 13540 S 247.7 6.9 21:44.32 mysqld
CPU 4核, 負載 6.48, 沒到瓶頸
內存, swap使用率是0, 也沒壓力
硬盤,
經過監控發現
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 688.00 0.00 17356.00 0 17356
硬盤寫入速度在 17M/s, 感受還沒到瓶頸, 7200起色械硬盤的是 90M
如何進一步優化,讓寫入速度達到瓶頸。 想到是配置mysql的參數了,下次繼續嘗試,有活要作了.
`不知道咋轉圖,