@@ -0,0 +1,75 @@
1+package authorization
2+
3+import (
4+ "net"
5+ "net/http"
6+ "net/http/httptest"
7+ "testing"
8+
9+ "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
10+)
11+
12+var result AuthorizationPolicy
13+
14+func benchmarkRuleSetMatches(ruleCount int, b *testing.B) {
15+ rule1 := options.AuthorizationRule{
16+ Policy: options.AllowPolicy,
17+ Path: "/[a-z]*/[a-z]*/[a-z]*",
18+ Methods: []string{
19+ "get",
20+ "put",
21+ "post",
22+ "patch",
23+ "head",
24+ "options",
25+ },
26+ IPs: []string{
27+ "10.0.0.0/24",
28+ "10.1.0.0/24",
29+ "10.2.1.0/24",
30+ "10.2.2.0/24",
31+ "10.3.3.0/24",
32+ "192.168.1.0/24",
33+ "192.168.2.0/24",
34+ "192.168.3.0/24",
35+ "192.168.0.128/25",
36+ "192.168.0.64/26",
37+ "192.168.0.32/27",
38+ "192.168.0.16/28",
39+ },
40+ }
41+
42+ requestRules := options.RequestRules{}
43+ for i := 0; i <= ruleCount; i++ {
44+ requestRules = append(requestRules, rule1)
45+ }
46+
47+ ruleSet, err := NewRuleSet(requestRules, func(_ *http.Request) net.IP {
48+ return net.IPv4(192, 168, 0, 1)
49+ })
50+ if err != nil {
51+ b.Fatal(err)
52+ }
53+
54+ req := httptest.NewRequest("GET", "/foo/bar/baz", nil)
55+
56+ var r AuthorizationPolicy
57+ for n := 0; n < b.N; n++ {
58+ r = ruleSet.Matches(req)
59+ if r != NonePolicy {
60+ b.Fatal("expected policy not to match")
61+ }
62+ }
63+ // always store the result to a package level variable
64+ // so the compiler cannot eliminate the Benchmark itself.
65+ result = r
66+}
67+
68+func BenchmarkRuleSetMatches1(b *testing.B) { benchmarkRuleSetMatches(1, b) }
69+func BenchmarkRuleSetMatches10(b *testing.B) { benchmarkRuleSetMatches(10, b) }
70+func BenchmarkRuleSetMatches25(b *testing.B) { benchmarkRuleSetMatches(25, b) }
71+func BenchmarkRuleSetMatches50(b *testing.B) { benchmarkRuleSetMatches(50, b) }
72+func BenchmarkRuleSetMatches100(b *testing.B) { benchmarkRuleSetMatches(100, b) }
73+func BenchmarkRuleSetMatches250(b *testing.B) { benchmarkRuleSetMatches(250, b) }
74+func BenchmarkRuleSetMatches500(b *testing.B) { benchmarkRuleSetMatches(500, b) }
75+func BenchmarkRuleSetMatches1000(b *testing.B) { benchmarkRuleSetMatches(1000, b) }