package loadaverage import ( "math" "time" ) // Constants for calculating EMA const SAMPLE_DELAY time.Duration = time.Second * 5 var ( ONE_MIN_ALPHA float64 = math.Exp(-SAMPLE_DELAY.Seconds() / 60.0) // e^(-dt/W), where dt is the sample time and W FIVE_MIN_ALPHA float64 = math.Exp(-SAMPLE_DELAY.Seconds() / 300.0) // is the target timeframe. eg.: 60 for one min FIFTEEN_MIN_ALPHA float64 = math.Exp(-SAMPLE_DELAY.Seconds() / 900.0) ) type LoadAverageInformation struct { /* * Load average in the past minute */ Load1 float64 /* * Load average in the past 5 minutes */ Load5 float64 /* * Load average in the past 15 minutes */ Load15 float64 initialized bool // track whether this is the first measurement or not } func (loadAvg *LoadAverageInformation) Update(currentSample float64) { if !loadAvg.initialized { // Seed the initial averages with the first sample to avoid a slow ramp-up from zero loadAvg.Load1 = currentSample loadAvg.Load5 = currentSample loadAvg.Load15 = currentSample loadAvg.initialized = true return } loadAvg.Load1 = loadAvg.Load1*ONE_MIN_ALPHA + currentSample*(1.0-ONE_MIN_ALPHA) loadAvg.Load5 = loadAvg.Load5*FIVE_MIN_ALPHA + currentSample*(1.0-FIVE_MIN_ALPHA) loadAvg.Load15 = loadAvg.Load15*FIFTEEN_MIN_ALPHA + currentSample*(1.0-FIFTEEN_MIN_ALPHA) }