mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	增加API方法调用耗时统计
This commit is contained in:
		
							
								
								
									
										46
									
								
								internal/rpc/context.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								internal/rpc/context.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
 | 
			
		||||
 | 
			
		||||
package rpc
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Context struct {
 | 
			
		||||
	context.Context
 | 
			
		||||
 | 
			
		||||
	tagMap  map[string]time.Time
 | 
			
		||||
	costMap map[string]float64 // tag => costMs
 | 
			
		||||
	locker  sync.Mutex
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewContext(ctx context.Context) *Context {
 | 
			
		||||
	return &Context{
 | 
			
		||||
		Context: ctx,
 | 
			
		||||
		tagMap:  map[string]time.Time{},
 | 
			
		||||
		costMap: map[string]float64{},
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *Context) Begin(tag string) {
 | 
			
		||||
	this.locker.Lock()
 | 
			
		||||
	this.tagMap[tag] = time.Now()
 | 
			
		||||
	this.locker.Unlock()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *Context) End(tag string) {
 | 
			
		||||
	this.locker.Lock()
 | 
			
		||||
	begin, ok := this.tagMap[tag]
 | 
			
		||||
	if ok {
 | 
			
		||||
		this.costMap[tag] = time.Since(begin).Seconds() * 1000
 | 
			
		||||
	}
 | 
			
		||||
	this.locker.Unlock()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *Context) TagMap() map[string]float64 {
 | 
			
		||||
	this.locker.Lock()
 | 
			
		||||
	defer this.locker.Unlock()
 | 
			
		||||
	return this.costMap
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user