-
Notifications
You must be signed in to change notification settings - Fork 207
/
tollbooth_benchmark_test.go
52 lines (43 loc) · 1.29 KB
/
tollbooth_benchmark_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
package tollbooth
import (
"fmt"
"net/http"
"strings"
"testing"
"time"
"github.com/didip/tollbooth/v8/limiter"
)
func BenchmarkLimitByKeys(b *testing.B) {
lmt := limiter.New(nil).SetMax(1) // Only 1 request per second is allowed.
for i := 0; i < b.N; i++ {
LimitByKeys(lmt, []string{"127.0.0.1", "/"})
}
}
func BenchmarkLimitByKeysWithExpiringBuckets(b *testing.B) {
lmt := limiter.New(
&limiter.ExpirableOptions{DefaultExpirationTTL: time.Minute},
).SetMax(1) // Only 1 request per second is allowed.
for i := 0; i < b.N; i++ {
LimitByKeys(lmt, []string{"127.0.0.1", "/"})
}
}
func BenchmarkBuildKeys(b *testing.B) {
lmt := limiter.New(nil).SetMax(1) // Only 1 request per second is allowed.
lmt.SetIPLookup(limiter.IPLookup{
Name: "X-Real-IP",
IndexFromRight: 0,
}).
SetHeaders(make(map[string][]string)).
SetHeader("X-Real-IP", []string{"2601:7:1c82:4097:59a0:a80b:2841:b8c8"})
request, err := http.NewRequest("GET", "/", strings.NewReader("Hello, world!"))
if err != nil {
fmt.Printf("Unable to create new HTTP request. Error: %v", err)
}
request.Header.Set("X-Real-IP", lmt.GetHeader("X-Real-IP")[0])
for i := 0; i < b.N; i++ {
sliceKeys := BuildKeys(lmt, request)
if len(sliceKeys) == 0 {
fmt.Print("Length of sliceKeys should never be empty.")
}
}
}