-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
74 lines (69 loc) · 1.75 KB
/
handler.go
File metadata and controls
74 lines (69 loc) · 1.75 KB
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
package loadgen
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func (lg *LoadGen) ServeHTTP(w http.ResponseWriter, r *http.Request) {
url := r.URL.Path
switch url {
case MetricsPathStr:
promhttp.Handler().ServeHTTP(w, r)
case WorkersPathStr:
lg.handleWorkersReq(w, r)
case RampUpDurationPathStr:
lg.handleRampUpDurationReq(w, r)
case RampUpFactorPathStr:
lg.handleRampUpFactorReq(w, r)
}
}
func (lg *LoadGen) handleWorkersReq(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
if val := r.FormValue("val"); val != "" {
workers, err := strconv.Atoi(val)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
lg.setWorkers(workers)
} else {
w.Write([]byte(fmt.Sprint(lg.getWorkersCount())))
}
}
func (lg *LoadGen) handleRampUpDurationReq(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
if val := r.FormValue("val"); val != "" {
dur, err := time.ParseDuration(val)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
lg.setRampUpDuration(dur)
} else {
w.Write([]byte(fmt.Sprint(lg.getRampUpDuration())))
}
}
func (lg *LoadGen) handleRampUpFactorReq(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
if val := r.FormValue("val"); val != "" {
factor, err := strconv.ParseFloat(val, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
lg.setRampUpFactor(factor)
} else {
w.Write([]byte(fmt.Sprint(lg.getRampUpFactor())))
}
}