优化性能分析器

This commit is contained in:
duanhf2012
2020-04-02 16:00:14 +08:00
parent 9e128759f6
commit 1313b2f7b3

View File

@@ -105,16 +105,16 @@ func (slf *Profiler) check(pElem *Element) (*Record,time.Duration) {
func (slf *Profiler) Pop() { func (slf *Profiler) Pop() {
slf.stackLocker.Lock() slf.stackLocker.Lock()
front := slf.stack.Front() back := slf.stack.Back()
if front!=nil && front.Value!=nil { if back!=nil && back.Value!=nil {
pElement := front.Value.(*Element) pElement := back.Value.(*Element)
pElem,subTm := slf.check(pElement) pElem,subTm := slf.check(pElement)
slf.callNum+=1 slf.callNum+=1
slf.totalCostTime += subTm slf.totalCostTime += subTm
if pElem != nil { if pElem != nil {
slf.pushRecordLog(pElem) slf.pushRecordLog(pElem)
} }
slf.stack.Remove(front) slf.stack.Remove(back)
} }
slf.stackLocker.Unlock() slf.stackLocker.Unlock()
@@ -135,7 +135,12 @@ func DefaultReportFunction(name string,callNum int,costTime time.Duration,record
var strReport string var strReport string
strReport = "Profiler report tag "+name+":\n" strReport = "Profiler report tag "+name+":\n"
strReport += fmt.Sprintf("process count %d,take time %d Milliseconds,average %d Milliseconds/per.\n",callNum,costTime.Milliseconds(),costTime.Milliseconds()/int64(callNum)) var average int64
if callNum>0 {
average = costTime.Milliseconds()/int64(callNum)
}
strReport += fmt.Sprintf("process count %d,take time %d Milliseconds,average %d Milliseconds/per.\n",callNum,costTime.Milliseconds(),average)
elem := record.Front() elem := record.Front()
var strTypes string var strTypes string
for elem!=nil { for elem!=nil {
@@ -157,19 +162,21 @@ func Report() {
var record *list.List var record *list.List
for name,prof := range mapProfiler{ for name,prof := range mapProfiler{
prof.stackLocker.RLock() prof.stackLocker.RLock()
if prof.record.Len() == 0 {
prof.stackLocker.RUnlock()
continue
}
//取栈的队首是否存在异常MaxOverTime数据 //取栈是否存在异常MaxOverTime数据
pElem := prof.stack.Front() pElem := prof.stack.Back()
if pElem!=nil && pElem.Value!=nil{ if pElem!=nil && pElem.Value!=nil{
pRecord,_ := prof.check(pElem.Value.(*Element)) pRecord,_ := prof.check(pElem.Value.(*Element))
if pRecord!=nil { if pRecord!=nil {
prof.pushRecordLog(pRecord) prof.pushRecordLog(pRecord)
} }
} }
if prof.record.Len() == 0 {
prof.stackLocker.RUnlock()
continue
}
record = prof.record record = prof.record
prof.record = list.New() prof.record = list.New()
prof.stackLocker.RUnlock() prof.stackLocker.RUnlock()