mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
add mock collector
This commit is contained in:
parent
4b169d6a1b
commit
271a059d3a
52
collector/mock.go
Normal file
52
collector/mock.go
Normal file
@ -0,0 +1,52 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Mock collector
|
||||
type Mock struct {
|
||||
Metrics
|
||||
stream chan Metrics
|
||||
done chan bool
|
||||
}
|
||||
|
||||
func NewMock() *Mock {
|
||||
c := &Mock{
|
||||
Metrics: Metrics{},
|
||||
stream: make(chan Metrics),
|
||||
done: make(chan bool),
|
||||
}
|
||||
c.MemLimit = 2147483648
|
||||
go c.run()
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Mock) run() {
|
||||
rand.Seed(int64(time.Now().Nanosecond()))
|
||||
for {
|
||||
c.CPUUtil += rand.Intn(10)
|
||||
if c.CPUUtil > 100 {
|
||||
c.CPUUtil = 0
|
||||
}
|
||||
c.CPUUtil += rand.Intn(2)
|
||||
c.NetTx += rand.Int63n(30)
|
||||
c.NetRx += rand.Int63n(30)
|
||||
c.MemUsage += rand.Int63n(c.MemLimit / 16)
|
||||
if c.MemUsage > c.MemLimit {
|
||||
c.MemUsage = 0
|
||||
}
|
||||
c.MemPercent = round((float64(c.MemUsage) / float64(c.MemLimit)) * 100)
|
||||
c.stream <- c.Metrics
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Mock) Stream() chan Metrics {
|
||||
return c.stream
|
||||
}
|
||||
|
||||
func (c *Mock) Stop() {
|
||||
c.done <- true
|
||||
}
|
Loading…
Reference in New Issue
Block a user