-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
168 lines (129 loc) · 3.65 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
package main
import (
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"syscall"
"time"
)
const (
CONFIG_FNAME = "config.json"
ARTEFACTS_FOLDER_NAME = "artefacts"
VERIFY_SSL = false
)
func main() {
config := GetConfig(CONFIG_FNAME)
results := make(map[string]*[]*TaskResult)
fmt.Println("Hello there")
ClearDirectory(ARTEFACTS_FOLDER_NAME)
for _, task := range config.Tasks {
results[task.Name] = runTask(&task, config)
fmt.Printf("Done: %v\n", task.Name)
}
fmt.Println("---------")
for _, task := range config.Tasks {
fmt.Println(task.Name)
hashes := make(map[string]bool)
for _, r := range *results[task.Name] {
fmt.Println(r.Duration)
hashes[r.HashString] = true
}
for hashString, _ := range hashes {
fmt.Println(hashString)
}
fmt.Println("---------")
}
fmt.Println("All done. Goodbye")
}
func runTask(task *Task, config *Config) *[]*TaskResult {
config.Printf("Running task %v\n", task.Name)
c := make(chan int)
// Prepare the target server
go runCommand(c, task.Command, config.Verbose)
results := make([]*TaskResult, config.AttemptsPerTask)
for pid := range c {
config.Println("Waiting for the server")
time.Sleep(20 * time.Second) // TODO: add healthcheck
config.Println("Server is ready (probably)")
// A hook to run additional actions before each request e.g. clear the cache.
if task.PreRequestCommand != "" {
runCommandSync(task.PreRequestCommand, config.Verbose)
}
for i := 0; i < config.AttemptsPerTask; i++ {
results[i] = doRequest(i, task, config)
}
// TODO: kill the task even if there is an error happened before
// (doRequest may fail with log.Fatal and I probably want to change that)
config.Printf("Done. [%v]\n\n", pid)
syscall.Kill(-pid, syscall.SIGKILL)
break
}
close(c)
return &results
}
// runCommand runs the command without waiting for it to complete.
func runCommand(c chan int, command string, verbose int) {
cmd := exec.Command(command)
if verbose > 1 {
cmd.Stdout = os.Stdout
}
// To make it possible to kill it with all the children later.
// https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
if verbose > 1 {
log.Printf("Just ran subprocess %d, exiting\n", cmd.Process.Pid)
}
c <- cmd.Process.Pid
}
// runCommandSync runs the command and waits for it to complete.
func runCommandSync(command string, verbose int) {
cmd := exec.Command(command)
if verbose > 1 {
cmd.Stdout = os.Stdout
}
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
// doRequest does a request to API endpoint and measure the time spent.
func doRequest(i int, task *Task, config *Config) *TaskResult {
// TODO: init only once?
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !VERIFY_SSL},
}
client := &http.Client{Transport: tr}
start := time.Now()
resp, err := client.Get(task.RequestURL)
end := time.Now()
if err != nil {
log.Fatal(err) // TODO: return Error instead for the caller to handle
}
if resp.StatusCode != 200 {
log.Fatalf("Invalid status code: %s", resp.StatusCode)
}
duration := end.Sub(start)
config.Printf("[%v] %v %v\n", i, duration, resp.ContentLength)
defer resp.Body.Close()
var body []byte
rawBody, _ := ioutil.ReadAll(resp.Body)
contentType := resp.Header.Get("Content-Type")
if contentType == "application/json" {
body = JSONPrettyfier(rawBody)
} else {
body = rawBody
}
hash_string := DumpBody(body, ARTEFACTS_FOLDER_NAME, task.Name)
return &TaskResult{
Duration: duration,
HashString: hash_string,
}
}