mirror of
https://github.com/divan/expvarmon.git
synced 2025-04-27 13:48:55 +08:00
Update test
This commit is contained in:
parent
555bff1b9d
commit
cc1ca679b9
20
stack.go
20
stack.go
@ -8,8 +8,8 @@ const DefaultSize = 1200
|
|||||||
|
|
||||||
// Stack is a limited FIFO for holding sparkline values.
|
// Stack is a limited FIFO for holding sparkline values.
|
||||||
type Stack struct {
|
type Stack struct {
|
||||||
Values []int
|
values []int
|
||||||
Len int
|
len int
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStack inits new Stack with default size limit.
|
// NewStack inits new Stack with default size limit.
|
||||||
@ -20,26 +20,26 @@ func NewStack() *Stack {
|
|||||||
// NewStackWithSize inits new Stack with size limit.
|
// NewStackWithSize inits new Stack with size limit.
|
||||||
func NewStackWithSize(size int) *Stack {
|
func NewStackWithSize(size int) *Stack {
|
||||||
return &Stack{
|
return &Stack{
|
||||||
Values: make([]int, size),
|
values: make([]int, size),
|
||||||
Len: size,
|
len: size,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push inserts data to stack, preserving constant length.
|
// Push inserts data to stack, preserving constant length.
|
||||||
func (s *Stack) Push(v IntVar) {
|
func (s *Stack) Push(v IntVar) {
|
||||||
val := v.Value()
|
val := v.Value()
|
||||||
s.Values = append(s.Values, val)
|
s.values = append(s.values, val)
|
||||||
if len(s.Values) > s.Len {
|
if len(s.values) > s.len {
|
||||||
// TODO: check if underlying array is growing constantly
|
// TODO: check if underlying array is growing constantly
|
||||||
s.Values = s.Values[1:]
|
s.values = s.values[1:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IntValues returns stack values explicitly casted to int.
|
// Values returns stack values explicitly casted to int.
|
||||||
//
|
//
|
||||||
// Main case is to use with termui.Sparklines.
|
// Main case is to use with termui.Sparklines.
|
||||||
func (s *Stack) IntValues() []int {
|
func (s *Stack) Values() []int {
|
||||||
return s.Values
|
return s.values
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: implement trim and resize
|
// TODO: implement trim and resize
|
||||||
|
@ -2,27 +2,13 @@ package main
|
|||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestPushWithFloatAndIntValue(t *testing.T) {
|
|
||||||
s := NewStack()
|
|
||||||
s.Push(Var(int64(0.0))) // from service.go:guessValue
|
|
||||||
s.Push(Var(5.0))
|
|
||||||
s.Push(Var(float64(15.0)))
|
|
||||||
if _, ok := s.Max.(float64); !ok {
|
|
||||||
t.Fatalf("Expected Max to be float64, but it's not")
|
|
||||||
}
|
|
||||||
s.Push(Var(int64(25.0)))
|
|
||||||
if _, ok := s.Max.(int64); !ok {
|
|
||||||
t.Fatalf("Expected Max to be int64, but it's not")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStack(t *testing.T) {
|
func TestStack(t *testing.T) {
|
||||||
size := 10
|
size := 10
|
||||||
s := NewStackWithSize(size)
|
s := NewStackWithSize(size)
|
||||||
|
|
||||||
for i := 0; i < size+5; i++ {
|
for i := 0; i < size+5; i++ {
|
||||||
s.Push(i)
|
s.Push(&Number{float64(i)})
|
||||||
l := len(s.Values)
|
l := len(s.values)
|
||||||
|
|
||||||
if l < size {
|
if l < size {
|
||||||
if l != i+1 {
|
if l != i+1 {
|
||||||
@ -35,33 +21,8 @@ func TestStack(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Front().(int) != 14 {
|
got := s.Values()[9]
|
||||||
t.Fatalf("Front returns wrong value: expecting %d, got %d", 14, s.Front())
|
if got != 14 {
|
||||||
}
|
t.Fatalf("Front returns wrong value: expecting %d, got %d", 14, got)
|
||||||
|
|
||||||
s1 := NewStackWithSize(3)
|
|
||||||
s1.Push(true)
|
|
||||||
s1.Push(false)
|
|
||||||
s1.Push(true)
|
|
||||||
|
|
||||||
ints1 := s1.IntValues()
|
|
||||||
if len(ints1) != 3 {
|
|
||||||
t.Fatalf("expecting len of to be %d, but got %d", 3, len(ints1))
|
|
||||||
}
|
|
||||||
if ints1[0] != 1 || ints1[1] != 0 || ints1[2] != 1 {
|
|
||||||
t.Fatalf("bool values converted to int incorrectly: %v", ints1)
|
|
||||||
}
|
|
||||||
|
|
||||||
s2 := NewStackWithSize(3)
|
|
||||||
s2.Push(0.1)
|
|
||||||
s2.Push(0.5)
|
|
||||||
s2.Push(0.03)
|
|
||||||
|
|
||||||
ints2 := s2.IntValues()
|
|
||||||
if len(ints2) != 3 {
|
|
||||||
t.Fatalf("expecting len to be %d, but got %d", 3, len(ints2))
|
|
||||||
}
|
|
||||||
if ints2[0] != 10 || ints2[1] != 50 || ints2[2] != 3 {
|
|
||||||
t.Fatalf("float values converted to int incorrectly: %v", ints2)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -120,7 +120,7 @@ func (t *TermUI) Update(data UIData) {
|
|||||||
data.Stats[name].Update(v)
|
data.Stats[name].Update(v)
|
||||||
max := data.Stats[name].Max().String()
|
max := data.Stats[name].Max().String()
|
||||||
t.Sparkline1.Lines[i].Title = fmt.Sprintf("%s (max: %s)", service.Name, max)
|
t.Sparkline1.Lines[i].Title = fmt.Sprintf("%s (max: %s)", service.Name, max)
|
||||||
t.Sparkline1.Lines[i].Data = data.Stacks[name].IntValues()
|
t.Sparkline1.Lines[i].Data = data.Stacks[name].Values()
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(data.Vars) == 1 {
|
if len(data.Vars) == 1 {
|
||||||
@ -134,7 +134,7 @@ func (t *TermUI) Update(data UIData) {
|
|||||||
data.Stats[name].Update(v)
|
data.Stats[name].Update(v)
|
||||||
max := data.Stats[name].Max().String()
|
max := data.Stats[name].Max().String()
|
||||||
t.Sparkline2.Lines[i].Title = fmt.Sprintf("%s (max: %s)", service.Name, max)
|
t.Sparkline2.Lines[i].Title = fmt.Sprintf("%s (max: %s)", service.Name, max)
|
||||||
t.Sparkline2.Lines[i].Data = data.Stacks[name].IntValues()
|
t.Sparkline2.Lines[i].Data = data.Stacks[name].Values()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,7 +106,7 @@ func (t *TermUISingle) Update(data UIData) {
|
|||||||
spl.TitleColor = colorByKind(name.Kind())
|
spl.TitleColor = colorByKind(name.Kind())
|
||||||
spl.LineColor = colorByKind(name.Kind())
|
spl.LineColor = colorByKind(name.Kind())
|
||||||
|
|
||||||
spl.Data = data.Stacks[name].IntValues()
|
spl.Data = data.Stacks[name].Values()
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Relayout()
|
t.Relayout()
|
||||||
|
7
var.go
7
var.go
@ -34,8 +34,10 @@ type IntVar interface {
|
|||||||
Value() int
|
Value() int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Number is a type for numeric values, obtained from JSON.
|
||||||
|
// In JSON it's always float64, so there is no straightforward way
|
||||||
|
// to separate float from int, so let's keep everything as float.
|
||||||
type Number struct {
|
type Number struct {
|
||||||
// TODO: add mutex here or level above, in service?
|
|
||||||
val float64
|
val float64
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,6 +60,7 @@ func (v *Number) Value() int {
|
|||||||
return int(v.val)
|
return int(v.val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Memory represents memory information in bytes.
|
||||||
type Memory struct {
|
type Memory struct {
|
||||||
bytes int64
|
bytes int64
|
||||||
}
|
}
|
||||||
@ -80,6 +83,7 @@ func (v *Memory) Value() int {
|
|||||||
return int(v.bytes)
|
return int(v.bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Duration represents duration data (in ns)
|
||||||
type Duration struct {
|
type Duration struct {
|
||||||
dur time.Duration
|
dur time.Duration
|
||||||
}
|
}
|
||||||
@ -105,6 +109,7 @@ func (v *Duration) Value() int {
|
|||||||
return int(v.dur)
|
return int(v.dur)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Strings represents string data.
|
||||||
type String struct {
|
type String struct {
|
||||||
str string
|
str string
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user