forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gen.go
140 lines (117 loc) · 2.78 KB
/
gen.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package stress
import (
"fmt"
"sync"
"time"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/atomic"
"github.com/elastic/beats/libbeat/logp"
)
type generateConfig struct {
Worker int `config:"worker" validate:"min=1"`
ACK bool `config:"ack"`
MaxEvents uint64 `config:"max_events"`
WaitClose time.Duration `config:"wait_close"`
PublishMode string `config:"publish_mode"`
Watchdog time.Duration `config:"watchdog"`
}
var defaultGenerateConfig = generateConfig{
Worker: 1,
ACK: false,
MaxEvents: 0,
WaitClose: 0,
Watchdog: 1 * time.Second,
}
var publishModes = map[string]beat.PublishMode{
"": beat.DefaultGuarantees,
"default": beat.DefaultGuarantees,
"guaranteed": beat.GuaranteedSend,
"drop_if_full": beat.DropIfFull,
}
func generate(
cs *closeSignaler,
p beat.Pipeline,
config generateConfig,
id int,
errors func(err error),
) error {
settings := beat.ClientConfig{
WaitClose: config.WaitClose,
}
if config.ACK {
settings.ACKCount = func(n int) {
logp.Info("Pipeline client (%v) ACKS; %v", id, n)
}
}
if m := config.PublishMode; m != "" {
mode, exists := publishModes[m]
if !exists {
err := fmt.Errorf("Unknown publish mode '%v'", mode)
if errors != nil {
errors(err)
}
return err
}
settings.PublishMode = mode
}
client, err := p.ConnectWith(settings)
if err != nil {
panic(err)
}
defer logp.Info("client (%v) closed: %v", id, time.Now())
done := make(chan struct{})
defer close(done)
count := atomic.MakeUint64(0)
var wg sync.WaitGroup
defer wg.Wait()
withWG(&wg, func() {
select {
case <-cs.C(): // stop signal has been received
case <-done: // generate just returns
}
client.Close()
})
if errors != nil && config.Watchdog > 0 {
// start generator watchdog
withWG(&wg, func() {
last := uint64(0)
ticker := time.NewTicker(config.Watchdog) // todo: make ticker interval configurable
defer ticker.Stop()
for {
select {
case <-cs.C():
return
case <-done:
return
case <-ticker.C:
}
current := count.Load()
if last == current {
err := fmt.Errorf("no progress in generators (last=%v, current=%v)", last, current)
errors(err)
}
last = current
}
})
}
logp.Info("start (%v) generator: %v", id, time.Now())
defer logp.Info("stop (%v) generator: %v", id, time.Now())
for cs.Active() {
event := beat.Event{
Timestamp: time.Now(),
Fields: common.MapStr{
"id": id,
"hello": "world",
"count": count,
// TODO: more custom event generation?
},
}
client.Publish(event)
total := count.Inc()
if config.MaxEvents > 0 && total == config.MaxEvents {
break
}
}
return nil
}