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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Daniel.Sy/loic-go/pkg/loic"
|
"forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Daniel.Sy/loic-go/pkg/loic"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var startCmd = &cobra.Command{
|
var startCmd = &cobra.Command{
|
||||||
Use: "start",
|
Use: "start",
|
||||||
Short: "Startet einen Lasttest",
|
Short: "Start an attack",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
targetURL, _ := cmd.Flags().GetString("target")
|
targetURL, _ := cmd.Flags().GetString("target")
|
||||||
concurrency, _ := cmd.Flags().GetInt("concurrency")
|
concurrency, _ := cmd.Flags().GetInt("concurrency")
|
||||||
|
@ -17,6 +18,7 @@ var startCmd = &cobra.Command{
|
||||||
rampUp, _ := cmd.Flags().GetDuration("ramp-up")
|
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)
|
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.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().IntP("concurrency", "c", 10, "Maximale Anzahl paralleler Anfragen")
|
||||||
startCmd.Flags().DurationP("duration", "d", time.Minute, "Dauer des Tests")
|
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.Flags().DurationP("ramp-up", "r", 30*time.Second, "Zeit für schrittweise Erhöhung der Last")
|
||||||
|
startCmd.MarkFlagRequired("target")
|
||||||
rootCmd.AddCommand(startCmd)
|
rootCmd.AddCommand(startCmd)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,18 +2,20 @@ package loic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"go.opentelemetry.io/otel"
|
|
||||||
"go.opentelemetry.io/otel/metric"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.opentelemetry.io/otel"
|
||||||
|
"go.opentelemetry.io/otel/metric"
|
||||||
)
|
)
|
||||||
|
|
||||||
var meter = otel.Meter("loic")
|
var meter = otel.Meter("loic")
|
||||||
var requestsSent metric.Int64Counter
|
var requestsSent metric.Int64Counter
|
||||||
var currentTest *Test
|
var currentTest *Test
|
||||||
var isRunning bool
|
var isRunning bool
|
||||||
|
var TestDone = make(chan struct{})
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var err error
|
var err error
|
||||||
|
@ -24,6 +26,8 @@ func init() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to create counter: %v", err)
|
log.Fatalf("failed to create counter: %v", err)
|
||||||
}
|
}
|
||||||
|
isRunning = false
|
||||||
|
log.Println("LOIC initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
type Test struct {
|
type Test struct {
|
||||||
|
@ -37,6 +41,11 @@ type Test struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartTest(targetURL string, concurrency int, duration time.Duration, rampUp time.Duration) {
|
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())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
currentTest := &Test{
|
currentTest := &Test{
|
||||||
TargetURL: targetURL,
|
TargetURL: targetURL,
|
||||||
|
@ -46,17 +55,24 @@ func StartTest(targetURL string, concurrency int, duration time.Duration, rampUp
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
}
|
}
|
||||||
|
isRunning = true
|
||||||
go currentTest.run()
|
log.Println("isRunning set to true, launching goroutine")
|
||||||
|
go func() {
|
||||||
|
currentTest.run()
|
||||||
|
close(TestDone)
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func StopTest() {
|
func StopTest() {
|
||||||
if currentTest != nil && isRunning {
|
if currentTest != nil && isRunning {
|
||||||
|
log.Println("Stopping test")
|
||||||
currentTest.cancel()
|
currentTest.cancel()
|
||||||
currentTest.wg.Wait()
|
currentTest.wg.Wait()
|
||||||
currentTest = nil
|
currentTest = nil
|
||||||
isRunning = false
|
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() {
|
func (t *Test) run() {
|
||||||
defer func() { isRunning = false }()
|
defer func() {
|
||||||
ticker := time.NewTicker(t.RampUpTime / time.Duration(t.Concurrency))
|
isRunning = false
|
||||||
defer ticker.Stop()
|
log.Println("Test run completed, isRunning set to false")
|
||||||
currentConcurrency := 1
|
}()
|
||||||
for i := 0; i < t.Concurrency; i++ {
|
log.Println("Starting test run")
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
// Start first worker immediately
|
||||||
if currentConcurrency < t.Concurrency {
|
if t.Concurrency > 0 {
|
||||||
currentConcurrency++
|
log.Println("Starting worker 1")
|
||||||
t.startWorker()
|
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()
|
t.wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,17 +123,20 @@ func (t *Test) startWorker() {
|
||||||
t.wg.Add(1)
|
t.wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer t.wg.Done()
|
defer t.wg.Done()
|
||||||
|
log.Println("Worker started")
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-t.ctx.Done():
|
case <-t.ctx.Done():
|
||||||
|
log.Println("Worker stopped due to context cancellation")
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
resp, err := http.Get(t.TargetURL)
|
resp, err := http.Get(t.TargetURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error:", err)
|
log.Println("Error in worker: %v", err)
|
||||||
} else {
|
} else {
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
requestsSent.Add(t.ctx, 1)
|
requestsSent.Add(t.ctx, 1)
|
||||||
|
log.Println("Request sent, counter incremented")
|
||||||
}
|
}
|
||||||
time.Sleep(time.Second / time.Duration(t.Concurrency))
|
time.Sleep(time.Second / time.Duration(t.Concurrency))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue