-
Notifications
You must be signed in to change notification settings - Fork 3
/
bufferbloater.go
211 lines (173 loc) · 4.59 KB
/
bufferbloater.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"flag"
"io/ioutil"
"sync"
"time"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
"allen.gg/bufferbloater/client"
"allen.gg/bufferbloater/server"
"allen.gg/bufferbloater/stats"
)
var statsDataDir = flag.String("data_dir", "bufferbloater_data", "Specifies the directory to drop the CSV data")
type Bufferbloater struct {
log *zap.SugaredLogger
c []*client.Client
s []*server.Server
statsMgr *stats.StatsMgr
}
type clientConfig struct {
Workload []struct {
Rps uint
Duration string
} `yaml:"workload"`
RqTimeout string `yaml:"rq_timeout"`
TargetServer struct {
Address string
Port uint
} `yaml:"target_server"`
RetryCount int `yaml:"retry_count"`
}
type serverConfig struct {
Profile []struct {
LatencyDistribution []struct {
Weight uint `yaml:"weight"`
Latency string `yaml:"latency"`
} `yaml:"latency_distribution"`
Duration string
} `yaml:"profile"`
ListenPort uint `yaml:"listen_port"`
Threads uint `yaml:"threads"`
}
// Basic representation of the parsed yaml file before the durations are parsed.
type parsedYamlConfig struct {
Clients []clientConfig `yaml:"clients"`
Servers []serverConfig `yaml:"servers"`
}
// Creates a properly typed client config.
func clientConfigParse(cc clientConfig) (client.Config, error) {
// TODO: validate config
conf := client.Config{
TargetServer: client.Target{
Address: cc.TargetServer.Address,
Port: cc.TargetServer.Port,
},
RetryCount: cc.RetryCount,
}
d, err := time.ParseDuration(cc.RqTimeout)
if err != nil {
return client.Config{}, err
}
conf.RequestTimeout = d
for _, stage := range cc.Workload {
d, err := time.ParseDuration(stage.Duration)
if err != nil {
return client.Config{}, err
}
workloadStage := client.WorkloadStage{
RPS: stage.Rps,
Duration: d,
}
conf.Workload = append(conf.Workload, workloadStage)
}
return conf, nil
}
func serverConfigParse(sc serverConfig) (server.Config, error) {
// TODO: validate config
serverConfig := server.Config{
ListenPort: sc.ListenPort,
Threads: sc.Threads,
}
for _, segment := range sc.Profile {
s := server.LatencySegment{}
// Calculate the latency distribution.
s.WeightSum = 0
s.LatencyDistribution = []server.WeightedLatency{}
for _, wl := range segment.LatencyDistribution {
l, err := time.ParseDuration(wl.Latency)
if err != nil {
return server.Config{}, err
}
weightedLatency := server.WeightedLatency{
Weight: wl.Weight,
Latency: l,
}
s.LatencyDistribution = append(s.LatencyDistribution, weightedLatency)
s.WeightSum += wl.Weight
}
d, err := time.ParseDuration(segment.Duration)
if err != nil {
return server.Config{}, err
}
s.SegmentDuration = d
serverConfig.Profile = append(serverConfig.Profile, s)
}
return serverConfig, nil
}
func parseConfigFromFile(configFilename string) (parsedYamlConfig, error) {
// Read the config file.
data, err := ioutil.ReadFile(configFilename)
if err != nil {
return parsedYamlConfig{}, err
}
// Parse the config file yaml.
var parsedConfig parsedYamlConfig
err = yaml.UnmarshalStrict([]byte(data), &parsedConfig)
if err != nil {
return parsedYamlConfig{}, err
}
return parsedConfig, nil
}
func NewBufferbloater(configFilename string, logger *zap.SugaredLogger) (*Bufferbloater, error) {
bb := Bufferbloater{
log: logger,
statsMgr: stats.NewStatsMgrImpl(logger),
}
parsedConfig, err := parseConfigFromFile(configFilename)
if err != nil {
bb.log.Fatalw("failed to parse yaml file",
"error", err)
}
// Create clients.
for tid, arg := range parsedConfig.Clients {
cc, err := clientConfigParse(arg)
if err != nil {
bb.log.Fatalw("failed to create server config",
"error", err)
}
bb.c = append(bb.c, client.NewClient(uint(tid), cc, logger, bb.statsMgr))
}
// Create servers.
for tid, arg := range parsedConfig.Servers {
sc, err := serverConfigParse(arg)
if err != nil {
bb.log.Fatalw("failed to create server config",
"error", err)
}
bb.s = append(bb.s, server.NewServer(uint(tid), sc, logger, bb.statsMgr))
}
return &bb, nil
}
func (bb *Bufferbloater) Run() {
// TODO: make folder configurable.
defer bb.statsMgr.DumpStatsToFolder(*statsDataDir)
stopStats := make(chan struct{}, 1)
var statsWg sync.WaitGroup
statsWg.Add(1)
go bb.statsMgr.PeriodicStatsCollection(500*time.Millisecond, stopStats, &statsWg)
var wg sync.WaitGroup
// Start servers.
for _, s := range bb.s {
wg.Add(1)
go s.Start(&wg)
}
// Start clients.
for _, c := range bb.c {
wg.Add(1)
go c.Start(&wg)
}
wg.Wait()
stopStats <- struct{}{}
statsWg.Wait()
}