-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
146 lines (114 loc) · 2.61 KB
/
agent.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
141
142
143
144
145
146
package qube
import (
"context"
"errors"
"fmt"
"time"
"golang.org/x/time/rate"
)
const (
RecIntvl = 1 * time.Second
)
type AgentOptions struct {
Force bool `kong:"negatable,default='false',help='Do not abort test on error. (default: disabled)'"`
}
type Agent struct {
*AgentOptions
ID string
db DBIface
data *Data
rec *Recorder
limiter *rate.Limiter
}
func NewAgent(taskID string, agentNum uint64, options *Options, rec *Recorder, limiter *rate.Limiter) (*Agent, error) {
autoCommit := options.CommitRate == 0
db, err := options.OpenDBWithPing(autoCommit)
if err != nil {
return nil, err
}
data, err := NewData(options, agentNum)
if err != nil {
return nil, err
}
agent := &Agent{
AgentOptions: &options.AgentOptions,
ID: fmt.Sprintf("%s/%d", taskID, agentNum),
db: db,
data: data,
rec: rec,
limiter: limiter,
}
return agent, nil
}
func (agent *Agent) Start(ctx context.Context) error {
defer func() {
agent.db.Close()
agent.data.Close()
}()
_, err := agent.db.Exec("select 'start agent - " + agent.ID + "'")
if err != nil {
return fmt.Errorf("failed to execute start query (%w)", err)
}
err = agent.start0(ctx)
if err != nil && err != EOD {
return err
}
_, err = agent.db.Exec("select 'exit agent - " + agent.ID + "'")
if err != nil {
return fmt.Errorf("failed to execute exit query (%w)", err)
}
return nil
}
func (agent *Agent) start0(ctx context.Context) error {
tkrec := time.NewTicker(RecIntvl)
defer tkrec.Stop()
dpes := []DataPointWithErr{}
defer func() {
if len(dpes) > 0 {
agent.rec.Add(dpes)
}
}()
L:
for {
if agent.limiter != nil {
agent.limiter.Wait(ctx) //nolint:errcheck
}
select {
case <-ctx.Done():
break L
case <-tkrec.C:
agent.rec.Add(dpes)
// Create new slices to avoid race conditions
dpes = []DataPointWithErr{}
default:
// Nothing to do
}
q, err := agent.data.Next()
if err != nil {
return err
}
dur, err := agent.execQuery(ctx, q)
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
break L
} else if err != nil && !agent.Force {
return fmt.Errorf("failed to execute query - %s (%w)", q, err)
}
dpes = append(dpes, DataPointWithErr{
DataPoint: DataPoint{
Time: time.Now().Unix(),
Duration: dur,
},
IsError: err != nil,
})
}
return nil
}
func (agent *Agent) execQuery(ctx context.Context, q string) (time.Duration, error) {
start := time.Now()
_, err := agent.db.ExecContext(ctx, q)
end := time.Now()
if err != nil {
return 0, err
}
return end.Sub(start), nil
}