mirror of
https://github.com/datarhei/core.git
synced 2025-10-10 10:20:06 +08:00
Add v16.7.2
This commit is contained in:
28
vendor/github.com/prep/average/.travis.yml
generated
vendored
Normal file
28
vendor/github.com/prep/average/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.9
|
||||
- master
|
||||
|
||||
# Skip the install step. Don't `go get` dependencies. Only build with the
|
||||
# code in vendor/
|
||||
install: true
|
||||
|
||||
matrix:
|
||||
# It's ok if our code fails on unstable development versions of Go.
|
||||
allow_failures:
|
||||
- go: master
|
||||
# Don't wait for tip tests to finish. Mark the test run green if the
|
||||
# tests pass on the stable versions of Go.
|
||||
fast_finish: true
|
||||
|
||||
notifications:
|
||||
email: false
|
||||
|
||||
before_script:
|
||||
- GO_FILES=$(find . -iname '*.go' -type f | grep -v /vendor/)
|
||||
|
||||
script:
|
||||
- test -z $(gofmt -s -l $GO_FILES)
|
||||
- go tool vet .
|
||||
- go test -v -race ./...
|
27
vendor/github.com/prep/average/LICENSE
generated
vendored
Normal file
27
vendor/github.com/prep/average/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
Copyright (c) 2009 The Go Authors. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
42
vendor/github.com/prep/average/README.md
generated
vendored
Normal file
42
vendor/github.com/prep/average/README.md
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
average
|
||||
[](https://travis-ci.org/prep/average.svg?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/prep/average)
|
||||
[](https://godoc.org/github.com/prep/average)
|
||||
=======
|
||||
This stupidly named Go package contains a single struct that is used to implement counters on a sliding time window.
|
||||
|
||||
Usage
|
||||
-----
|
||||
```go
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/prep/average"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create a SlidingWindow that has a window of 15 minutes, with a
|
||||
// granulity of 1 minute.
|
||||
sw := average.MustNew(15 * time.Minute, time.Minute)
|
||||
defer sw.Stop()
|
||||
|
||||
// Do some work.
|
||||
sw.Add(15)
|
||||
// Do some more work.
|
||||
sw.Add(22)
|
||||
// Do even more work.
|
||||
sw.Add(22)
|
||||
|
||||
fmt.Printf("Average of last 1m: %f\n", sw.Average(time.Minute)
|
||||
fmt.Printf("Average of last 5m: %f\n", sw.Average(5 * time.Minute)
|
||||
fmt.Printf("Average of last 15m: %f\n\n", sw.Average(15 * time.Minute)
|
||||
|
||||
total, numSamples := sw.Total(15 * time.Minute)
|
||||
fmt.Printf("Counter has a total of %d over %d samples", total, numSamples)
|
||||
}
|
||||
```
|
||||
|
||||
License
|
||||
-------
|
||||
This software is created for MessageBird B.V. and distributed under the BSD-style license found in the LICENSE file.
|
142
vendor/github.com/prep/average/slidingwindow.go
generated
vendored
Normal file
142
vendor/github.com/prep/average/slidingwindow.go
generated
vendored
Normal file
@@ -0,0 +1,142 @@
|
||||
// Package average implements sliding time window.
|
||||
package average
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SlidingWindow provides a sliding time window with a custom size and
|
||||
// granularity to store int64 counters. This can be used to determine the total
|
||||
// or unweighted mean average of a subset of the window size.
|
||||
type SlidingWindow struct {
|
||||
window time.Duration
|
||||
granularity time.Duration
|
||||
samples []int64
|
||||
pos int
|
||||
size int
|
||||
stopOnce sync.Once
|
||||
stopC chan struct{}
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// MustNew returns a new SlidingWindow, but panics if an error occurs.
|
||||
func MustNew(window, granularity time.Duration) *SlidingWindow {
|
||||
sw, err := New(window, granularity)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
return sw
|
||||
}
|
||||
|
||||
// New returns a new SlidingWindow.
|
||||
func New(window, granularity time.Duration) (*SlidingWindow, error) {
|
||||
if window == 0 {
|
||||
return nil, errors.New("window cannot be 0")
|
||||
}
|
||||
if granularity == 0 {
|
||||
return nil, errors.New("granularity cannot be 0")
|
||||
}
|
||||
if window <= granularity || window%granularity != 0 {
|
||||
return nil, errors.New("window size has to be a multiplier of the granularity size")
|
||||
}
|
||||
|
||||
sw := &SlidingWindow{
|
||||
window: window,
|
||||
granularity: granularity,
|
||||
samples: make([]int64, int(window/granularity)),
|
||||
stopC: make(chan struct{}),
|
||||
}
|
||||
|
||||
go sw.shifter()
|
||||
return sw, nil
|
||||
}
|
||||
|
||||
func (sw *SlidingWindow) shifter() {
|
||||
ticker := time.NewTicker(sw.granularity)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
sw.Lock()
|
||||
if sw.pos = sw.pos + 1; sw.pos >= len(sw.samples) {
|
||||
sw.pos = 0
|
||||
}
|
||||
sw.samples[sw.pos] = 0
|
||||
if sw.size < len(sw.samples) {
|
||||
sw.size++
|
||||
}
|
||||
sw.Unlock()
|
||||
|
||||
case <-sw.stopC:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add increments the value of the current sample.
|
||||
func (sw *SlidingWindow) Add(v int64) {
|
||||
sw.Lock()
|
||||
sw.samples[sw.pos] += v
|
||||
sw.Unlock()
|
||||
}
|
||||
|
||||
// Average returns the unweighted mean of the specified window.
|
||||
func (sw *SlidingWindow) Average(window time.Duration) float64 {
|
||||
total, sampleCount := sw.Total(window)
|
||||
if sampleCount == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return float64(total) / float64(sampleCount)
|
||||
}
|
||||
|
||||
// Reset the samples in this sliding time window.
|
||||
func (sw *SlidingWindow) Reset() {
|
||||
sw.Lock()
|
||||
defer sw.Unlock()
|
||||
|
||||
sw.pos, sw.size = 0, 0
|
||||
for i := range sw.samples {
|
||||
sw.samples[i] = 0
|
||||
}
|
||||
}
|
||||
|
||||
// Stop the shifter of this sliding time window. A stopped SlidingWindow cannot
|
||||
// be started again.
|
||||
func (sw *SlidingWindow) Stop() {
|
||||
sw.stopOnce.Do(func() {
|
||||
sw.stopC <- struct{}{}
|
||||
})
|
||||
}
|
||||
|
||||
// Total returns the sum of all values over the specified window, as well as
|
||||
// the number of samples.
|
||||
func (sw *SlidingWindow) Total(window time.Duration) (int64, int) {
|
||||
if window > sw.window {
|
||||
window = sw.window
|
||||
}
|
||||
|
||||
sampleCount := int(window / sw.granularity)
|
||||
if sampleCount > sw.size {
|
||||
sampleCount = sw.size
|
||||
}
|
||||
|
||||
sw.RLock()
|
||||
defer sw.RUnlock()
|
||||
|
||||
var total int64
|
||||
for i := 1; i <= sampleCount; i++ {
|
||||
pos := sw.pos - i
|
||||
if pos < 0 {
|
||||
pos += len(sw.samples)
|
||||
}
|
||||
|
||||
total += sw.samples[pos]
|
||||
}
|
||||
|
||||
return total, sampleCount
|
||||
}
|
Reference in New Issue
Block a user