github.com/argoproj/argo-cd@v1.8.7/util/stats/stats.go (about)

     1  package stats
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // mock out time.Now() for unit tests
     8  var now = time.Now
     9  
    10  // TimingStats is a helper to breakdown the timing of an expensive function call
    11  // Usage:
    12  // ts := NewTimingStats()
    13  // ts.AddCheckpoint("checkpoint-1")
    14  // ...
    15  // ts.AddCheckpoint("checkpoint-2")
    16  // ...
    17  // ts.AddCheckpoint("checkpoint-3")
    18  // ts.Timings()
    19  type TimingStats struct {
    20  	StartTime time.Time
    21  
    22  	checkpoints []tsCheckpoint
    23  }
    24  
    25  type tsCheckpoint struct {
    26  	name string
    27  	time time.Time
    28  }
    29  
    30  func NewTimingStats() *TimingStats {
    31  	return &TimingStats{
    32  		StartTime: now(),
    33  	}
    34  }
    35  
    36  func (t *TimingStats) AddCheckpoint(name string) {
    37  	cp := tsCheckpoint{
    38  		name: name,
    39  		time: now(),
    40  	}
    41  	t.checkpoints = append(t.checkpoints, cp)
    42  }
    43  
    44  func (t *TimingStats) Timings() map[string]time.Duration {
    45  	timings := make(map[string]time.Duration)
    46  	prev := t.StartTime
    47  	for _, cp := range t.checkpoints {
    48  		timings[cp.name] = cp.time.Sub(prev)
    49  		prev = cp.time
    50  	}
    51  	return timings
    52  }