Last active
August 29, 2015 13:56
-
-
Save karlseguin/8877070 to your computer and use it in GitHub Desktop.
Track data points with constant-space via sampling (can be used to calculate percentiles, for example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ( | |
MAX_SAMPLE_COUNT = 50 | |
MAX_SAMPLE_COUNT_F = float64(MAX_SAMPLE_COUNT) | |
) | |
type Stat struct { | |
sync.Mutex | |
hits int64 | |
samples []int | |
} | |
func (s *Stat) Timing(t int) { | |
index := -1 | |
hits := atomic.AddInt64(&s.hits, 1) | |
if hits < MAX_SAMPLE_COUNT { | |
index = int(hits) | |
} else if MAX_SAMPLE_COUNT_F / float64(hits) > rand.Float64() { | |
index = int(rand.Int31n(MAX_SAMPLE_COUNT)) | |
} | |
if index != -1 { | |
s.Lock() | |
s.samples[index] = t | |
s.Unlock() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment