-
Notifications
You must be signed in to change notification settings - Fork 0
/
barometer.go
91 lines (84 loc) · 2.21 KB
/
barometer.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
package hwapi
import (
"encoding/json"
"strings"
)
// TraceRoute result list
type TraceRoute struct {
List []*TraceRouteResponse `json:"list"`
}
// TraceRouteResponse traceroute info
type TraceRouteResponse struct {
DataCenter string //POP COde
IPAddress string //Hostname's IPAddress
RoundTripMS int //RTT
HopCount int //hop
}
// BarometerList list contains multiple BarometerResponse
type BarometerList struct {
List []*BarometerResponse `json:"list"`
}
// BarometerResponse http barometer response
type BarometerResponse struct {
Datacenter string //Datacenter
DNSMs int //dnsMS
ConnectMS int //connectMS
SslMS int //sslMS
FirstByteMS int //firstByteMS
DownloadMS int //downloadMS
TotalMS int //totalMS
Status int //Status
Headers map[string]string //Header
}
// BarometerTrace Get the list of traceroute responses for a given url
//hostName is Address to test
//pops is List of pops to test
func (api *HWApi) BarometerTrace(hostName string, pops ...string) (*TraceRouteResponse, error) {
pop := ""
for _, k := range pops {
pop += k + ","
}
var q map[string]string
q["hostname"] = hostName
if pop != "" {
q["pop"] = strings.TrimRight(pop, ",")
}
if r, e := api.Request(&Request{
URL: "/api/v1/barometer/traceroute",
Query: map[string]string{
"hostname": hostName,
"pop": pop,
},
}); e == nil {
al := &TraceRouteResponse{}
return al, json.Unmarshal(r.body, al)
} else {
return nil, e
}
}
// BarometerRequest test performance between POPs and destionation
// hostname is Url to test
// pops is List of pops to test
func (api *HWApi) BarometerRequest(hostName string, pops ...string) (*BarometerResponse, error) {
pop := ""
for _, k := range pops {
pop += k + ","
}
var q map[string]string
q["hostname"] = hostName
if pop != "" {
q["pop"] = strings.TrimRight(pop, ",")
}
if r, e := api.Request(&Request{
URL: "/api/v1/barometer/traceroute",
Query: map[string]string{
"hostname": hostName,
"pop": pop,
},
}); e == nil {
al := &BarometerResponse{}
return al, json.Unmarshal(r.body, al)
} else {
return nil, e
}
}