-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel_req.go
72 lines (62 loc) · 1.55 KB
/
channel_req.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
package main
import (
"fmt"
"log"
"io/ioutil"
"net/http"
"os"
"strconv"
"time"
)
func sendRequest(url string, ch chan<- string, printResponse int) {
start := time.Now()
client := &http.Client{}
resp, err := client.Get(url)
if err != nil {
log.Fatal(err)
return
}
defer resp.Body.Close()
ms := time.Since(start).Milliseconds()
if printResponse == 1 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return
}
fmt.Println("Response:", string(body))
}
statusCode := resp.StatusCode
if statusCode == 200 {
ch <- fmt.Sprintf("Response Time: %dms | Status Code: %d", ms, statusCode)
} else {
ch <- fmt.Sprintf("Server can't handle the requests: %dms | Status Code: %d", ms, statusCode)
}
}
func main() {
if len(os.Args) != 4 {
fmt.Printf("Usage: %s <url> <parallel requests> <print response>\n ", os.Args[0])
fmt.Printf("Example for 1 request printing response: %s https://www.google.com 1 1\n ", os.Args[0])
fmt.Printf("Example for 100 parallel requests not printing response: %s https://www.google.com 100 0\n ", os.Args[0])
os.Exit(1)
}
url := os.Args[1]
tries, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println("Type the number of parallel requests")
os.Exit(1)
}
printResponse, err := strconv.Atoi(os.Args[3])
if err != nil {
fmt.Println("Type '1' if you want to print the response")
os.Exit(1)
}
ch := make(chan string)
for i := 1; i <= tries; i++ {
go sendRequest(url, ch, printResponse)
}
for i := 1; i <= tries; i++ {
fmt.Println("#", i, "->", <-ch)
}
os.Exit(0)
}