forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged ewma and cleaned up meter tests
- Loading branch information
Showing
8 changed files
with
257 additions
and
333 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// (c) 2019-2020, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package uptime | ||
|
||
import ( | ||
"math" | ||
"time" | ||
) | ||
|
||
// ContinuousFactory implements the Factory interface by returning a continuous | ||
// time meter. | ||
type ContinuousFactory struct{} | ||
|
||
// New implements the Factory interface. | ||
func (ContinuousFactory) New(halflife time.Duration) Meter { | ||
return NewMeter(halflife) | ||
} | ||
|
||
type continuousMeter struct { | ||
running bool | ||
started time.Time | ||
|
||
halflife time.Duration | ||
|
||
value float64 | ||
lastUpdated time.Time | ||
} | ||
|
||
// NewMeter returns a new Meter with the provided halflife | ||
func NewMeter(halflife time.Duration) Meter { | ||
return &continuousMeter{halflife: halflife} | ||
} | ||
|
||
func (a *continuousMeter) Start(currentTime time.Time) { | ||
if a.running { | ||
return | ||
} | ||
a.Read(currentTime) | ||
a.running = true | ||
} | ||
|
||
func (a *continuousMeter) Stop(currentTime time.Time) { | ||
if !a.running { | ||
return | ||
} | ||
a.Read(currentTime) | ||
a.running = false | ||
} | ||
|
||
func (a *continuousMeter) Read(currentTime time.Time) float64 { | ||
timeSincePreviousUpdate := currentTime.Sub(a.lastUpdated) | ||
if timeSincePreviousUpdate <= 0 { | ||
return a.value | ||
} | ||
a.lastUpdated = currentTime | ||
|
||
factor := math.Pow(2, -float64(timeSincePreviousUpdate)/float64(a.halflife)) | ||
a.value *= factor | ||
if a.running { | ||
a.value += 1 - factor | ||
} | ||
return a.value | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// (c) 2019-2020, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package uptime | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Factory returns new meters. | ||
type Factory interface { | ||
// New returns a new meter with the provided halflife. | ||
New(halflife time.Duration) Meter | ||
} |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.