Skip to content

Commit

Permalink
goimports -w . and fix lttb tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tomás Senart <tsenart@gmail.com>
  • Loading branch information
tsenart committed Jul 17, 2023
1 parent 130dfd3 commit a1eaf0f
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 52 deletions.
1 change: 1 addition & 0 deletions attack_nonwindows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package main
Expand Down
1 change: 1 addition & 0 deletions lib/attack_fuzz.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build gofuzz
// +build gofuzz

package vegeta
Expand Down
4 changes: 2 additions & 2 deletions lib/lttb/lttb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestDownsample(t *testing.T) {
}

// Test LTTB algorithm's equivalence to dgrisky/go-lttb
in := *(*[]golttb.Point)(unsafe.Pointer(&ps)) // #skipcq: GSC-G103
in := *(*[]golttb.Point[float64])(unsafe.Pointer(&ps)) // #skipcq: GSC-G103
out := golttb.LTTB(in, threshold)
theirs := *(*[]Point)(unsafe.Pointer(&out)) // #skipcq: GSC-G103

Expand All @@ -82,7 +82,7 @@ func TestDownsample(t *testing.T) {
}

func BenchmarkLTTB(b *testing.B) {
data := *(*[]golttb.Point)(unsafe.Pointer(&points[0])) // #skipcq: GSC-G103
data := *(*[]golttb.Point[float64])(unsafe.Pointer(&points[0])) // #skipcq: GSC-G103
b.Run("dgryski", func(b *testing.B) {
for i := 0; i < b.N; i++ {
golttb.LTTB(data, 1000)
Expand Down
80 changes: 46 additions & 34 deletions lib/pacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type Rate = ConstantPacer
var _ Pacer = ConstantPacer{}

// String returns a pretty-printed description of the ConstantPacer's behaviour:
// ConstantPacer{Freq: 1, Per: time.Second} => Constant{1 hits/1s}
//
// ConstantPacer{Freq: 1, Per: time.Second} => Constant{1 hits/1s}
func (cp ConstantPacer) String() string {
return fmt.Sprintf("Constant{%d hits/%s}", cp.Freq, cp.Per)
}
Expand Down Expand Up @@ -99,37 +100,43 @@ const (
)

// SinePacer is a Pacer that describes attack request rates with the equation:
// R = MA sin(O+(2𝛑/P)t)
//
// R = MA sin(O+(2𝛑/P)t)
//
// Where:
// R = Instantaneous attack rate at elapsed time t, hits per nanosecond
// M = Mean attack rate over period P, sp.Mean, hits per nanosecond
// A = Amplitude of sine wave, sp.Amp, hits per nanosecond
// O = Offset of sine wave, sp.StartAt, radians
// P = Period of sine wave, sp.Period, nanoseconds
// t = Elapsed time since attack start, nanoseconds
//
// R = Instantaneous attack rate at elapsed time t, hits per nanosecond
// M = Mean attack rate over period P, sp.Mean, hits per nanosecond
// A = Amplitude of sine wave, sp.Amp, hits per nanosecond
// O = Offset of sine wave, sp.StartAt, radians
// P = Period of sine wave, sp.Period, nanoseconds
// t = Elapsed time since attack start, nanoseconds
//
// Many thanks to http://ascii.co.uk/art/sine and "sps" for the ascii here :-)
//
// Mean -| ,-'''-.
// +Amp | ,-' | `-.
// | ,' | `. O=𝛑
// | ,' O=𝛑/2 `. MeanDown
// | / Peak \ /
// |/ \ /
// Mean -+-------------------------\--------------------------> t
// |\ \ /
// | \ \ O=3𝛑/2 /
// | O=0 `. Trough ,'
// | MeanUp `. | ,'
// Mean | `-. | ,-'
// -Amp -| `-,,,-'
// |<-------------------- Period --------------------->|
// Mean -| ,-'''-.
// +Amp | ,-' | `-.
// | ,' | `. O=𝛑
// | ,' O=𝛑/2 `. MeanDown
// | / Peak \ /
// |/ \ /
// Mean -+-------------------------\--------------------------> t
// |\ \ /
// | \ \ O=3𝛑/2 /
// | O=0 `. Trough ,'
// | MeanUp `. | ,'
// Mean | `-. | ,-'
// -Amp -| `-,,,-'
// |<-------------------- Period --------------------->|
//
// This equation is integrated with respect to time to derive the expected
// number of hits served at time t after the attack began:
// H = Mt - (AP/2𝛑)cos(O+(2𝛑/P)t) + (AP/2𝛑)cos(O)
//
// H = Mt - (AP/2𝛑)cos(O+(2𝛑/P)t) + (AP/2𝛑)cos(O)
//
// Where:
// H = Total number of hits triggered during t
//
// H = Total number of hits triggered during t
type SinePacer struct {
// The period of the sine wave, e.g. 20*time.Minute
// MUST BE > 0
Expand All @@ -148,13 +155,14 @@ type SinePacer struct {
var _ Pacer = SinePacer{}

// String returns a pretty-printed description of the SinePacer's behaviour:
// SinePacer{
// Period: time.Hour,
// Mean: Rate{100, time.Second},
// Amp: Rate{50, time.Second},
// StartAt: MeanDown,
// } =>
// Sine{Constant{100 hits/1s} ± Constant{50 hits/1s} / 1h, offset 1𝛑}
//
// SinePacer{
// Period: time.Hour,
// Mean: Rate{100, time.Second},
// Amp: Rate{50, time.Second},
// StartAt: MeanDown,
// } =>
// Sine{Constant{100 hits/1s} ± Constant{50 hits/1s} / 1h, offset 1𝛑}
func (sp SinePacer) String() string {
return fmt.Sprintf("Sine{%s ± %s / %s, offset %g𝛑}", sp.Mean, sp.Amp, sp.Period, sp.StartAt/math.Pi)
}
Expand Down Expand Up @@ -218,17 +226,21 @@ func (sp SinePacer) radians(t time.Duration) float64 {

// hitsPerNs calculates the instantaneous rate of attack at
// t nanoseconds after the attack began.
// R = MA sin(O+(2𝛑/P)t)
//
// R = MA sin(O+(2𝛑/P)t)
func (sp SinePacer) hitsPerNs(t time.Duration) float64 {
return sp.Mean.hitsPerNs() + sp.Amp.hitsPerNs()*math.Sin(sp.radians(t))
}

// hits returns the number of hits that have been sent during an attack
// lasting t nanoseconds. It returns a float so we can tell exactly how
// much we've missed our target by when solving numerically in Pace.
// H = Mt - (AP/2𝛑)cos(O+(2𝛑/P)t) + (AP/2𝛑)cos(O)
//
// H = Mt - (AP/2𝛑)cos(O+(2𝛑/P)t) + (AP/2𝛑)cos(O)
//
// This re-arranges to:
// H = Mt + (AP/2𝛑)(cos(O) - cos(O+(2𝛑/P)t))
//
// H = Mt + (AP/2𝛑)(cos(O) - cos(O+(2𝛑/P)t))
func (sp SinePacer) hits(t time.Duration) float64 {
if t <= 0 || sp.invalid() {
return 0
Expand Down
29 changes: 15 additions & 14 deletions lib/pacer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,25 @@ var quarterPeriods = map[string]float64{
// These are multipliers that help us integrate our rate equation
// in steps of 𝛑/2 without needing to resort to trig functions.
// This relies on integral in each quarter period being:
// (Mean * Period) / 4 ± (Amp * Period) / 2𝛑
//
// (Mean * Period) / 4 ± (Amp * Period) / 2𝛑
//
// Put another way, the two shaded areas in the graph below contain
// an equal number of hits -- (Amp * Period) / 2𝛑, or ampHits().
//
// Mean -| ,-'''-.
// +Amp | ,-'xxx| `-.
// | ,'xxxxxx| `.
// | ,'xxxxxxxx| `.
// | /xxxxxxxxxx| \
// |/xxxxxxxxxxx| \
// Mean -+-------------------------\--------------------------> t
// | \ |xxxxxxxxxxx/
// | \ |xxxxxxxxxx/
// | `. |xxxxxxxx,'
// | `. |xxxxxx,'
// Mean | `-. |xxx,-'
// -Amp -| `-,,,-'
// Mean -| ,-'''-.
// +Amp | ,-'xxx| `-.
// | ,'xxxxxx| `.
// | ,'xxxxxxxx| `.
// | /xxxxxxxxxx| \
// |/xxxxxxxxxxx| \
// Mean -+-------------------------\--------------------------> t
// | \ |xxxxxxxxxxx/
// | \ |xxxxxxxxxx/
// | `. |xxxxxxxx,'
// | `. |xxxxxx,'
// Mean | `-. |xxx,-'
// -Amp -| `-,,,-'
//
// The four multipliers are how many multiples of ampHits() away from
// Mean*t the integral is after 1, 2, 3 and 4 quarter-periods respectively.
Expand Down
1 change: 1 addition & 0 deletions lib/plot/assets.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build dev
// +build dev

package plot
Expand Down
1 change: 1 addition & 0 deletions lib/plot/assets_gen.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
5 changes: 3 additions & 2 deletions lib/results_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/results_fuzz.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build gofuzz
// +build gofuzz

package vegeta
Expand Down
1 change: 1 addition & 0 deletions lib/targets_fuzz.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build gofuzz
// +build gofuzz

package vegeta
Expand Down
1 change: 1 addition & 0 deletions lib/util_fuzz.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build gofuzz
// +build gofuzz

package vegeta
Expand Down
1 change: 1 addition & 0 deletions report_nonwindows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package main
Expand Down
1 change: 1 addition & 0 deletions report_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package main
Expand Down

0 comments on commit a1eaf0f

Please sign in to comment.