Skip to content

Commit

Permalink
UI: make stop load test button works
Browse files Browse the repository at this point in the history
  • Loading branch information
andylibrian committed Dec 19, 2020
1 parent a6fc977 commit a448343
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
15 changes: 15 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (s *Server) setupRouter() (*httprouter.Router, error) {
router.GET("/cluster/join", s.acceptWorkerConn)
router.GET("/notifications", s.acceptNotificationConn)
router.POST("/api/v1/load_test", s.handleStartLoadTest)
router.DELETE("/api/v1/load_test", s.handleStopLoadTest)

// CORS
router.GlobalOPTIONS = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -159,6 +160,11 @@ func (s *Server) StartLoadTest(r *messages.StartLoadTestRequest) {
s.GetWorkerService().BroadcastMessageToWorkers(envelope)
}

func (s *Server) StopLoadTest() {
envelope, _ := json.Marshal(messages.Envelope{Kind: messages.KindStopLoadTestRequest})
s.GetWorkerService().BroadcastMessageToWorkers(envelope)
}

func (s *Server) watchWorkerStateChange() {
for {
<-s.workerService.stateUpdatedCh
Expand Down Expand Up @@ -221,3 +227,12 @@ func (s *Server) handleStartLoadTest(responseWriter http.ResponseWriter, req *ht
responseWriter.WriteHeader(200)
responseWriter.Write([]byte("ok"))
}

func (s *Server) handleStopLoadTest(responseWriter http.ResponseWriter, req *http.Request, _ httprouter.Params) {
s.StopLoadTest()

header := responseWriter.Header()
header.Set("Access-Control-Allow-Origin", "*")

responseWriter.WriteHeader(204)
}
15 changes: 12 additions & 3 deletions pkg/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,18 @@ func (h *defaultMessageHandler) HandleMessage(message []byte) {
Body: []byte(req.Body),
})

h.worker.resetLoadTest()
go h.worker.startLoadTest(targeter, rate, duration, "terjang")
} else if envelope.Kind == messages.KindStopLoadTestRequest {
h.worker.stopLoadTest()
}
}

func (w *Worker) resetLoadTest() {
w.attacker = vegeta.NewAttacker()
w.metrics = vegeta.Metrics{}
}

func (w *Worker) startLoadTest(tr vegeta.Targeter, p vegeta.Pacer, du time.Duration, name string) {
w.loadTestState = messages.WorkerStateRunning
w.sendWorkerInfoToServer()
Expand All @@ -164,14 +170,17 @@ func (w *Worker) startLoadTest(tr vegeta.Targeter, p vegeta.Pacer, du time.Durat
w.metrics.Add(res)
}

w.loadTestState = messages.WorkerStateDone
// Preserves state if it's stopped
if w.loadTestState != messages.WorkerStateStopped {
w.loadTestState = messages.WorkerStateDone
}

w.sendWorkerInfoToServer()
}

func (w *Worker) stopLoadTest() {
w.attacker.Stop()

w.loadTestState = messages.WorkerStateStopped
w.attacker.Stop()
}

func (w *Worker) LoopSendMetricsToServer() {
Expand Down

0 comments on commit a448343

Please sign in to comment.