Added start/stop methods to profiler

This commit is contained in:
Carlos Salguero
2017-04-02 20:19:55 -03:00
parent 248c74e6b7
commit 1a8e3a0b76
2 changed files with 10 additions and 3 deletions

View File

@@ -72,6 +72,7 @@ type Profile struct {
stats []Stat stats []Stat
keyFilters []string keyFilters []string
fingerprinter fingerprinter.Fingerprinter fingerprinter fingerprinter.Fingerprinter
running bool
} }
func NewProfiler(iterator Iter, filters []filter.Filter, ticker chan time.Time, fp fingerprinter.Fingerprinter) Profiler { func NewProfiler(iterator Iter, filters []filter.Filter, ticker chan time.Time, fp fingerprinter.Fingerprinter) Profiler {
@@ -91,12 +92,17 @@ func (p *Profile) StatsChan() chan []Stat {
} }
func (p *Profile) Start() { func (p *Profile) Start() {
if !p.running {
p.running = true
go p.getData() go p.getData()
} }
}
func (p *Profile) Stop() { func (p *Profile) Stop() {
if p.running {
p.stopChan <- true p.stopChan <- true
} }
}
func (p *Profile) getData() { func (p *Profile) getData() {
var doc proto.SystemProfile var doc proto.SystemProfile
@@ -165,10 +171,11 @@ func (p *Profile) getData() {
} }
p.statsChan <- statsToArray(stats) p.statsChan <- statsToArray(stats)
p.running = false
} }
func statsToArray(stats map[StatsGroupKey]*Stat) []Stat { func statsToArray(stats map[StatsGroupKey]*Stat) []Stat {
sa := make([]Stat, len(stats)) sa := []Stat{}
for _, s := range stats { for _, s := range stats {
sa = append(sa, *s) sa = append(sa, *s)
} }