-
Notifications
You must be signed in to change notification settings - Fork 1
/
tw_load.go
200 lines (172 loc) · 5.3 KB
/
tw_load.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
/*
Package main contains the main file that will load test the twilio endpoint
TODO:
- Test cases
-
*/
package main
import (
"os"
"time"
"fmt"
"flag"
"sort"
"bytes"
"strings"
"strconv"
"net/http"
"net/url"
"io/ioutil"
"encoding/base64"
"encoding/json"
"crypto/hmac"
"crypto/sha1"
"github.com/vaughan0/go-ini"
)
type Config struct {
Token string
Users int
URI string
Concurrency int
Unit float64
}
func (config *Config) populateConfig(config_file string) {
config_data, err := ini.LoadFile(config_file)
check(err)
config.Token, _ = config_data.Get("Auth", "Token")
var user_count, _ = config_data.Get("Load", "Users")
config.Users, _ = strconv.Atoi(user_count)
var concurrency, _ = config_data.Get("Load", "Concurrency")
config.Concurrency, _ = strconv.Atoi(concurrency)
config.URI, _ = config_data.Get("Load", "URI")
var unit_cost, _ = config_data.Get("Cost", "Unit")
config.Unit, _ = strconv.ParseFloat(unit_cost, 64)
}
//Struct for URL Parameters
type Param struct {
Name string
Value string
}
//Sort implementation for Param struct
type ByName []Param
func (a ByName) Len() int { return len(a) }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i]}
func (a ByName) Less(i, j int) bool { return a[i].Name < a[j].Name }
type Request struct {
Params []Param
Hashed string
}
func (request *Request) unmarshal(data_file string, config Config){
data_content, err := ini.LoadFile(data_file)
check(err)
var data, _ = data_content.Get("Data", "Raw")
//unmarshall and store the signature
json.Unmarshal([]byte(data), &request.Params)
request.computeSignature(config)
}
func (request *Request) computeSignature(config Config) {
sort.Sort(ByName(request.Params)) //in place ordering
var buffer bytes.Buffer
buffer.WriteString(config.URI)
for _, param := range request.Params {
buffer.WriteString(param.Name)
buffer.WriteString(param.Value)
}
request.Hashed = base64.StdEncoding.EncodeToString(hmacSHA1([]byte(config.Token), buffer.String()))
}
func (request *Request) post(config Config) Response {
data := url.Values{}
for _, param := range request.Params {
data.Add(param.Name, param.Value)
}
client := &http.Client{}
r, _ := http.NewRequest("POST", config.URI, bytes.NewBufferString(data.Encode()))
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("X-Twilio-Signature", request.Hashed)
start := time.Now()
resp, _ := client.Do(r)
defer resp.Body.Close()
elapsed := time.Since(start)
contents, _ := ioutil.ReadAll(resp.Body)
return Response{Status: resp.Status, Time: elapsed, Content: string(contents)}
}
type Response struct {
Status string
Time time.Duration
Content string
}
//-------------- Utility methods -----------------------------//
func check(e error) {
if e != nil {
panic(e)
}
}
//hmacSHA1 creates the SHA1 hash key for the given string and key
func hmacSHA1(key []byte, content string) []byte {
mac := hmac.New(sha1.New, key)
mac.Write([]byte(content))
return mac.Sum(nil)
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage of %s: ", os.Args[0])
fmt.Fprintf(os.Stderr, "Load test a Twilio endpoint. \nSee tw_load.sample.ini for documentation.\n\nParameters:\n")
flag.PrintDefaults()
}
//----------------End of Utility methods-----------------------//
//worker function that executes requests for user count and sends results across the channel
func worker(c_id int, config Config, request Request, results chan <- Response) {
for u:=1; u <= config.Users; u++ {
results <- request.post(config)
}
}
//---------------End of Worker pool---------------------------//
func main(){
config_file := flag.String("f", "/path/to/config", "Config file to load test a Twilio endpoint")
data_file := flag.String("d", "/path/to/data", "Data file to load test a Twilio endpoint")
flag.Usage = usage
flag.Parse()
//load up config and data
config := Config{}
config.populateConfig(*config_file)
request := Request{}
request.unmarshal(*data_file, config)
//setup the response channel
results := make(chan Response)
start := time.Now()
for c:=1; c <= config.Concurrency; c++ {
go worker(c, config, request, results)
}
fmt.Printf("Benchmarking %s (be patient).....\n", config.URI)
success := 0
failed := 0
var total_time time.Duration
total := config.Concurrency * config.Users
responses := 0
for r:=1; r<=total; r++{
response := <- results
if response.Status == "200 OK" {
success += 1
} else {
failed += 1
}
//check if there is a response body, then increment the responses
if strings.Contains(response.Content, "Body") {
responses += 1
}
total_time += response.Time
fmt.Printf("\r%.0f%%", (float32(success + failed) / float32(total)) * 100)
}
fmt.Printf("\rDone!\n")
fmt.Printf("Document Path: %s\n", config.URI)
fmt.Printf("Concurrency Level: %d\n", config.Concurrency)
fmt.Printf("Users: %d\n", config.Users)
elapsed := time.Since(start)
fmt.Printf("Completed requests: %d\n", success)
fmt.Printf("Failed requests: %d\n", failed)
fmt.Printf("Total time: %s\n", elapsed)
fmt.Printf("Time per request: %s\n", (total_time / time.Duration(total)))
fmt.Println("-----------------------------------")
fmt.Printf("Total recv: %d\n", total)
fmt.Printf("Total sent %d\n", responses)
fmt.Printf("Approx cost: $%.2f\n", (config.Unit * float64(total + responses)))
}