github.com/TeaOSLab/EdgeNode@v1.3.8/internal/trackers/label.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package trackers
     4  
     5  import "time"
     6  
     7  type tracker struct {
     8  	label     string
     9  	startTime time.Time
    10  }
    11  
    12  func Begin(label string) *tracker {
    13  	return &tracker{label: label, startTime: time.Now()}
    14  }
    15  
    16  func Run(label string, f func()) {
    17  	var tr = Begin(label)
    18  	f()
    19  	tr.End()
    20  }
    21  
    22  func (this *tracker) End() {
    23  	SharedManager.Add(this.label, time.Since(this.startTime).Seconds()*1000)
    24  }
    25  
    26  func (this *tracker) Begin(subLabel string) *tracker {
    27  	return Begin(this.label + ":" + subLabel)
    28  }
    29  
    30  func (this *tracker) Add(duration time.Duration) {
    31  	this.startTime = this.startTime.Add(-duration)
    32  }