-
Notifications
You must be signed in to change notification settings - Fork 0
/
adaptive_test.go
86 lines (75 loc) · 2.09 KB
/
adaptive_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package bulwark
import (
"context"
"errors"
"fmt"
"math/rand"
"strings"
"sync"
"testing"
"text/tabwriter"
"time"
"github.com/bradenaw/backpressure"
"github.com/deixis/faults"
"golang.org/x/time/rate"
)
func TestAdaptiveThrottleBasic(t *testing.T) {
duration := 28 * time.Second
start := time.Now()
demandRates := []int{5, 10, 20}
supplyRate := 20.0
acceptedErrorPct := 0.1
serverLimiter := backpressure.NewRateLimiter(len(demandRates), supplyRate, supplyRate)
clientThrottle := NewAdaptiveThrottle(len(demandRates), WithAdaptiveThrottleWindow(3*time.Second))
var wg sync.WaitGroup
requestsByPriority := make([]int, len(demandRates))
sentByPriority := make([]int, len(demandRates))
for i, r := range demandRates {
i := i
p := Priority(i)
l := rate.NewLimiter(rate.Limit(r), r)
wg.Add(1)
go func() {
defer wg.Done()
for time.Since(start) < duration {
l.Wait(context.Background())
requestsByPriority[i]++
_, _ = WithAdaptiveThrottle(clientThrottle, p, func() (struct{}, error) {
sentByPriority[i]++
err := serverLimiter.Wait(context.Background(), backpressure.Priority(p), 1)
if err != nil {
return struct{}{}, err
}
if rand.Float64() < acceptedErrorPct {
return struct{}{}, faults.WithNotFound(errors.New("not really an error"))
}
return struct{}{}, nil
})
}
}()
}
wg.Wait()
realDuration := time.Since(start)
totalSent := 0
for _, sent := range sentByPriority {
totalSent += sent
}
sendRate := float64(totalSent) / realDuration.Seconds()
t.Logf("total supply: %.2f", supplyRate)
t.Logf("aggregate sent rate: %.2f", sendRate)
var sb strings.Builder
tw := tabwriter.NewWriter(&sb, 0, 4, 2, ' ', 0)
fmt.Fprint(tw, "priority\trequest rate\tsend rate\treject %\n")
for i := range demandRates {
fmt.Fprintf(
tw,
"%d\t%.2f/sec\t%.2f/sec\t%.2f%%\n",
i,
float64(requestsByPriority[i])/realDuration.Seconds(),
float64(sentByPriority[i])/realDuration.Seconds(),
float64(requestsByPriority[i]-sentByPriority[i])/float64(requestsByPriority[i])*100,
)
}
tw.Flush()
t.Log("\n" + sb.String())
}