ctop/connector/collector/mock.go

84 lines
1.4 KiB
Go
Raw Normal View History

// +build !release
2017-06-12 14:12:03 +00:00
package collector
2017-01-30 05:25:56 +00:00
import (
"math/rand"
"time"
2017-06-12 14:12:03 +00:00
2017-06-27 15:46:03 +00:00
"github.com/bcicen/ctop/models"
2017-01-30 05:25:56 +00:00
)
// Mock collector
type Mock struct {
2017-06-27 15:46:03 +00:00
models.Metrics
stream chan models.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{
2017-06-27 15:46:03 +00:00
Metrics: models.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
2017-06-27 15:46:03 +00:00
c.stream = make(chan models.Metrics)
go c.run()
}
func (c *Mock) Stop() {
c.done = true
}
2017-06-27 15:46:03 +00:00
func (c *Mock) Stream() chan models.Metrics {
return c.stream
}
func (c *Mock) Logs() LogCollector {
return &MockLogs{make(chan bool)}
}
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-06-13 20:25:58 +00:00
// set to random static value, once
c.Pids = rand.Intn(12)
c.IOBytesRead = rand.Int63n(8098) * c.aggression
c.IOBytesWrite = rand.Int63n(8098) * c.aggression
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/512) * c.aggression
2017-01-30 05:25:56 +00:00
if c.MemUsage > c.MemLimit {
c.MemUsage = 0
}
2017-06-10 13:00:54 +00:00
c.MemPercent = percent(float64(c.MemUsage), float64(c.MemLimit))
2017-01-30 05:25:56 +00:00
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
}