removed sparkline
This commit is contained in:
parent
89425716d1
commit
c5e9dcd2c6
54
bps.go
54
bps.go
@ -45,15 +45,6 @@ func New(interval time.Duration) (*BPS, error) {
|
|||||||
return NewPrecise(interval, 100)
|
return NewPrecise(interval, 100)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustNew behaves like New, but panics on error.
|
|
||||||
func MustNew(interval time.Duration) *BPS {
|
|
||||||
b, err := New(interval)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewPrecise behaves like New, but allows the user to specify the temporal
|
// NewPrecise behaves like New, but allows the user to specify the temporal
|
||||||
// resolution.
|
// resolution.
|
||||||
//
|
//
|
||||||
@ -131,51 +122,6 @@ func (b *BPS) HumanRate() string {
|
|||||||
return human(uint64(b.Rate())) + "/s"
|
return human(uint64(b.Rate())) + "/s"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sparkline returns a human-friendly sprakline of history
|
|
||||||
func (b *BPS) Sparkline(count int) string {
|
|
||||||
if count <= 0 || count > len(b.buckets) {
|
|
||||||
return "bucket count inappropriate"
|
|
||||||
}
|
|
||||||
sparks := []string{"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"}
|
|
||||||
line := ""
|
|
||||||
bucketsPer := len(b.buckets) / count
|
|
||||||
b.Lock()
|
|
||||||
defer b.Unlock()
|
|
||||||
|
|
||||||
var max int64
|
|
||||||
var cur int64
|
|
||||||
|
|
||||||
// find max
|
|
||||||
for i := 0; i < len(b.buckets); i++ {
|
|
||||||
cur += b.buckets[i]
|
|
||||||
if i%bucketsPer == 0 {
|
|
||||||
if cur > max {
|
|
||||||
max = cur
|
|
||||||
}
|
|
||||||
cur = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if max == 0 {
|
|
||||||
for i := 0; i < count; i++ {
|
|
||||||
line += sparks[0]
|
|
||||||
}
|
|
||||||
return line
|
|
||||||
}
|
|
||||||
|
|
||||||
cur = 0
|
|
||||||
for i := 0; i < len(b.buckets); i++ {
|
|
||||||
val := b.buckets[(b.timeI+i)%len(b.buckets)]
|
|
||||||
cur += val
|
|
||||||
if i%bucketsPer == 0 {
|
|
||||||
approx := int((float64(cur) / float64(max)) * float64(len(sparks)-1))
|
|
||||||
line += sparks[approx]
|
|
||||||
cur = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return line
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close cleans up and shuts down a BPS.
|
// Close cleans up and shuts down a BPS.
|
||||||
func (b *BPS) Close() {
|
func (b *BPS) Close() {
|
||||||
close(b.quit)
|
close(b.quit)
|
||||||
|
65
bps_test.go
65
bps_test.go
@ -93,68 +93,3 @@ func TestHumanBytes(t *testing.T) {
|
|||||||
t.Fatalf("did not get right human rate; got %v, want %v", got, want)
|
t.Fatalf("did not get right human rate; got %v, want %v", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestSparkBadBucketCounts just makes sure that bad inputs are reported
|
|
||||||
func TestSparkBadBucketCounts(t *testing.T) {
|
|
||||||
bw := BPS{
|
|
||||||
buckets: make([]int64, 10),
|
|
||||||
}
|
|
||||||
sizes := []int{-10, -1, 0, 11, 12, 100, 1000}
|
|
||||||
for _, size := range sizes {
|
|
||||||
got := bw.Sparkline(size)
|
|
||||||
want := "bucket count inappropriate"
|
|
||||||
if got != want {
|
|
||||||
t.Fatalf("got: %v, want: %v", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestSparkline just creates a simple BPS and traverses all reasonable count
|
|
||||||
// sizes. It's primarily a test for panics which plagued us early on.
|
|
||||||
func TestSparkline(t *testing.T) {
|
|
||||||
buckets := 20
|
|
||||||
bw := BPS{
|
|
||||||
buckets: make([]int64, buckets),
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("all 0")
|
|
||||||
for i := 0; i < buckets; i++ {
|
|
||||||
bw.buckets[i] = 0
|
|
||||||
}
|
|
||||||
for i := 1; i < buckets; i++ {
|
|
||||||
line := bw.Sparkline(i)
|
|
||||||
t.Logf("count: %d, len %d: %+v", i, len([]rune(line)), line)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("increasing")
|
|
||||||
for i := 0; i < buckets; i++ {
|
|
||||||
bw.buckets[i] = int64(i * buckets)
|
|
||||||
}
|
|
||||||
for i := 1; i < buckets; i++ {
|
|
||||||
line := bw.Sparkline(i)
|
|
||||||
t.Logf("count: %d, len %d: %+v", i, len([]rune(line)), line)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("decreasing")
|
|
||||||
for i := 0; i < buckets; i++ {
|
|
||||||
bw.buckets[i] = int64((buckets - i) * buckets)
|
|
||||||
}
|
|
||||||
for i := 1; i < buckets; i++ {
|
|
||||||
line := bw.Sparkline(i)
|
|
||||||
t.Logf("count: %d, len %d: %+v", i, len([]rune(line)), line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSparkRandom(t *testing.T) {
|
|
||||||
buckets := 20
|
|
||||||
bw := BPS{
|
|
||||||
buckets: make([]int64, buckets),
|
|
||||||
}
|
|
||||||
bw.buckets[3] = 100
|
|
||||||
t.Logf("%+v", bw.Sparkline(10))
|
|
||||||
bw.buckets[7] = 300
|
|
||||||
for i := 1; i < buckets; i++ {
|
|
||||||
line := bw.Sparkline(i)
|
|
||||||
t.Logf("count: %d, len: %d: %+v", i, len([]rune(line)), line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user