/
options.go
227 lines (178 loc) · 3.94 KB
/
options.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
// SPDX-FileCopyrightText: 2023 Iván Szkiba
// SPDX-FileCopyrightText: 2023 Raintank, Inc. dba Grafana Labs
//
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-License-Identifier: MIT
package dashboard
import (
"errors"
"math"
"net"
"net/url"
"strconv"
"strings"
"time"
)
const (
defaultHost = ""
defaultPort = 5665
defaultPeriod = time.Second * 10
defaultOpen = false
defaultExport = ""
defaultRecord = ""
)
func defaultTags() []string { return []string{"group"} }
type options struct {
Port int
Host string
Period time.Duration
Open bool
Export string
Record string
Tags []string
TagsS string
}
func envopts(env map[string]string) (*options, error) {
opts := &options{
Port: defaultPort,
Host: defaultHost,
Period: defaultPeriod,
Open: defaultOpen,
Export: defaultExport,
Record: defaultRecord,
Tags: defaultTags(),
TagsS: "",
}
if len(env) == 0 {
return opts, nil
}
if v, ok := env[envPort]; ok {
i, e := strconv.Atoi(v)
if e != nil {
return nil, e
}
opts.Port = i
}
if v, ok := env[envHost]; ok {
opts.Host = v
}
if v, ok := env[envExport]; ok {
opts.Export = v
} else if v, ok := env[envReport]; ok {
opts.Export = v
}
if v, ok := env[envRecord]; ok {
opts.Record = v
}
if v, ok := env[envPeriod]; ok {
d, e := time.ParseDuration(v)
if e != nil {
return nil, errInvalidDuration
}
opts.Period = d
}
if v, ok := env[envOpen]; ok && v == "true" {
opts.Open = true
}
if v, ok := env[envTags]; ok {
opts.Tags = strings.Split(v, ",")
}
return opts, nil
}
func getopts(query string, env map[string]string) (*options, error) {
opts, err := envopts(env)
if err != nil {
return nil, err
}
if query == "" {
return opts, nil
}
value, err := url.ParseQuery(query)
if err != nil {
return nil, err
}
if v := value.Get(paramPort); len(v) != 0 {
i, e := strconv.Atoi(v)
if e != nil {
return nil, e
}
opts.Port = i
}
if v := value.Get(paramHost); len(v) != 0 {
opts.Host = v
}
if v := value.Get(paramExport); len(v) != 0 {
opts.Export = v
} else if v := value.Get(paramReport); len(v) != 0 {
opts.Export = v
}
if v := value.Get(paramRecord); len(v) != 0 {
opts.Record = v
}
if v := value.Get(paramPeriod); len(v) != 0 {
d, e := time.ParseDuration(v)
if e != nil {
return nil, errInvalidDuration
}
opts.Period = d
}
if v := value[paramTag]; len(v) != 0 {
opts.Tags = v
}
if value.Has(paramOpen) && (len(value.Get(paramOpen)) == 0 || value.Get(paramOpen) == "true") {
opts.Open = true
}
if v := value.Get(paramTags); len(v) != 0 {
opts.Tags = append(opts.Tags, strings.Split(v, ",")...)
}
return opts, err
}
func (opts *options) addr() string {
if opts.Port < 0 {
return ""
}
return net.JoinHostPort(opts.Host, strconv.Itoa(opts.Port))
}
func (opts *options) url() string {
if opts.Port < 0 {
return ""
}
host := opts.Host
if host == "" {
host = "127.0.0.1"
}
return "http://" + net.JoinHostPort(host, strconv.Itoa(opts.Port))
}
// period adjusts period, limit points per test run to 'points'.
func (opts *options) period(duration time.Duration) time.Duration {
if duration == 0 {
return opts.Period
}
optimal := float64(duration) / float64(points)
return time.Duration(math.Ceil(optimal/float64(opts.Period))) * opts.Period
}
/*
approx. 1MB max report size, 8 hours test run with 10sec event period.
*/
const points = 2880
var errInvalidDuration = errors.New("invalid duration")
const (
envPrefix = "K6_WEB_DASHBOARD_"
paramPort = "port"
envPort = envPrefix + "PORT"
paramHost = "host"
envHost = envPrefix + "HOST"
paramPeriod = "period"
envPeriod = envPrefix + "PERIOD"
paramOpen = "open"
envOpen = envPrefix + "OPEN"
paramReport = "report"
envReport = envPrefix + "REPORT"
paramExport = "export"
envExport = envPrefix + "EXPORT"
paramRecord = "record"
envRecord = envPrefix + "RECORD"
paramTag = "tag"
paramTags = "tags"
envTags = envPrefix + "TAGS"
)