vendor: update all dependencies to latest versions

This commit is contained in:
Nick Craig-Wood
2018-01-16 13:20:59 +00:00
parent 8e83fb6fb9
commit 7d3a17725d
4878 changed files with 1974213 additions and 201199 deletions
@@ -0,0 +1,70 @@
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package distribution
import (
"log"
"math"
"sort"
"sync/atomic"
)
// D is a distribution. Methods of D can be called concurrently by multiple
// goroutines.
type D struct {
buckets []uint64
}
// New creates a new distribution capable of holding values from 0 to n-1.
func New(n int) *D {
return &D{
buckets: make([]uint64, n),
}
}
// Record records value v to the distribution.
// To help with distributions with long tails, if v is larger than the maximum value,
// Record records the maximum value instead.
// If v is negative, Record panics.
func (d *D) Record(v int) {
if v < 0 {
log.Panicf("Record: value out of range: %d", v)
} else if v >= len(d.buckets) {
v = len(d.buckets) - 1
}
atomic.AddUint64(&d.buckets[v], 1)
}
// Percentile computes the p-th percentile of the distribution where
// p is between 0 and 1.
func (d *D) Percentile(p float64) int {
// NOTE: This implementation uses the nearest-rank method.
// https://en.wikipedia.org/wiki/Percentile#The_nearest-rank_method
if p < 0 || p > 1 {
log.Panicf("Percentile: percentile out of range: %f", p)
}
bucketSums := make([]uint64, len(d.buckets))
var sum uint64
for i := range bucketSums {
sum += atomic.LoadUint64(&d.buckets[i])
bucketSums[i] = sum
}
total := bucketSums[len(bucketSums)-1]
target := uint64(math.Ceil(float64(total) * p))
return sort.Search(len(bucketSums), func(i int) bool { return bucketSums[i] >= target })
}
@@ -0,0 +1,94 @@
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package distribution
import (
"sync"
"testing"
)
func TestDistribution(t *testing.T) {
// These tests come from examples in https://en.wikipedia.org/wiki/Percentile#The_nearest-rank_method
tests := []struct {
// values in distribution
vals []int
// percentiles and expected percentile values
pp []float64
vv []int
}{
{
vals: []int{15, 20, 35, 40, 50},
pp: []float64{0.05, 0.3, 0.4, 0.5, 1},
vv: []int{15, 20, 20, 35, 50},
},
{
vals: []int{3, 6, 7, 8, 8, 10, 13, 15, 16, 20},
pp: []float64{0.25, 0.5, 0.75, 1},
vv: []int{7, 8, 15, 20},
},
{
vals: []int{3, 6, 7, 8, 8, 9, 10, 13, 15, 16, 20},
pp: []float64{0.25, 0.5, 0.75, 1},
vv: []int{7, 9, 15, 20},
},
}
maxVal := 0
for _, tst := range tests {
for _, v := range tst.vals {
if maxVal < v {
maxVal = v
}
}
}
for _, tst := range tests {
d := New(maxVal + 1)
for _, v := range tst.vals {
d.Record(v)
}
for i, p := range tst.pp {
got, want := d.Percentile(p), tst.vv[i]
if got != want {
t.Errorf("d=%v, d.Percentile(%f)=%d, want %d", d, p, got, want)
}
}
}
}
func TestRace(t *testing.T) {
const N int = 1e3
const parallel = 2
d := New(N)
var wg sync.WaitGroup
wg.Add(parallel)
for i := 0; i < parallel; i++ {
go func() {
for i := 0; i < N; i++ {
d.Record(i)
}
wg.Done()
}()
}
for i := 0; i < N; i++ {
if p := d.Percentile(0.5); p > N {
t.Fatalf("d.Percentile(0.5)=%d, expected to be at most %d", p, N)
}
}
}