Skip to content

Commit 9905f2b

Browse files
committed
Add benchmarks to test performance
1 parent bd708da commit 9905f2b

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

path_to_regexp_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3180,6 +3180,88 @@ func TestPathToRegexp(t *testing.T) {
31803180
})
31813181
}
31823182

3183+
func BenchmarkPathToRegexp(b *testing.B) {
3184+
b.Run("string", func(b *testing.B) {
3185+
b.Run("no parameters", func(b *testing.B) {
3186+
for i := 0; i < b.N; i++ {
3187+
PathToRegexp("/foo", &[]Token{}, nil)
3188+
}
3189+
})
3190+
b.Run("named parameters", func(b *testing.B) {
3191+
for i := 0; i < b.N; i++ {
3192+
PathToRegexp("/foo/:bar", &[]Token{}, nil)
3193+
}
3194+
})
3195+
b.Run("unnamed parameters", func(b *testing.B) {
3196+
for i := 0; i < b.N; i++ {
3197+
PathToRegexp("/:foo/(.*)", &[]Token{}, nil)
3198+
}
3199+
})
3200+
b.Run("optional parameters", func(b *testing.B) {
3201+
for i := 0; i < b.N; i++ {
3202+
PathToRegexp("/:foo/:bar?", &[]Token{}, nil)
3203+
}
3204+
})
3205+
})
3206+
3207+
b.Run("regexp", func(b *testing.B) {
3208+
b.Run("simple", func(b *testing.B) {
3209+
for i := 0; i < b.N; i++ {
3210+
PathToRegexp(regexp2.MustCompile("^/foo/\\d+", regexp2.None), &[]Token{}, nil)
3211+
}
3212+
})
3213+
b.Run("complex", func(b *testing.B) {
3214+
for i := 0; i < b.N; i++ {
3215+
PathToRegexp(regexp2.MustCompile("^/foo/\\d+(?:\\.\\d+)?", regexp2.None), &[]Token{}, nil)
3216+
}
3217+
})
3218+
})
3219+
3220+
b.Run("array", func(b *testing.B) {
3221+
b.Run("simple", func(b *testing.B) {
3222+
for i := 0; i < b.N; i++ {
3223+
PathToRegexp([]string{"/foo", "/foo/bar"}, &[]Token{}, nil)
3224+
}
3225+
})
3226+
b.Run("complex", func(b *testing.B) {
3227+
for i := 0; i < b.N; i++ {
3228+
PathToRegexp([]string{"/foo/:bar", "/:foo/:bar?"}, &[]Token{}, nil)
3229+
}
3230+
})
3231+
})
3232+
3233+
b.Run("with end false", func(b *testing.B) {
3234+
for i := 0; i < b.N; i++ {
3235+
PathToRegexp("/foo/:bar", &[]Token{}, &Options{End: &falseValue})
3236+
}
3237+
})
3238+
}
3239+
3240+
func BenchmarkParse(b *testing.B) {
3241+
for i := 0; i < b.N; i++ {
3242+
Parse("/foo/:bar/(.*)", nil)
3243+
}
3244+
}
3245+
3246+
func BenchmarkCompile(b *testing.B) {
3247+
b.Run("simple", func(b *testing.B) {
3248+
for i := 0; i < b.N; i++ {
3249+
Compile("/foo/:bar", nil)
3250+
}
3251+
})
3252+
b.Run("complex", func(b *testing.B) {
3253+
for i := 0; i < b.N; i++ {
3254+
Compile("/foo/:bar(\\d+)", nil)
3255+
}
3256+
})
3257+
}
3258+
3259+
func BenchmarkMatch(b *testing.B) {
3260+
for i := 0; i < b.N; i++ {
3261+
Match("/foo/:bar", nil)
3262+
}
3263+
}
3264+
31833265
func TestMustCompile(t *testing.T) {
31843266
r := MustCompile("/user/:id(\\d+)", nil)
31853267
if r == nil {

0 commit comments

Comments
 (0)