Перенесите реализацию ЛР 4 на стенд ЛР 5
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type timeoutErr struct{}
|
||||
|
||||
func (timeoutErr) Error() string { return "i/o timeout" }
|
||||
func (timeoutErr) Timeout() bool { return true }
|
||||
func (timeoutErr) Temporary() bool { return true }
|
||||
|
||||
var _ net.Error = timeoutErr{}
|
||||
|
||||
func TestClassify(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
res result
|
||||
want string
|
||||
}{
|
||||
{result{status: 201, instance: "a"}, "ok"},
|
||||
{result{status: 200, instance: "a"}, "ok"},
|
||||
{result{status: 409, instance: "a"}, "http_409"},
|
||||
{result{status: 503, instance: "a"}, "http_503"},
|
||||
{result{status: 503}, "http_503_lb"},
|
||||
{result{status: 502}, "http_502_lb"},
|
||||
{result{err: context.DeadlineExceeded}, "client_timeout"},
|
||||
{result{err: fmt.Errorf("post: %w", timeoutErr{})}, "client_timeout"},
|
||||
{result{err: errors.New("connection refused")}, "conn_error"},
|
||||
} {
|
||||
if got := classify(c.res); got != c.want {
|
||||
t.Errorf("classify(%+v) = %q, want %q", c.res, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPercentileNearestRank(t *testing.T) {
|
||||
ms := func(v ...int) []time.Duration {
|
||||
out := make([]time.Duration, len(v))
|
||||
for i, x := range v {
|
||||
out[i] = time.Duration(x) * time.Millisecond
|
||||
}
|
||||
return out
|
||||
}
|
||||
sorted := ms(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
|
||||
for p, want := range map[float64]time.Duration{50: 5 * time.Millisecond, 95: 10 * time.Millisecond, 99: 10 * time.Millisecond, 10: time.Millisecond} {
|
||||
if got := percentile(sorted, p); got != want {
|
||||
t.Errorf("p%v = %v, want %v", p, got, want)
|
||||
}
|
||||
}
|
||||
if got := percentile(nil, 99); got != 0 {
|
||||
t.Errorf("empty p99 = %v, want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A step is invalid when the generator itself fell behind its schedule: the
|
||||
// system was then offered less than the nominal rate.
|
||||
func TestStepSummaryValidityAndErrorShare(t *testing.T) {
|
||||
samples := []sample{
|
||||
{class: "ok", latency: 10 * time.Millisecond, lag: time.Millisecond},
|
||||
{class: "ok", latency: 20 * time.Millisecond, lag: time.Millisecond},
|
||||
{class: "client_timeout", latency: 2 * time.Second, lag: time.Millisecond},
|
||||
{class: "http_503_lb", latency: time.Millisecond, lag: time.Millisecond},
|
||||
}
|
||||
s := summarize(4, time.Second, samples)
|
||||
if s.Sent != 4 || s.OK != 2 || s.OKRPS != 2 || s.ErrorShare != 0.5 || !s.Valid {
|
||||
t.Fatalf("summary = %+v", s)
|
||||
}
|
||||
if s.Classes["client_timeout"] != 1 || s.Classes["http_503_lb"] != 1 {
|
||||
t.Fatalf("classes = %v", s.Classes)
|
||||
}
|
||||
if s.P99Ms != 20 {
|
||||
t.Fatalf("p99 of successful requests = %v ms, want 20", s.P99Ms)
|
||||
}
|
||||
samples[0].lag = 50 * time.Millisecond
|
||||
samples[1].lag = 50 * time.Millisecond
|
||||
if s := summarize(4, time.Second, samples); s.Valid {
|
||||
t.Fatalf("lagging generator marked valid: %+v", s)
|
||||
}
|
||||
}
|
||||
|
||||
// Open loop: requests leave on schedule even when responses are slow. A
|
||||
// closed loop with one worker would send only a handful here.
|
||||
func TestOpenLoopDoesNotWaitForResponses(t *testing.T) {
|
||||
var fired atomic.Int32
|
||||
start := time.Now()
|
||||
openLoop(context.Background(), 50, time.Second, func(time.Time) func() {
|
||||
return func() {
|
||||
fired.Add(1)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
})
|
||||
if n := fired.Load(); n < 45 || n > 50 {
|
||||
t.Fatalf("fired %d requests in 1 s at 50/s, want about 50", n)
|
||||
}
|
||||
// About 1.2 s; the bound leaves room for -race on a shared CI runner and
|
||||
// still fails a closed loop, which would need 50 x 200 ms = 10 s.
|
||||
if took := time.Since(start); took > 2500*time.Millisecond {
|
||||
t.Fatalf("open loop took %v, want about 1.2 s", took)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStepPassesOnlyWithinTheObjective(t *testing.T) {
|
||||
ok := func(latency, lag time.Duration) sample {
|
||||
return sample{class: "ok", latency: latency, lag: lag}
|
||||
}
|
||||
fast := []sample{ok(10*time.Millisecond, time.Millisecond), ok(20*time.Millisecond, time.Millisecond)}
|
||||
if s := summarize(2, time.Second, fast); !s.Passed {
|
||||
t.Fatalf("fast clean step must pass: %+v", s)
|
||||
}
|
||||
slow := []sample{ok(10*time.Millisecond, time.Millisecond), ok(600*time.Millisecond, time.Millisecond)}
|
||||
if s := summarize(2, time.Second, slow); s.Passed {
|
||||
t.Fatalf("p99 above 500 ms must fail: %+v", s)
|
||||
}
|
||||
failing := append(fast, sample{class: "http_503_lb", lag: time.Millisecond})
|
||||
if s := summarize(3, time.Second, failing); s.Passed {
|
||||
t.Fatalf("errors above 1 %% must fail: %+v", s)
|
||||
}
|
||||
late := []sample{ok(10*time.Millisecond, 50*time.Millisecond), ok(10*time.Millisecond, 50*time.Millisecond)}
|
||||
if s := summarize(2, time.Second, late); s.Passed {
|
||||
t.Fatalf("an invalid step must fail: %+v", s)
|
||||
}
|
||||
}
|
||||
|
||||
// The ladder stops when the system leaves the objective, not when the
|
||||
// generator alone fell behind: a later step may still be valid and pass.
|
||||
func TestOnlyTheSystemEndsTheLadder(t *testing.T) {
|
||||
ok := func(latency, lag time.Duration) sample {
|
||||
return sample{class: "ok", latency: latency, lag: lag}
|
||||
}
|
||||
late := []sample{ok(10*time.Millisecond, 50*time.Millisecond), ok(10*time.Millisecond, 50*time.Millisecond)}
|
||||
if s := summarize(2, time.Second, late); s.overloaded() {
|
||||
t.Fatalf("a lagging generator must not end the ladder: %+v", s)
|
||||
}
|
||||
slow := []sample{ok(10*time.Millisecond, time.Millisecond), ok(600*time.Millisecond, time.Millisecond)}
|
||||
if s := summarize(2, time.Second, slow); !s.overloaded() {
|
||||
t.Fatalf("p99 above 500 ms must end the ladder: %+v", s)
|
||||
}
|
||||
failing := []sample{ok(10*time.Millisecond, time.Millisecond), {class: "client_timeout", lag: time.Millisecond}}
|
||||
if s := summarize(2, time.Second, failing); !s.overloaded() {
|
||||
t.Fatalf("errors above 1 %% must end the ladder: %+v", s)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user