-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtests.go
337 lines (306 loc) · 10 KB
/
tests.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package wpt
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/google/go-querystring/query"
)
// Test represents a test within WPT
type Test struct {
RequestID string
StatusChan chan string
Status string
Monitor bool
Client *Client
Response *TestRequest
Results *TestResults
Params *TestParams
}
// TestParams represents all the params available for performing tests
// reference for these params can be found at the following link
// https://sites.google.com/a/webpagetest.org/docs/advanced-features/webpagetest-restful-apis
type TestParams struct {
URL string `url:"url"`
APIKey string `url:"k,omitempty"`
Label string `url:"label,omitempty"`
// locations and connectivity are combined at validation into LocationString
Location string `url:"-"`
Connectivity string `url:"-"`
LocationString string `url:"location,omitempty"`
// Block string array is converted to a space delimited string at validation
Block []string `url:"-"`
BlockString string `url:"block,omitempty"`
MobileDevice string `url:"mobileDevice,omitempty"`
UAString string `url:"uastring,omitempty"`
Login string `url:"login,omitempty"`
Password string `url:"password,omitempty"`
Notify string `url:"notify,omitempty"`
Pingback string `url:"pingback,omitempty"`
CMDLine string `url:"cmdline,omitempty"`
TSViewID string `url:"tsview_id,omitempty"`
Custom string `url:"custom,omitempty"`
Tester string `url:"tester,omitempty"`
Affinity string `url:"affinity,omitempty"`
DomElement string `url:"domelement,omitempty"`
Script string `url:"script,omitempty"`
TimelineStack int `url:"timelineStack,omitempty"`
Runs int `url:"runs,omitempty"`
Connections int `url:"connections,omitempty"`
Authtype int `url:"authType,omitempty"`
BWDown int `url:"bwDown,omitempty"`
BWUp int `url:"bwUp,omitempty"`
Latency int `url:"latency,omitempty"`
PackLossRate int `url:"plr,omitempty"`
IQ int `url:"iq,omitempty"`
// these are integer representations of any booleans
FVOnly int `url:"fvonly,omitempty"`
Private int `url:"private,omitempty"`
StopOnLoad int `url:"web10,omitempty"`
Video int `url:"video,omitempty"`
TCPDump int `url:"tcpdump,omitempty"`
NoOpt int `url:"noopt,omitempty"`
NoImages int `url:"noimages,omitempty"`
NoHeaders int `url:"noheaders,omitempty"`
PNGSS int `url:"pngss,omitempty"`
NoScript int `url:"noscript,omitempty"`
ClearCerts int `url:"clearcerts,omitempty"`
Mobile int `url:"mobile,omitempty"`
MV int `url:"mv,omitempty"`
HTMLBody int `url:"htmlbody,omitempty"`
Timeline int `url:"timeline,omitempty"`
IgnoreSSL int `url:"ignoreSSL,omitempty"`
Lighthouse int `url:"lighthouse,omitempty"`
}
// TestRequest represents the test request
type TestRequest struct {
StatusCode int `json:"statusCode"`
StatusText string `json:"statusText"`
Data struct {
TestID string `json:"testId"`
OwnerKey string `json:"ownerKey"`
JSONURL string `json:"jsonUrl"`
XMLURL string `json:"xmlUrl"`
UserURL string `json:"userUrl"`
SummaryCSV string `json:"summaryCSV"`
DetailCSV string `json:"detailCSV"`
} `json:"data"`
}
// TestStatus represents the state of a specific test within WPT
type TestStatus struct {
StatusCode int `json:"statusCode"`
StatusText string `json:"statusText"`
ID string `json:"id"`
Data struct {
StatusCode int `json:"statusCode"`
StatusText string `json:"statusText"`
ID string `json:"id"`
TestInfo TestParams `json:"testInfo"`
} `json:"data"`
TestID string `json:"testId"`
Runs int `json:"runs"`
FVOnly int `json:"fvonly"`
Remote bool `json:"remote"`
TestsExpected int `json:"testsExpected"`
Location string `json:"location"`
Elapsed int `json:"elapsed"`
BehindCount int `json:"behindCount"`
FVRunsCompleted int `json:"fvRunsCompleted"`
RVRunsCompleted int `json:"rvRunsCompleted"`
}
// TestResults represents the completed test and associated data
type TestResults struct {
StatusCode int `json:"statusCode"`
StatusText string `json:"statusText"`
Data struct {
ID string `json:"id"`
URL string `json:"url"`
Summary string `json:"summary"`
TestURL string `json:"testUrl"`
Location string `json:"location"`
From string `json:"from"`
Connectivity string `json:"connectivity"`
BWDown int `json:"bwDown"`
BWUp int `json:"bwUp"`
Latency int `json:"latency"`
PackLossRate string `json:"plr"`
Mobile int `json:"mobile"`
Label string `json:"label"`
Completed Timestamp `json:"completed"`
Tester string `json:"tester"`
FVOnly bool `json:"fvonly"`
SuccessfulFVRuns int `json:"successfulFVRuns"`
SuccessfulRVRuns int `json:"successfulRVRuns"`
Runs map[string]Run `json:"runs"`
Average Run `json:"average"`
StdDev Run `json:"standardDeviation"`
Median Run `json:"median"`
} `json:"data"`
}
// NewTest takes a TestParams and Client struct and returns a Test
func NewTest(tp *TestParams, c *Client) (*Test, error) {
// validate test params
err := tp.Validate()
if err != nil {
return nil, err
}
return &Test{
Client: c,
Monitor: true,
Params: tp,
Status: testNew,
}, nil
}
// Run executes a test against the defined WPT server
func (t *Test) Run() error {
t.StatusChan = make(chan string)
// send the request to webpagetest
err := t.Client.query(urlRunTest, t.Params.getQueryString(), &t.Response)
if err != nil {
t.setStatus(testFailed)
return err
}
if t.Response.StatusCode != 200 {
t.setStatus(testFailed)
return fmt.Errorf("webpagetest: bad status code %v when submitting test", t.Response.StatusCode)
}
// update the Test struct
t.RequestID = t.Response.Data.TestID
t.Status = testQueued
// call the monitor if set in test to update the Test
if t.Monitor {
go t.monitor()
}
return nil
}
// monitor periodically polls the server for the status of the test
// if the status changes it will transfer the new status over the
// State channel and load the result
func (t *Test) monitor() {
expired := time.After(maximumMonitorPeriod)
defer close(t.StatusChan)
var status string
for {
select {
case <-expired:
t.setStatus(testTimedOut)
return
default:
// sleep for defined interval
time.Sleep(pollingInterval)
// get latest status
status = t.CheckStatus()
// only send update if status has changed
if t.Status != status {
// send status over the status channel
t.setStatus(status)
// if test has finished call function to load results
if status == testFinished || status == testNotFound || status == testCancelled {
err := t.LoadResults()
if err != nil {
t.setStatus(testFailed)
} else {
t.setStatus(testComplete)
}
return
}
}
}
}
}
// CheckStatus returns the current status of the test by quering the testStatus.php endpoint
func (t *Test) CheckStatus() string {
var status TestStatus
err := t.Client.query(urlStatus, fmt.Sprintf("test=%s", t.RequestID), &status)
if err != nil {
fmt.Printf("webpagetest: error checking status for test: %s\n", t.RequestID)
return testError
}
switch {
case status.StatusCode == 100:
return testRunning
case status.StatusCode == 101:
return testQueued
case status.StatusCode == 200:
return testFinished
case status.StatusCode == 400:
return testNotFound
case status.StatusCode == 402:
return testCancelled
default:
fmt.Printf("webpagetest: unknown status code %d returned for test %s\n", status.StatusCode, t.RequestID)
return testError
}
}
// LoadResults is called
func (t *Test) LoadResults() error {
qs := fmt.Sprintf("test=%s", t.RequestID)
err := t.Client.query(urlResults, qs, &t.Results)
if err != nil {
return fmt.Errorf("webpagetest: error loading results for test: %s", t.RequestID)
}
return nil
}
func (t *Test) setStatus(state string) {
select {
case t.StatusChan <- state:
case <-t.StatusChan:
t.StatusChan <- state
}
t.Status = state
}
// Validate checks options present against know valid inputs
func (tp *TestParams) Validate() error {
// check URL is set
if tp.URL == "" {
return errors.New("webpagetest: no url specified")
}
// if Connection is specified check it's valid and Location is also specified
if tp.Connectivity != "" {
if tp.Location == "" {
return errors.New("webpagetest: you must set a location to specify a connection profile")
}
found := false
for _, profile := range connectionProfiles {
if tp.Connectivity == profile {
found = true
}
}
if !found {
return fmt.Errorf("webpagetest: invalid connection profile %s", tp.Connectivity)
}
// if custom is specified check that the bandwidth params are set
if (tp.Connectivity == "custom") && ((tp.BWDown == 0) || (tp.BWUp == 0)) {
return errors.New("webpagetest: you must set BWUp & BWDown to use custom connectivity")
}
tp.LocationString = fmt.Sprintf("%s.%s", tp.Location, tp.Connectivity)
}
// block set convert string array to space delimited string
if tp.Block != nil {
tp.BlockString = strings.Join(tp.Block, " ")
}
return nil
}
func (tp *TestParams) getQueryString() string {
qs, _ := query.Values(tp)
return qs.Encode()
}
// Timestamp wrappes time.Time
type Timestamp time.Time
// MarshalJSON for Timestamp
func (t Timestamp) MarshalJSON() ([]byte, error) {
ts := time.Time(t).Unix()
stamp := fmt.Sprint(ts)
return []byte(stamp), nil
}
// UnmarshalJSON for Timestamp
func (t *Timestamp) UnmarshalJSON(b []byte) error {
pieces := strings.Split(string(b), ".")
ts, err := strconv.ParseInt(pieces[0], 10, 64)
if err != nil {
return err
}
*t = Timestamp(time.Unix(ts, 0))
return nil
}