背景
一个接口性能测试,有数万结果对象产生,在做脚本的时候,发现访问很慢大约在10-20s。
环境
web3台使用lvs负载均衡,应用层5台机器,db主从共2台
准备
1、在3台web服务器上和5台应用层服务器分别装上greys
curl -sLk http://ompc.oss.aliyuncs.com/greys/install.sh|sh
2、查找代码中controller层和service层的入口方法
@RequestMapping(value = "/checkOtherInfo", method = RequestMethod.POST)
public GenericResponse<Map<String, Object>> checkOtherInfo(……) {
……
}
HashMap<String, Object> checkOtherInfo(……);
3、在3台web上查找对应java进程,启动greys
./greys $pid
使用ptrace(我隐藏了部分类路径)
ga?>ptrace com.…….biz* checkOtherInfo
4、在5台应用上做类似3中的操作,启动greys,并使用ptrace
5、发起接口请求
6、从应用层跟踪内容
重要的只有两句(当然这里的时间因greys变得比实际要长,但是可以反映问题),再详细的见附录
+---+[66161,65538ms]${projectClassPath}wms.biz.in.purchase.service.impl.ReceiptManageServiceImpl:getPickAreaMaxReceptionCount()
| `---+[66161,23256ms]com.dangdang.wms.biz.in.purchase.service.impl.ReceiptManageServiceImpl:getReceiptUnTrunkCount()
根据这两句并结合这两句下面的详细信息定位到是两处for循环
第一处是在for循环中查询db导致多次查询
第二处是循环将结果拼装成对象
建议优化:
1、将for循环中的查询改成批量查询,即for循环中进行条件组装,使用批量查询减少与DB交互
2、将查询结果组装成对象是用多线程而非逐条处理
7、优化后再测,450ms
附录
[BEGIN] 2017/7/24 11:37:09
`---+pTracing for : thread_name="DubboServerHandler-10.3.16.173:20890-thread-498" thread_id=0x4e4;is_daemon=true;priority=5;process=1001;
`---+[66313,66313ms]${projectClassPath}wms.biz.api.facade.impl.ReceiptAPIFacadeImpl:checkOtherInfo()
`---+[66313,66312ms]${projectClassPath}wms.common.request.RequestWorker:execute()
`---+[66313,66312ms]null:processRequest()
`---+[66313,66312ms]null:processRequest()
`---+[66313,66312ms]com.sun.proxy.$Proxy330:checkOtherInfo()
`---+[66304,66301ms]${projectClassPath}wms.biz.in.purchase.service.impl.ReceiptManageServiceImpl:checkOtherInfo()
| `---+[230,19ms]${projectClassPath}wms.biz.in.purchase.service.impl.ReceiptStartTimeServiceImpl:selectByReceipt()
| `---+[230,19ms]com.sun.proxy.$Proxy478:selectByReceipt()
| `---+[230,18ms]com.sun.proxy.$Proxy477:selectByReceipt()
+---+[237,7ms]com.sun.proxy.$Proxy479:updateNotNull()
| `---+[237,6ms]${projectClassPath}ms.service.sharding.AbstractShardingService:updateNotNull()
| `---+[237,6ms]com.sun.proxy.$Proxy478:updateNotNull()
| `---+[236,4ms]com.sun.proxy.$Proxy477:updateNotNull()
| `---+[287,49ms]${projectClassPath}ms.service.sharding.AbstractShardingService:selectByCondition()
| `---+[287,49ms]${projectClassPath}ms.service.sharding.AbstractShardingService:selectByCondition()
| `---+[287,49ms]${projectClassPath}ms.service.sharding.AbstractShardingService:selectByCondition()
| `---+[287,49ms]com.sun.proxy.$Proxy226:selectByCondition()
| +---[242,1ms]${projectClassPath}wms.biz.in.purchase.model.PurchaseDetails:getLastDeliveryDate()
| +---[250,1ms]${projectClassPath}wms.biz.in.purchase.model.PurchaseDetailsExt:getTaxRateEnd()
| +---[254,1ms]${projectClassPath}wms.biz.in.purchase.model.PurchaseDetailsExt:getPackStandardEnd()
+---+[329,42ms]com.sun.proxy.$Proxy236:selectByCondition()
| `---+[329,42ms]${projectClassPath}ms.service.core.AbstractCRUDService:selectByCondition()
| `---+[329,42ms]${projectClassPath}ms.service.core.AbstractCRUDService:selectByCondition()
| `---+[329,41ms]com.sun.proxy.$Proxy235:selectByCondition()
| `---+[328,32ms]com.sun.proxy.$Proxy234:selectByCondition()
| +---[300,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptNoteDetails:getAlreadyActualPrice()
| +---[301,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptNoteDetailsExt:getReplyCountEnd()
+---+[560,19ms]com.sun.proxy.$Proxy236:selectByCondition()
| `---+[560,19ms]${projectClassPath}ms.service.core.AbstractCRUDService:selectByCondition()
| `---+[560,19ms]${projectClassPath}ms.service.core.AbstractCRUDService:selectByCondition()
| `---+[560,19ms]com.sun.proxy.$Proxy235:selectByCondition()
| +---[542,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptNoteDetailsExt:getActualReceiptCountEnd()
| +---[543,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptNoteDetails:getBoxStandard()
| `---+[560,12ms]com.sun.proxy.$Proxy234:selectByCondition()
| +---[560,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptNoteDetailsExt:getLastModifiedDateBegin()
+---+[586,25ms]com.sun.proxy.$Proxy233:selectByReceiptNoteCode()
| `---+[586,24ms]${projectClassPath}wms.biz.in.purchase.service.impl.ReceiptNoteServiceImpl:selectByReceiptNoteCode()
| `---+[586,24ms]com.sun.proxy.$Proxy232:selectByReceiptNoteCode()
| `---+[585,22ms]com.sun.proxy.$Proxy231:selectByReceiptNoteCode()
| +---[583,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptNote:setCanExcessCategory()
+---+[623,36ms]com.sun.proxy.$Proxy257:selectLineRule()
| `---+[623,35ms]${projectClassPath}wms.biz.in.purchase.service.impl.GoodsOnshelfConfigServiceImpl:selectLineRule()
| `---+[623,35ms]com.sun.proxy.$Proxy256:selectLineRule()
| +---[589,1ms]${projectClassPath}wms.biz.in.purchase.model.GoodsOnshelfConfig:getDefaultPickArea()
| `---+[622,32ms]com.sun.proxy.$Proxy255:selectLineRule()
| +---[592,1ms]${projectClassPath}wms.biz.in.purchase.model.GoodsOnshelfConfig:getBusinessType()
+---+[66161,65538ms]${projectClassPath}wms.biz.in.purchase.service.impl.ReceiptManageServiceImpl:getPickAreaMaxReceptionCount()
| +---+[631,8ms]com.sun.proxy.$Proxy401:selectByGoodsId()
| | `---+[631,7ms]${projectClassPath}wms.biz.on.restock.service.impl.RestockSysServiceImpl:selectByGoodsId()
| | `---+[631,7ms]com.sun.proxy.$Proxy398:selectByGoodsId()
| | `---[631,6ms]com.sun.proxy.$Proxy397:selectByGoodsId()
| +---+[920,178ms]com.sun.proxy.$Proxy82:queryQtyOfSpecialZoneType()
| | `---+[920,177ms]${projectClassPath}wms.biz.stock.api.StockManageApiDefaultImpl:queryQtyOfSpecialZoneType()
| | `---+[920,177ms]com.sun.proxy.$Proxy61:queryQtyOfSpecialZoneType()
| | `---+[920,176ms]${projectClassPath}wms.biz.stock.service.impl.StockStorageServiceImpl:queryQtyOfSpecialZoneType()
| | `---+[920,176ms]com.sun.proxy.$Proxy55:queryQtyOfSpecialZoneType()
| | `---[920,175ms]com.sun.proxy.$Proxy54:queryQtyOfSpecialZoneType()
| +---+[42905,41985ms]${projectClassPath}wms.biz.in.purchase.service.impl.ReceiptManageServiceImpl:getWaitingOnshelfCount()
| | +---+[5938,5017ms]com.sun.proxy.$Proxy253:getWaitingOnshelfCount()
| | | `---+[5938,5017ms]${projectClassPath}wms.biz.in.purchase.service.impl.OnshelfTaskDetailsServiceImpl:getWaitingOnshelfCount()
| | | `---+[5938,5017ms]com.sun.proxy.$Proxy249:getWaitingOnshelfCount()
| | | `---+[5937,5014ms]com.sun.proxy.$Proxy248:getWaitingOnshelfCount()
| | | +---[1639,1ms]${projectClassPath}wms.biz.in.purchase.model.OnshelfTaskDetails:setRecommendLocation()
| | | +---[1643,1ms]${projectClassPath}wms.biz.in.purchase.model.OnshelfTaskDetails:setOnshelfTaskType()
| | | +---[1677,1ms]${projectClassPath}wms.biz.in.purchase.model.OnshelfTaskDetails:setBillCode()
……省略数万行
| | | +---[5921,1ms]${projectClassPath}wms.biz.in.purchase.model.OnshelfTaskDetails:setBoxStandard()
| | | +---[5924,1ms]${projectClassPath}wms.biz.in.purchase.model.OnshelfTaskDetails:setCreator()
| | | +---[5927,1ms]${projectClassPath}wms.biz.in.purchase.model.OnshelfTaskDetails:setGoodsName()
| | | +---[5931,1ms]${projectClassPath}wms.biz.in.purchase.model.OnshelfTaskDetails:setBatchId()
| | +---[6276,1ms]${projectClassPath}wms.biz.in.purchase.model.OnshelfTaskDetails:getRecommendLocation()
| | +---[6353,1ms]${projectClassPath}wms.biz.in.purchase.model.OnshelfTaskDetails:getRecommendLocation()
……省略数万行
| | +---[41669,1ms]${projectClassPath}wms.biz.in.purchase.model.OnshelfTaskDetails:getWaitingOnshelfCount()
| | +---[42127,1ms]${projectClassPath}wms.biz.in.purchase.model.OnshelfTaskDetails:getRecommendLocation()
| | +---[42499,1ms]${projectClassPath}wms.biz.in.purchase.model.OnshelfTaskDetails:getRecommendLocation()
| `---+[66161,23256ms]${projectClassPath}wms.biz.in.purchase.service.impl.ReceiptManageServiceImpl:getReceiptUnTrunkCount()
| +---+[45311,2405ms]com.sun.proxy.$Proxy245:getUnTrunkCount()
| | `---+[45311,2405ms]${projectClassPath}wms.biz.in.purchase.service.impl.ReceiptRecordDetailsServiceImpl:getUnTrunkCount()
| | `---+[45311,2405ms]com.sun.proxy.$Proxy244:getUnTrunkCount()
| | `---+[45311,2404ms]com.sun.proxy.$Proxy243:getUnTrunkCount()
| | +---[43018,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptRecordDetails:setContainerCode()
……省略数万行
+---[45250,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptRecordDetails:setReceiptNoteCode()
| | +---[45274,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptRecordDetails:setReceiptNoteCode()
| | +---[45298,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptRecordDetails:setStatus()
| | +---[45309,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptRecordDetails:setModifier()
| +---[45348,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptRecordDetails:getRecommendLocation()
| +---[45442,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptRecordDetails:getActualReceiptCount()
……省略数万行
| +---[65943,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptRecordDetails:getRecommendLocation()
| +---[66158,1ms]${projectClassPath}wms.biz.in.purchase.model.ReceiptRecordDetails:getRecommendLocation()
[END] 2017/7/24 11:45:42