From 37254a139a786b2bf099a39158cc53e9261bd1f4 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 11 May 2020 22:21:08 +0300 Subject: [PATCH] lib/storage: add a benchmark for Graphite-like regexps for metric names --- lib/storage/tag_filters_timing_test.go | 202 +++++++++++++++++++++++-- 1 file changed, 193 insertions(+), 9 deletions(-) diff --git a/lib/storage/tag_filters_timing_test.go b/lib/storage/tag_filters_timing_test.go index 52f776b89..df0956f57 100644 --- a/lib/storage/tag_filters_timing_test.go +++ b/lib/storage/tag_filters_timing_test.go @@ -7,7 +7,7 @@ import ( ) func BenchmarkTagFilterMatchSuffix(b *testing.B) { - b.Run("regexp-any-suffix", func(b *testing.B) { + b.Run("regexp-any-suffix-match", func(b *testing.B) { key := []byte("foo.*") isNegative := false isRegexp := true @@ -30,7 +30,7 @@ func BenchmarkTagFilterMatchSuffix(b *testing.B) { } }) }) - b.Run("regexp-any-nonzero-suffix", func(b *testing.B) { + b.Run("regexp-any-nonzero-suffix-match", func(b *testing.B) { key := []byte("foo.+") isNegative := false isRegexp := true @@ -53,11 +53,34 @@ func BenchmarkTagFilterMatchSuffix(b *testing.B) { } }) }) - b.Run("regexp-special-suffix", func(b *testing.B) { - key := []byte("foo.*ss?") + b.Run("regexp-any-nonzero-suffix-mismatch", func(b *testing.B) { + key := []byte("foo.+") isNegative := false isRegexp := true - suffix := marshalTagValue(nil, []byte("ojksdfds")) + suffix := marshalTagValue(nil, []byte("")) + b.ReportAllocs() + b.SetBytes(int64(1)) + b.RunParallel(func(pb *testing.PB) { + var tf tagFilter + if err := tf.Init(nil, nil, key, isNegative, isRegexp); err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + for pb.Next() { + ok, err := tf.matchSuffix(suffix) + if err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + if ok { + logger.Panicf("BUG: unexpected suffix match") + } + } + }) + }) + b.Run("regexp-special-suffix-match", func(b *testing.B) { + key := []byte("foo.*sss?") + isNegative := false + isRegexp := true + suffix := marshalTagValue(nil, []byte("ojksdfdss")) b.ReportAllocs() b.SetBytes(int64(1)) b.RunParallel(func(pb *testing.PB) { @@ -76,7 +99,53 @@ func BenchmarkTagFilterMatchSuffix(b *testing.B) { } }) }) - b.Run("regexp-or-values", func(b *testing.B) { + b.Run("regexp-special-suffix-mismatch", func(b *testing.B) { + key := []byte("foo.*sss?") + isNegative := false + isRegexp := true + suffix := marshalTagValue(nil, []byte("ojksdfds")) + b.ReportAllocs() + b.SetBytes(int64(1)) + b.RunParallel(func(pb *testing.PB) { + var tf tagFilter + if err := tf.Init(nil, nil, key, isNegative, isRegexp); err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + for pb.Next() { + ok, err := tf.matchSuffix(suffix) + if err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + if ok { + logger.Panicf("BUG: unexpected suffix match") + } + } + }) + }) + b.Run("regexp-or-values-match", func(b *testing.B) { + key := []byte("foo|bar|baz") + isNegative := false + isRegexp := true + suffix := marshalTagValue(nil, []byte("bar")) + b.ReportAllocs() + b.SetBytes(int64(1)) + b.RunParallel(func(pb *testing.PB) { + var tf tagFilter + if err := tf.Init(nil, nil, key, isNegative, isRegexp); err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + for pb.Next() { + ok, err := tf.matchSuffix(suffix) + if err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + if !ok { + logger.Panicf("BUG: unexpected mismatch") + } + } + }) + }) + b.Run("regexp-or-values-mismatch", func(b *testing.B) { key := []byte("foo|bar|baz") isNegative := false isRegexp := true @@ -94,12 +163,35 @@ func BenchmarkTagFilterMatchSuffix(b *testing.B) { logger.Panicf("BUG: unexpected error: %s", err) } if ok { - logger.Panicf("BUG: unexpected suffix match") + logger.Panicf("BUG: unexpected match") } } }) }) - b.Run("regexp-contains", func(b *testing.B) { + b.Run("regexp-contains-dot-star-match", func(b *testing.B) { + key := []byte(".*foo.*") + isNegative := false + isRegexp := true + suffix := marshalTagValue(nil, []byte("ojksfoodfds")) + b.ReportAllocs() + b.SetBytes(int64(1)) + b.RunParallel(func(pb *testing.PB) { + var tf tagFilter + if err := tf.Init(nil, nil, key, isNegative, isRegexp); err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + for pb.Next() { + ok, err := tf.matchSuffix(suffix) + if err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + if !ok { + logger.Panicf("BUG: unexpected mismatch") + } + } + }) + }) + b.Run("regexp-contains-dot-star-mismatch", func(b *testing.B) { key := []byte(".*foo.*") isNegative := false isRegexp := true @@ -117,7 +209,99 @@ func BenchmarkTagFilterMatchSuffix(b *testing.B) { logger.Panicf("BUG: unexpected error: %s", err) } if ok { - logger.Panicf("BUG: unexpected suffix match") + logger.Panicf("BUG: unexpected match") + } + } + }) + }) + b.Run("regexp-contains-dot-plus-match", func(b *testing.B) { + key := []byte(".+foo.+") + isNegative := false + isRegexp := true + suffix := marshalTagValue(nil, []byte("ojksdfoofds")) + b.ReportAllocs() + b.SetBytes(int64(1)) + b.RunParallel(func(pb *testing.PB) { + var tf tagFilter + if err := tf.Init(nil, nil, key, isNegative, isRegexp); err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + for pb.Next() { + ok, err := tf.matchSuffix(suffix) + if err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + if !ok { + logger.Panicf("BUG: unexpected mismatch") + } + } + }) + }) + b.Run("regexp-contains-dot-plus-mismatch", func(b *testing.B) { + key := []byte(".+foo.+") + isNegative := false + isRegexp := true + suffix := marshalTagValue(nil, []byte("ojksdfds")) + b.ReportAllocs() + b.SetBytes(int64(1)) + b.RunParallel(func(pb *testing.PB) { + var tf tagFilter + if err := tf.Init(nil, nil, key, isNegative, isRegexp); err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + for pb.Next() { + ok, err := tf.matchSuffix(suffix) + if err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + if ok { + logger.Panicf("BUG: unexpected match") + } + } + }) + }) + b.Run("regexp-graphite-metric-mismatch", func(b *testing.B) { + key := []byte(`foo[^.]*?\.bar\.baz\.[^.]*?\.ddd`) + isNegative := false + isRegexp := true + suffix := marshalTagValue(nil, []byte("foo1.xar.baz.sss.ddd")) + b.ReportAllocs() + b.SetBytes(int64(1)) + b.RunParallel(func(pb *testing.PB) { + var tf tagFilter + if err := tf.Init(nil, nil, key, isNegative, isRegexp); err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + for pb.Next() { + ok, err := tf.matchSuffix(suffix) + if err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + if ok { + logger.Panicf("BUG: unexpected match") + } + } + }) + }) + b.Run("regexp-graphite-metric-match", func(b *testing.B) { + key := []byte(`foo[^.]*?\.bar\.baz\.[^.]*?\.ddd`) + isNegative := false + isRegexp := true + suffix := marshalTagValue(nil, []byte("foo1.bar.baz.sss.ddd")) + b.ReportAllocs() + b.SetBytes(int64(1)) + b.RunParallel(func(pb *testing.PB) { + var tf tagFilter + if err := tf.Init(nil, nil, key, isNegative, isRegexp); err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + for pb.Next() { + ok, err := tf.matchSuffix(suffix) + if err != nil { + logger.Panicf("BUG: unexpected error: %s", err) + } + if !ok { + logger.Panicf("BUG: unexpected mismatch") } } })