forked from Daniel.Sy/loic-go
fix(#1): 🐛 Fixed application startup
The main process did not get blocked before. Hence a new channel 'TestDone' is added to be closed when the attack is finished.
This commit is contained in:
parent
600e641560
commit
f02994f32c
2 changed files with 62 additions and 22 deletions
|
@ -2,14 +2,15 @@ package cmd
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Daniel.Sy/loic-go/pkg/loic"
|
||||
"github.com/spf13/cobra"
|
||||
"time"
|
||||
)
|
||||
|
||||
var startCmd = &cobra.Command{
|
||||
Use: "start",
|
||||
Short: "Startet einen Lasttest",
|
||||
Short: "Start an attack",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
targetURL, _ := cmd.Flags().GetString("target")
|
||||
concurrency, _ := cmd.Flags().GetInt("concurrency")
|
||||
|
@ -17,6 +18,7 @@ var startCmd = &cobra.Command{
|
|||
rampUp, _ := cmd.Flags().GetDuration("ramp-up")
|
||||
fmt.Printf("Starting test with target %s, concurrency %d, duration %v, ramp-up %v\n", targetURL, concurrency, duration, rampUp)
|
||||
loic.StartTest(targetURL, concurrency, duration, rampUp)
|
||||
<-loic.TestDone
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -25,5 +27,6 @@ func init() {
|
|||
startCmd.Flags().IntP("concurrency", "c", 10, "Maximale Anzahl paralleler Anfragen")
|
||||
startCmd.Flags().DurationP("duration", "d", time.Minute, "Dauer des Tests")
|
||||
startCmd.Flags().DurationP("ramp-up", "r", 30*time.Second, "Zeit für schrittweise Erhöhung der Last")
|
||||
startCmd.MarkFlagRequired("target")
|
||||
rootCmd.AddCommand(startCmd)
|
||||
}
|
||||
|
|
|
@ -2,18 +2,20 @@ package loic
|
|||
|
||||
import (
|
||||
"context"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
)
|
||||
|
||||
var meter = otel.Meter("loic")
|
||||
var requestsSent metric.Int64Counter
|
||||
var currentTest *Test
|
||||
var isRunning bool
|
||||
var TestDone = make(chan struct{})
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
|
@ -24,6 +26,8 @@ func init() {
|
|||
if err != nil {
|
||||
log.Fatalf("failed to create counter: %v", err)
|
||||
}
|
||||
isRunning = false
|
||||
log.Println("LOIC initialized")
|
||||
}
|
||||
|
||||
type Test struct {
|
||||
|
@ -37,6 +41,11 @@ type Test struct {
|
|||
}
|
||||
|
||||
func StartTest(targetURL string, concurrency int, duration time.Duration, rampUp time.Duration) {
|
||||
if isRunning {
|
||||
log.Println("Test already running, skipping start")
|
||||
return
|
||||
}
|
||||
log.Printf("Starting test with target: %s, concurrency: %d, duration: %v, rampUp: %v", targetURL, concurrency, duration, rampUp)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
currentTest := &Test{
|
||||
TargetURL: targetURL,
|
||||
|
@ -46,17 +55,24 @@ func StartTest(targetURL string, concurrency int, duration time.Duration, rampUp
|
|||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
}
|
||||
|
||||
go currentTest.run()
|
||||
isRunning = true
|
||||
log.Println("isRunning set to true, launching goroutine")
|
||||
go func() {
|
||||
currentTest.run()
|
||||
close(TestDone)
|
||||
}()
|
||||
}
|
||||
|
||||
func StopTest() {
|
||||
if currentTest != nil && isRunning {
|
||||
log.Println("Stopping test")
|
||||
currentTest.cancel()
|
||||
currentTest.wg.Wait()
|
||||
currentTest = nil
|
||||
isRunning = false
|
||||
log.Println("Test stopped")
|
||||
log.Println("Test stopped, isRunning set to false")
|
||||
} else {
|
||||
log.Println("No test running to stop")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,23 +81,41 @@ func IsTestRunning() bool {
|
|||
}
|
||||
|
||||
func (t *Test) run() {
|
||||
defer func() { isRunning = false }()
|
||||
ticker := time.NewTicker(t.RampUpTime / time.Duration(t.Concurrency))
|
||||
defer ticker.Stop()
|
||||
currentConcurrency := 1
|
||||
for i := 0; i < t.Concurrency; i++ {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
if currentConcurrency < t.Concurrency {
|
||||
currentConcurrency++
|
||||
t.startWorker()
|
||||
defer func() {
|
||||
isRunning = false
|
||||
log.Println("Test run completed, isRunning set to false")
|
||||
}()
|
||||
log.Println("Starting test run")
|
||||
|
||||
// Start first worker immediately
|
||||
if t.Concurrency > 0 {
|
||||
log.Println("Starting worker 1")
|
||||
t.startWorker()
|
||||
currentConcurrency := 1
|
||||
if t.Concurrency > 1 {
|
||||
for i := 1; i < t.Concurrency; i++ {
|
||||
select {
|
||||
case <-time.After(t.RampUpTime / time.Duration(t.Concurrency-1)):
|
||||
currentConcurrency++
|
||||
log.Printf("Starting worker %d", currentConcurrency)
|
||||
t.startWorker()
|
||||
case <-t.ctx.Done():
|
||||
log.Println("Context cancelled during ramp-up, exiting run")
|
||||
return
|
||||
}
|
||||
}
|
||||
case <-time.After(t.Duration):
|
||||
t.cancel()
|
||||
case <-t.ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for the duration or context cancellation
|
||||
select {
|
||||
case <-time.After(t.Duration):
|
||||
log.Println("Duration reached, cancelling test")
|
||||
t.cancel()
|
||||
case <-t.ctx.Done():
|
||||
log.Println("Context cancelled, exiting run")
|
||||
}
|
||||
log.Println("Waiting for all workers to finish")
|
||||
t.wg.Wait()
|
||||
}
|
||||
|
||||
|
@ -89,17 +123,20 @@ func (t *Test) startWorker() {
|
|||
t.wg.Add(1)
|
||||
go func() {
|
||||
defer t.wg.Done()
|
||||
log.Println("Worker started")
|
||||
for {
|
||||
select {
|
||||
case <-t.ctx.Done():
|
||||
log.Println("Worker stopped due to context cancellation")
|
||||
return
|
||||
default:
|
||||
resp, err := http.Get(t.TargetURL)
|
||||
if err != nil {
|
||||
log.Println("Error:", err)
|
||||
log.Println("Error in worker: %v", err)
|
||||
} else {
|
||||
resp.Body.Close()
|
||||
requestsSent.Add(t.ctx, 1)
|
||||
log.Println("Request sent, counter incremented")
|
||||
}
|
||||
time.Sleep(time.Second / time.Duration(t.Concurrency))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue