This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
fake.go
94 lines (76 loc) · 1.94 KB
/
fake.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
package main
import (
"io"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
"github.com/dlsniper/debugger"
)
func fakeTraffic() {
// Wait for the server to start
time.Sleep(1 * time.Second)
pages := []string{"/", "/login", "/logout", "/products", "/product/{productID}", "/basket", "/about"}
activeConns := make(chan struct{}, 10)
c := &http.Client{
Timeout: 10 * time.Second,
}
i := int64(0)
for {
activeConns <- struct{}{}
i++
page := pages[rand.Intn(len(pages))]
// We need to launch this using a closure function to
// ensure that we capture the correct value for the
// two parameters we need: page and i
go func(p string, rid int64) {
// Step 2. Now that the debugger stopped, we can take a goroutines snapshot
// using the 📷 button at the bottom of the left debugger toolbar
// and then filter them, group them, or hide them
makeRequest(activeConns, c, p, rid)
}(page, i)
}
}
func makeRequest(done chan struct{}, c *http.Client, page string, i int64) {
defer func() {
// Unblock the next request from the queue
<-done
}()
debugger.SetLabels(func() []string {
return []string{
"request", "automated",
"page", page,
"rid", strconv.Itoa(int(i)),
}
})
page = strings.Replace(page, "{productID}", "abc-"+strconv.Itoa(int(i)), -1)
r, err := http.NewRequest(http.MethodGet, "http://localhost:8080"+page, nil)
if err != nil {
return
}
resp, err := c.Do(r)
if err != nil {
return
}
defer resp.Body.Close()
_, _ = io.Copy(ioutil.Discard, resp.Body)
debugger.SetLabels(func() []string {
return []string{
"request", "automated",
"page", page,
"rid", strconv.Itoa(int(i)),
"status", "before sleep",
}
})
time.Sleep(time.Duration(10+rand.Intn(40)) + time.Millisecond)
debugger.SetLabels(func() []string {
return []string{
"request", "automated",
"page", page,
"rid", strconv.Itoa(int(i)),
"status", "after sleep",
}
})
}