forked from bcicen/ctop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.go
85 lines (70 loc) · 1.46 KB
/
mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//go:build !release
// +build !release
package collector
import (
"math/rand"
"time"
"github.com/bcicen/ctop/models"
)
// Mock collector
type Mock struct {
models.Metrics
stream chan models.Metrics
done bool
running bool
aggression int64
}
func NewMock(a int64) *Mock {
c := &Mock{
Metrics: models.Metrics{},
aggression: a,
}
c.MemLimit = 2147483648
return c
}
func (c *Mock) Running() bool {
return c.running
}
func (c *Mock) Start() {
c.done = false
c.stream = make(chan models.Metrics)
go c.run()
}
func (c *Mock) Stop() {
c.running = false
c.done = true
}
func (c *Mock) Stream() chan models.Metrics {
return c.stream
}
func (c *Mock) Logs() LogCollector {
return &MockLogs{make(chan bool)}
}
func (c *Mock) run() {
c.running = true
rand.Seed(int64(time.Now().Nanosecond()))
defer close(c.stream)
// 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
for {
c.CPUUtil += rand.Intn(2) * int(c.aggression)
if c.CPUUtil >= 100 {
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
if c.MemUsage > c.MemLimit {
c.MemUsage = 0
}
c.MemPercent = percent(float64(c.MemUsage), float64(c.MemLimit))
c.stream <- c.Metrics
if c.done {
break
}
time.Sleep(1 * time.Second)
}
c.running = false
}