-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathmain.go
181 lines (147 loc) · 3.9 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
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"flag"
"fmt"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/assetnote/kiterunner/pkg/log"
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
)
const (
responseSize = 1024
)
var (
requestCount count32
)
type count32 struct {
val uint32
}
func (c *count32) increment() {
atomic.AddUint32(&c.val, 1)
}
func (c *count32) get() uint32 {
return atomic.LoadUint32(&c.val)
}
func PreRequest() {
time.Sleep(0* time.Millisecond)
requestCount.increment()
}
func Index(ctx *fasthttp.RequestCtx) {
PreRequest()
ctx.WriteString("Welcome!")
}
func ASDFResponder(ctx *fasthttp.RequestCtx) {
PreRequest()
ctx.WriteString("asdf!")
}
func Hello(ctx *fasthttp.RequestCtx) {
PreRequest()
fmt.Fprintf(ctx, "Hello, %s!\n", ctx.UserValue("name"))
}
func WildcardResponder(ctx *fasthttp.RequestCtx) {
PreRequest()
fmt.Fprintf(ctx, "get %s\n", ctx.RequestURI())
// log.Info().Msgf("got: %s", ctx.RequestURI())
ctx.SetStatusCode(200)
}
func RedirectResponder(ctx *fasthttp.RequestCtx) {
PreRequest()
fmt.Fprintf(ctx, "go to, %s!\n", ctx.UserValue("dest"))
ctx.SetStatusCode(302)
ctx.Response.Header.Add("location", "/"+ctx.UserValue("dest").(string))
}
func UserWildcardResponder(ctx *fasthttp.RequestCtx) {
PreRequest()
log.Info().
Bytes("method",ctx.Method()).
Bytes("uri", ctx.RequestURI()).Msg("got user request")
switch string( ctx.Method() ) {
case "GET":
fmt.Fprintf(ctx, "get %s user woo\n", ctx.RequestURI())
default:
ctx.SetStatusCode(302)
ctx.Response.Header.Add("location", string(ctx.RequestURI()))
}
}
func APIWildcardResponder(ctx *fasthttp.RequestCtx) {
PreRequest()
fmt.Fprintf(ctx, "get %s\n", ctx.RequestURI())
}
func StatsFunc(end <-chan bool) {
// rolling average
lastRequest := time.Now()
lastRequestCount := requestCount.get()
rpsPeak := float64(0)
for {
select {
case <-end:
fmt.Println("\nTerminating.")
return
default:
timeDiff := time.Since(lastRequest).Seconds()
curRequestCount := requestCount.get()
requestCountDiff := curRequestCount - lastRequestCount
rps := float64(requestCountDiff) / timeDiff
if rps > rpsPeak {
rpsPeak = rps
}
fmt.Printf("Total Requests: %d. Requests since last checkin: %d. RPS: %f. Peak: %f\t\t\t\t\r", curRequestCount, requestCountDiff, rps, rpsPeak)
lastRequest = time.Now()
lastRequestCount = curRequestCount
time.Sleep(1 * time.Second)
}
}
}
func main() {
var portRange string
flag.StringVar(&portRange, "p", "14000-14500", "Range of ports to start servers on")
flag.Parse()
flagParts := strings.Split(portRange, "-")
if len(flagParts) != 2 {
log.Fatal().Msg("Invalid portRange. Format should be <int>-<int>")
}
startPort, err := strconv.Atoi(flagParts[0])
if err != nil {
log.Fatal().Msgf("Unable to parse port: %s", err)
}
endPort, err := strconv.Atoi(flagParts[1])
if err != nil {
log.Fatal().Msgf("Unable to parse port: %s", err)
}
r := router.New()
r.GET("/", Index)
r.GET("/_search", ASDFResponder)
r.GET("/hello/:name", Hello)
r.GET("/redir/{dest:*}", RedirectResponder)
r.GET("/api/{req:*}", APIWildcardResponder)
r.GET("/api/user/{req:*}", UserWildcardResponder)
r.POST("/api/user/{req:*}", UserWildcardResponder)
r.Handle("*", "/{req:*}", WildcardResponder)
var wg sync.WaitGroup
// Need just 1 more port for integration tests to pass
wg.Add(1)
go func(port int) {
Host := fmt.Sprintf(":%d", port)
// log.Printf("Starting server on %s", Host)
log.Fatal().Err(fasthttp.ListenAndServe(Host, r.Handler)).Msg("failed to start server")
wg.Done()
}(9200)
for i := startPort; i < endPort; i++ {
wg.Add(1)
go func(port int) {
Host := fmt.Sprintf(":%d", port)
// log.Printf("Starting server on %s", Host)
log.Fatal().Err(fasthttp.ListenAndServe(Host, r.Handler)).Msg("failed to start server")
wg.Done()
}(i)
}
statsFunc := make(chan bool, 0)
go StatsFunc(statsFunc)
wg.Wait()
statsFunc <- true
close(statsFunc)
}