-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
269 lines (247 loc) · 8.08 KB
/
main.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
Copyright (c) Facebook, Inc. and its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
_ "net/http/pprof"
"github.com/facebook/dns/goose/query"
"github.com/facebook/dns/goose/report"
"github.com/facebook/dns/goose/stats"
"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
"go.uber.org/ratelimit"
)
const pprofHTTP = "localhost:6060"
var (
daemon bool
logLevel string
totalQueries int
dport int
// names string
domain string
host string
logging bool
timeout time.Duration
duration time.Duration
samplingInterval time.Duration
debugger bool
maxqps int
randomiseQueries bool
qTypeStr string
monitorPort int
monitorHost string
parallelConnections int
reportJSON bool
inputFile string
exporterAddr string
)
func main() {
flag.BoolVar(&daemon, "daemon", false,
"Running in daemon mode means that metrics will be exported rather than printed to stdout")
flag.StringVar(&logLevel, "loglevel", "info", "Set a log level. Can be: debug, info, warning, error")
flag.IntVar(&totalQueries, "total-queries", 50000, "Total queries to send")
flag.IntVar(&dport, "port", 53, "destination port")
flag.StringVar(&domain, "domain", "", "Domain for uncached queries")
flag.StringVar(&qTypeStr, "query-type", "A", "Query type to be used for the query")
flag.StringVar(&inputFile, "input-file", "", "The file that contains queries to be made in qname qtype format")
flag.StringVar(&host, "host", "127.0.0.1", "IP address of DNS server to test")
flag.BoolVar(&logging, "enable-logging", true, "Whether to enable logging or not")
flag.BoolVar(&randomiseQueries, "randomise-queries", false, "Whether to randomise dns queries to bypass potential caching")
flag.IntVar(&monitorPort, "monitor-port", 8953, "DNS queries not sent if this port is down on the monitored host (defaults to unbound remote-control port)")
flag.StringVar(&monitorHost, "monitor-host", "127.0.0.1", "DNS queries not sent if the monitored port on this host is down")
flag.StringVar(&exporterAddr, "exporter-addr", ":6869", "Exporter bind address")
flag.DurationVar(&duration, "max-duration", 0*time.Second, "Maximum duration of test (seconds)")
flag.DurationVar(&timeout, "timeout", 3*time.Second, "Duration of timeout for queries")
flag.DurationVar(&samplingInterval, "sample", 0*time.Second, "Sampling frequency for reporting (seconds)")
flag.BoolVar(&debugger, "pprof", false, "Enable pprof")
flag.IntVar(&maxqps, "max-qps", 0, "max number of QPS")
flag.IntVar(¶llelConnections, "parallel-connections", 1, "max number of parallel connections")
flag.BoolVar(&reportJSON, "report-json", false, "Report run results to stdout in json format")
flag.Parse()
switch logLevel {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warning":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
default:
log.Fatalf("Unrecognized log level: %v", logLevel)
}
if domain == "" && inputFile == "" {
log.Fatal("Need to specify either domain or input file, neither is specified")
}
if domain != "" && inputFile != "" {
log.Fatal("Need to specify either domain or input file, both are specified, please only specify one of them")
}
qnames := make([]string, 0)
qtypes := make([]dns.Type, 0)
var err error
if inputFile != "" {
qnames, qtypes, err = query.ProcessQueryInputFile(inputFile)
if err != nil {
log.Fatalf("Failed to process query input file: %s %v", inputFile, err)
}
} else {
qnames = []string{domain}
qType, qtypeErr := query.QTypeStrToDNSQtype(qTypeStr)
if qtypeErr != nil {
log.Fatalf("%v", err)
}
qtypes = []dns.Type{qType}
}
sigStop := make(chan os.Signal, 1)
sigPause := make(chan struct{}, 1)
var reporter stats.Reporter = &report.LogStatsReporter{}
if reportJSON {
reporter = &report.JSONStatsReporter{}
}
// Do not stop unless interrupted
signal.Notify(sigStop, syscall.SIGINT) // ^C (Control-C).
signal.Notify(sigStop, syscall.SIGQUIT) // ^\ (Control-Backslash)
signal.Notify(sigStop, syscall.SIGTERM) // kill/pkill etc
if debugger {
log.Infof("Starting profiler on %s", pprofHTTP)
go func() {
listenError := http.ListenAndServe(pprofHTTP, nil)
if listenError != nil {
log.Errorf("failed to start pprof: %s", listenError.Error())
}
}()
}
if daemon {
log.Infof("Running in daemon mode")
log.Infof("Monitor Host/Port is %s:%d", monitorHost, monitorPort)
query.MonitorTarget(sigPause, monitorPort, monitorHost)
// @fb-only: reporter = &report.ODSMetricsReporter{Prefix: "dns.goose", Addr: exporterAddr}
reporter = &report.PrometheusMetricsReporter{Addr: exporterAddr} // @oss-only
// Do nothing on SIGHUP (terminal disconnect)
signal.Notify(make(chan os.Signal, 1), syscall.SIGHUP)
} else {
log.Infof("The total number of DNS requests will be: %v", totalQueries)
}
goosestr := `
_______________________________
< Honking at %s:%d with %s QPS.>
--------------------------------
\
\
\
___
.^ ""-.
_.-^( e _ '.
'-===.>_.-^ '. "
" "
: "
: | __.--._
| '--" ""-._ _.^)
/ ""-^ _>
: -^>
: .__> __)
\ '._ .__.-' .-'
'.___ '-.__.-' /
'-.__ . _.' /
\_____> >'.__/_.""
.'.----' | |
.' / | |
'^-/ ___| :
>-- /
>.'.'
'-^`
go func() {
reporterInitError := reporter.Initialize()
if reporterInitError != nil {
log.Errorf("Failed to initialize stats reporter %v", reporterInitError)
}
}()
var rate ratelimit.Limiter
qpsStr := "Unlimited"
if maxqps > 0 {
log.Infof("Limiting max qps to: %d", maxqps)
rate = ratelimit.New(maxqps)
qpsStr = fmt.Sprint(maxqps)
} else {
rate = ratelimit.NewUnlimited()
}
log.Infof(goosestr, host, dport, qpsStr)
runState := query.NewRunState(totalQueries, rate, daemon, time.Now)
if duration != 0 && !daemon {
timer := time.NewTimer(duration)
go func() {
<-timer.C
log.Info("Max duration reached. Exiting.")
close(sigStop)
}()
}
periodicReporterStop := make(chan os.Signal, 1)
if samplingInterval != 0 {
log.Infof("Sampling interval is %v", samplingInterval)
samplingTicker := time.NewTicker(samplingInterval)
go func() {
for {
select {
case <-samplingTicker.C:
sampledMetrics := runState.ExportIntermediateResults()
repErr := reporter.ReportMetrics(sampledMetrics)
if err != nil {
log.Errorf("Failed to report metrics %v", repErr)
}
case <-periodicReporterStop:
return
}
}
}()
}
go func() {
log.Infof(
"Starting the test and running %d connections in parallel",
parallelConnections,
)
var wg sync.WaitGroup
for i := 0; i < parallelConnections; i++ {
wg.Add(1)
go func() {
qErr := query.RunQueries(dport, host, qnames, timeout, randomiseQueries, qtypes, time.Now, runState, sigPause)
if err != nil {
log.Errorf("Failed to run queries %v", qErr)
}
wg.Done()
}()
}
wg.Wait()
log.Info("Finished running all connections")
close(sigStop)
}()
select { // nolint: gosimple
case <-sigStop:
log.Infof("No more requests will be sent")
close(periodicReporterStop)
}
if !daemon {
log.Info("The test results are:")
finishedMetrics := runState.ExportResults()
err = reporter.ReportMetrics(finishedMetrics)
if err != nil {
log.Errorf("Failed to report metrics %v", err)
}
}
}