attack

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Copyright © 2025 Semen Adamenko <[email protected]>

Copyright © 2025 Semen Adamenko <[email protected]>

Copyright © 2025 Semen Adamenko <[email protected]>

Copyright © 2025 Semen Adamenko <[email protected]>

Copyright © 2025 Semen Adamenko <[email protected]>

Index

Constants

View Source
const (
	MaxLatencySamples        = 100000
	MaxLatencyHistorySamples = 1000 // For latency over time chart
)

Variables

This section is empty.

Functions

func CalculatePercentile

func CalculatePercentile(latencies []time.Duration, percentile int) time.Duration

CalculatePercentile calculates the percentile from latencies slice

Types

type AttackConfig

type AttackConfig struct {
	URL         string
	Method      string
	Body        string
	Headers     []string
	Output      string
	Duration    time.Duration
	Timeout     time.Duration
	Workers     uint
	Connections uint

	RPS int
}

AttackConfig holds the configuration for a load test

func (*AttackConfig) ToJSON

func (cfg *AttackConfig) ToJSON() AttackConfigJSON

ToJSON converts AttackConfig to AttackConfigJSON

type AttackConfigJSON

type AttackConfigJSON struct {
	URL         string   `json:"url"`
	Method      string   `json:"method"`
	Body        string   `json:"body,omitempty"`
	Headers     []string `json:"headers,omitempty"`
	Duration    string   `json:"duration"`
	Timeout     string   `json:"timeout"`
	Workers     uint     `json:"workers"`
	Connections uint     `json:"connections"`
	RPS         int      `json:"rps"`
}

AttackConfigJSON is a JSON-serializable version of AttackConfig that converts time.Duration fields to strings

type ErrorBreakdown

type ErrorBreakdown struct {
	Timeout           uint64 `json:"timeout"`
	ConnectionRefused uint64 `json:"connection_refused"`
	DNS               uint64 `json:"dns"`
	TLS               uint64 `json:"tls"`
	Other             uint64 `json:"other"`
}

ErrorBreakdown represents error type breakdown

type ErrorType

type ErrorType string

ErrorType represents the type of error that occurred

const (
	ErrorTypeTimeout           ErrorType = "timeout"
	ErrorTypeConnectionRefused ErrorType = "connection_refused"
	ErrorTypeDNS               ErrorType = "dns"
	ErrorTypeTLS               ErrorType = "tls"
	ErrorTypeOther             ErrorType = "other"
)

type GlobalMetrics

type GlobalMetrics struct {
	TotalRequests uint64
	Successes     uint64
	Failures      uint64
	TotalLatency  time.Duration

	// Min/Max latency tracking
	MinLatency time.Duration
	MaxLatency time.Duration

	// Bytes transferred
	BytesSent     uint64
	BytesReceived uint64

	// Error breakdown
	ErrorTypes map[ErrorType]uint64

	// Latency history for over-time chart (sampled)
	LatencyHistory []LatencyPoint

	// Target RPS for comparison
	TargetRPS int

	// Test timing
	StartTime time.Time
	Duration  time.Duration // Configured duration (0 = infinite)

	Latencies []time.Duration

	StatusCodes map[int]uint64

	sync.Mutex
	// contains filtered or unexported fields
}

GlobalMetrics holds aggregated metrics for a load test

func NewGlobalMetrics

func NewGlobalMetrics() *GlobalMetrics

NewGlobalMetrics creates a new GlobalMetrics instance

func RunAttack

func RunAttack(cfg *AttackConfig, stopCh chan struct{}) (*GlobalMetrics, time.Duration, error)

RunAttack executes the load test and returns metrics. stopCh: channel to signal test stop (close to stop) Returns: metrics, elapsed time, error

func RunAttackWithMetrics

func RunAttackWithMetrics(cfg *AttackConfig, stopCh chan struct{}, metrics *GlobalMetrics) (*GlobalMetrics, time.Duration, error)

RunAttackWithMetrics executes the load test with an optional pre-created metrics object. If metrics is nil, a new one is created. This allows callers to monitor metrics in real-time during the attack.

func (*GlobalMetrics) AddBytes

func (m *GlobalMetrics) AddBytes(sent, received uint64)

AddBytes adds bytes transferred

func (*GlobalMetrics) AddError

func (m *GlobalMetrics) AddError(errType ErrorType)

AddError records an error with its type

func (*GlobalMetrics) AddLatency

func (m *GlobalMetrics) AddLatency(latency time.Duration)

AddLatency adds a latency sample to the metrics

func (*GlobalMetrics) SetTestConfig

func (m *GlobalMetrics) SetTestConfig(targetRPS int, duration time.Duration, startTime time.Time)

SetTestConfig sets the test configuration for progress tracking

func (*GlobalMetrics) ToJSON

func (m *GlobalMetrics) ToJSON(cfg *AttackConfig, elapsedTime time.Duration, reqPerSec float64, calculatePercentile func([]time.Duration, int) time.Duration) ([]byte, error)

ToJSON converts GlobalMetrics to JSON

type LatencyPercentiles

type LatencyPercentiles struct {
	P50 string `json:"p50"`
	P90 string `json:"p90"`
	P95 string `json:"p95"`
	P99 string `json:"p99"`
}

LatencyPercentiles represents latency percentiles

type LatencyPoint

type LatencyPoint struct {
	Timestamp time.Time
	Latency   time.Duration
}

LatencyPoint represents a latency sample at a point in time

type MetricsReport

type MetricsReport struct {
	Config             AttackConfigJSON   `json:"config"`
	Summary            Summary            `json:"summary"`
	LatencyPercentiles LatencyPercentiles `json:"latency_percentiles"`
	StatusCodes        map[int]uint64     `json:"status_codes"`
	ErrorBreakdown     ErrorBreakdown     `json:"error_breakdown"`
	Timestamp          string             `json:"timestamp"`
}

MetricsReport represents the full metrics report

type RequestResult

type RequestResult struct {
	StatusCode    int
	Latency       time.Duration
	Success       bool
	ErrorType     ErrorType // Type of error if failed
	BytesSent     uint64    // Request body size
	BytesReceived uint64    // Response body size
}

RequestResult represents the result of a single HTTP request

type Summary

type Summary struct {
	TotalRequests      uint64  `json:"total_requests"`
	SuccessfulRequests uint64  `json:"successful_requests"`
	FailedRequests     uint64  `json:"failed_requests"`
	TotalElapsedTime   string  `json:"total_elapsed_time"`
	AverageLatency     string  `json:"average_latency"`
	MinLatency         string  `json:"min_latency"`
	MaxLatency         string  `json:"max_latency"`
	ThroughputRPS      float64 `json:"throughput_rps"`
	TargetRPS          int     `json:"target_rps"`
	BytesSent          uint64  `json:"bytes_sent"`
	BytesReceived      uint64  `json:"bytes_received"`
}

Summary represents the test summary

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL