最近因?yàn)轫?xiàng)目需要調(diào)優(yōu)暂殖,所以特地研究了jmeter舒萎,壓力測(cè)試主要分為4個(gè)方向:
1.程序內(nèi)存分析尔当;
2.cpu可用性分析;
3.磁盤空間;
4.網(wǎng)絡(luò)帶寬;
關(guān)于項(xiàng)目酪穿,主要方向往tomcat優(yōu)化和mysql優(yōu)化去著手:
tomcat的優(yōu)化分為三個(gè)方向: 1.線程池優(yōu)化??? 2.內(nèi)存優(yōu)化(即JVM優(yōu)化)?? 3.IO優(yōu)化;
jvm內(nèi)存優(yōu)化和GC回收機(jī)制晴裹,jvm的內(nèi)存優(yōu)化主要是指堆內(nèi)存優(yōu)化被济,堆內(nèi)存分為3個(gè)狀態(tài) 新生代(占內(nèi)存空間1/3,里面又分eden surv1,surv2,內(nèi)存比例為8:1:1涧团,內(nèi)存回收機(jī)制為復(fù)制算法) 年老代(占內(nèi)存空間 2/3只磷,內(nèi)存回收機(jī)制為標(biāo)記算法)? 持久代(大約64M,有的是16M)泌绣,如果新建對(duì)象比較多钮追,回收頻繁,則應(yīng)將新生代的內(nèi)存擴(kuò)容阿迈;反之如果經(jīng)常使用的對(duì)象多元媚,則應(yīng)擴(kuò)容老年代;同時(shí)jvm虛擬機(jī)可使用最大內(nèi)存與啟動(dòng)最小內(nèi)存應(yīng)保持一致,避免GC頻繁的回收與開啟內(nèi)存刊棕;
線程池的優(yōu)化主要方向是設(shè)置maxThresds:最大并發(fā)量和minSpareThreads:tomcat啟動(dòng)時(shí)初始化的線程數(shù)
io優(yōu)化主要分Bio(tomcat8.0以下默認(rèn)是bio 同步阻塞式IO) nio(tomat8.0以上默認(rèn)是nio炭晒,同步非阻塞式IO),最強(qiáng)的是arp甥角,本質(zhì)就是使用jni技術(shù)來調(diào)用操作系統(tǒng)底層的io接口
mysql優(yōu)化: 設(shè)置最大并發(fā)數(shù) 開啟查詢緩存? 建立索引(普通 唯一 聚合 全文) 還有myisam與innodb
引擎優(yōu)化网严,myisam默認(rèn)不支持事務(wù),查詢效率高蜈膨,innodb默認(rèn)支持事務(wù)屿笼,默認(rèn)有鎖,行鎖里面的悲觀鎖 for update 翁巍,行鎖里面的樂觀鎖 lock in share mode,支持查詢并發(fā)驴一;優(yōu)化邏輯 : 先建議索引 再采用myisam,需要注意的是有些查詢會(huì)導(dǎo)致索引失效,例如 like 條件 not null之類
優(yōu)化前的查詢時(shí)間對(duì)比:
api:http://localhost:8080/lovePhone/model/list?bid=1
result:{ "resultCode": "0", "resultDesc": null, "resultData": { "total": 6, "list": [ { "pm_id": 1, "phoneBrand": { "pb_id": 1, "pb_name": "HTC", "pb_ylone": null, "pb_createtime": "2019-05-07", "pb_updatetime": null, "pb_yltwo": null, "pb_ylthree": null }, "pm_name": "HTC J(Z321e)", "pm_imagepath": "1.jpg", "pm_price": null, "pm_ylone": null, "pm_ylthree": null, "pm_yltwo": null, "pm_createtime": 1557158400000, "pm_updatetime": null, "firstStyle": null, "secondStyle": null, "thirdStyle": null }, { "pm_id": 2, "phoneBrand": { "pb_id": 1, "pb_name": "HTC", "pb_ylone": null, "pb_createtime": "2019-05-07", "pb_updatetime": null, "pb_yltwo": null, "pb_ylthree": null }, "pm_name": "HTC A320(渴望C)", "pm_imagepath": "2.jpg", "pm_price": null, "pm_ylone": null, "pm_ylthree": null, "pm_yltwo": null, "pm_createtime": 1557158400000, "pm_updatetime": null, "firstStyle": null, "secondStyle": null, "thirdStyle": null }, { "pm_id": 3, "phoneBrand": { "pb_id": 1, "pb_name": "HTC", "pb_ylone": null, "pb_createtime": "2019-05-07", "pb_updatetime": null, "pb_yltwo": null, "pb_ylthree": null }, "pm_name": "HTC X9u", "pm_imagepath": "3.jpg", "pm_price": null, "pm_ylone": null, "pm_ylthree": null, "pm_yltwo": null, "pm_createtime": 1557158400000, "pm_updatetime": null, "firstStyle": null, "secondStyle": null, "thirdStyle": null }, { "pm_id": 4, "phoneBrand": { "pb_id": 1, "pb_name": "HTC", "pb_ylone": null, "pb_createtime": "2019-05-07", "pb_updatetime": null, "pb_yltwo": null, "pb_ylthree": null }, "pm_name": "HTC 8X(C620e/聯(lián)通版)", "pm_imagepath": "4.jpg", "pm_price": null, "pm_ylone": null, "pm_ylthree": null, "pm_yltwo": null, "pm_createtime": 1557158400000, "pm_updatetime": null, "firstStyle": null, "secondStyle": null, "thirdStyle": null }, { "pm_id": 5, "phoneBrand": { "pb_id": 1, "pb_name": "HTC", "pb_ylone": null, "pb_createtime": "2019-05-07", "pb_updatetime": null, "pb_yltwo": null, "pb_ylthree": null }, "pm_name": "HTC T327d", "pm_imagepath": "5.jpg", "pm_price": null, "pm_ylone": null, "pm_ylthree": null, "pm_yltwo": null, "pm_createtime": 1557158400000, "pm_updatetime": null, "firstStyle": null, "secondStyle": null, "thirdStyle": null }, { "pm_id": 6, "phoneBrand": { "pb_id": 1, "pb_name": "HTC", "pb_ylone": null, "pb_createtime": "2019-05-07", "pb_updatetime": null, "pb_yltwo": null, "pb_ylthree": null }, "pm_name": "HTC T327t", "pm_imagepath": "6.jpg", "pm_price": null, "pm_ylone": null, "pm_ylthree": null, "pm_yltwo": null, "pm_createtime": 1557158400000, "pm_updatetime": null, "firstStyle": null, "secondStyle": null, "thirdStyle": null } ], "pageNum": 1, "pageSize": 8, "size": 6, "startRow": 1, "endRow": 6, "pages": 1, "prePage": 0, "nextPage": 0, "isFirstPage": true, "isLastPage": true, "hasPreviousPage": false, "hasNextPage": false, "navigatePages": 8, "navigatepageNums": [ 1 ], "navigateFirstPage": 1, "navigateLastPage": 1, "firstPage": 1, "lastPage": 1 } }
沒有優(yōu)化前的查詢:
優(yōu)化后的查詢 :
測(cè)試結(jié)論:
1.發(fā)現(xiàn)第一次請(qǐng)求耗時(shí)比較長(zhǎng)灶壶,還無法找到原因肝断;
2.優(yōu)化tomcat的catalina.bat文件和servlet.xml文件有效果,優(yōu)化mysql的最大并發(fā)數(shù)和開啟查詢緩存有效果驰凛,因數(shù)據(jù)庫(kù)數(shù)據(jù)有限胸懈,故沒有建立索引,也沒有采用myisam