1
0
mirror of https://github.com/bcicen/ctop.git synced 2024-08-30 18:23:19 +00:00
ctop/metrics/mock.go

73 lines
1.1 KiB
Go
Raw Normal View History

// +build !release
2017-02-23 02:03:55 +00:00
package metrics
2017-01-30 05:25:56 +00:00
import (
"math/rand"
"time"
)
// Mock collector
type Mock struct {
Metrics
stream chan Metrics
done bool
running bool
aggression int64
2017-01-30 05:25:56 +00:00
}
func NewMock(a int64) *Mock {
2017-01-30 05:25:56 +00:00
c := &Mock{
Metrics: Metrics{},
aggression: a,
2017-01-30 05:25:56 +00:00
}
c.MemLimit = 2147483648
return c
}
func (c *Mock) Running() bool {
return c.running
}
func (c *Mock) Start() {
c.done = false
c.stream = make(chan Metrics)
go c.run()
}
func (c *Mock) Stop() {
c.done = true
}
func (c *Mock) Stream() chan Metrics {
return c.stream
}
2017-01-30 05:25:56 +00:00
func (c *Mock) run() {
c.running = true
2017-01-30 05:25:56 +00:00
rand.Seed(int64(time.Now().Nanosecond()))
defer close(c.stream)
2017-01-30 05:25:56 +00:00
for {
c.CPUUtil += rand.Intn(2) * int(c.aggression)
if c.CPUUtil >= 100 {
2017-01-30 05:25:56 +00:00
c.CPUUtil = 0
}
c.NetTx += rand.Int63n(60) * c.aggression
c.NetRx += rand.Int63n(60) * c.aggression
c.MemUsage += rand.Int63n(c.MemLimit/16) * c.aggression
2017-01-30 05:25:56 +00:00
if c.MemUsage > c.MemLimit {
c.MemUsage = 0
}
c.MemPercent = round((float64(c.MemUsage) / float64(c.MemLimit)) * 100)
c.stream <- c.Metrics
if c.done {
break
}
2017-01-30 05:25:56 +00:00
time.Sleep(1 * time.Second)
}
c.running = false
2017-01-30 05:25:56 +00:00
}