|
| 1 | +package urlpattern_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/dunglas/go-urlpattern" |
| 7 | +) |
| 8 | + |
| 9 | +var benchmarkPatterns = []struct { |
| 10 | + name, pattern, baseURL string |
| 11 | +}{ |
| 12 | + {"simple", "https://example.com/foo/bar", ""}, |
| 13 | + {"wildcard", "https://*.example.com/*", ""}, |
| 14 | + {"named", "https://example.com/users/:id/posts/:postId", ""}, |
| 15 | + {"regex", "https://example.com/items/(\\d+)", ""}, |
| 16 | + {"full", "https://user:pw@example.com:8080/path/:id?q=:v#:frag", ""}, |
| 17 | +} |
| 18 | + |
| 19 | +var benchmarkMatches = []struct { |
| 20 | + name, pattern, input string |
| 21 | +}{ |
| 22 | + {"simple", "https://example.com/foo/bar", "https://example.com/foo/bar"}, |
| 23 | + {"wildcard", "https://*.example.com/*", "https://api.example.com/users/42"}, |
| 24 | + {"named", "https://example.com/users/:id/posts/:postId", "https://example.com/users/42/posts/7"}, |
| 25 | + {"regex", "https://example.com/items/(\\d+)", "https://example.com/items/12345"}, |
| 26 | + {"miss", "https://example.com/foo", "https://example.com/bar"}, |
| 27 | +} |
| 28 | + |
| 29 | +func BenchmarkNew(b *testing.B) { |
| 30 | + for _, bc := range benchmarkPatterns { |
| 31 | + b.Run(bc.name, func(b *testing.B) { |
| 32 | + b.ReportAllocs() |
| 33 | + for i := 0; i < b.N; i++ { |
| 34 | + if _, err := urlpattern.New(bc.pattern, bc.baseURL, nil); err != nil { |
| 35 | + b.Fatal(err) |
| 36 | + } |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func BenchmarkTest(b *testing.B) { |
| 43 | + for _, bc := range benchmarkMatches { |
| 44 | + p, err := urlpattern.New(bc.pattern, "", nil) |
| 45 | + if err != nil { |
| 46 | + b.Fatal(err) |
| 47 | + } |
| 48 | + b.Run(bc.name, func(b *testing.B) { |
| 49 | + b.ReportAllocs() |
| 50 | + for i := 0; i < b.N; i++ { |
| 51 | + p.Test(bc.input, "") |
| 52 | + } |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func BenchmarkExec(b *testing.B) { |
| 58 | + for _, bc := range benchmarkMatches { |
| 59 | + p, err := urlpattern.New(bc.pattern, "", nil) |
| 60 | + if err != nil { |
| 61 | + b.Fatal(err) |
| 62 | + } |
| 63 | + b.Run(bc.name, func(b *testing.B) { |
| 64 | + b.ReportAllocs() |
| 65 | + for i := 0; i < b.N; i++ { |
| 66 | + p.Exec(bc.input, "") |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
0 commit comments