Skip to content

Commit

Permalink
Opmizing Group Regex
Browse files Browse the repository at this point in the history
  • Loading branch information
alanprot committed May 17, 2023
1 parent b1faf12 commit 5424fc9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
52 changes: 40 additions & 12 deletions pkg/store/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1384,19 +1384,40 @@ func benchBucketSeries(t testutil.TB, sampleType chunkenc.ValueType, skipChunk b
seriesCut = expectedSamples / samplesPerSeriesPerBlock
}

bCases = append(bCases, &storetestutil.SeriesCase{
Name: fmt.Sprintf("%dof%d", expectedSamples, totalSeries*samplesPerSeries),
Req: &storepb.SeriesRequest{
MinTime: 0,
MaxTime: int64(expectedSamples) - 1,
Matchers: []storepb.LabelMatcher{
{Type: storepb.LabelMatcher_EQ, Name: "foo", Value: "bar"},
matchersCase := []*labels.Matcher{
labels.MustNewMatcher(labels.MatchEqual, "foo", "bar"),
labels.MustNewMatcher(labels.MatchNotEqual, "foo", "bar"),
labels.MustNewMatcher(labels.MatchRegexp, "j", "(0|1)"),
labels.MustNewMatcher(labels.MatchRegexp, "j", "0|1"),
labels.MustNewMatcher(labels.MatchNotRegexp, "j", "(0|1)"),
labels.MustNewMatcher(labels.MatchNotRegexp, "j", "0|1"),
}

for _, lm := range matchersCase {
expectedSeries := make([]*storepb.Series, 0, seriesCut)
m, err := storepb.PromMatchersToMatchers(lm)
testutil.Ok(t, err)

// seriesCut does not cut chunks properly, but those are assured against for non benchmarks only, where we use 100% case only.
for _, s := range series[:seriesCut] {
for _, label := range s.Labels {
if label.Name == lm.Name && lm.Matches(label.Value) {
expectedSeries = append(expectedSeries, s)
break
}
}
}
bCases = append(bCases, &storetestutil.SeriesCase{
Name: fmt.Sprintf("%dof%d[%s]", expectedSamples, totalSeries*samplesPerSeries, lm.String()),
Req: &storepb.SeriesRequest{
MinTime: 0,
MaxTime: int64(expectedSamples) - 1,
Matchers: m,
SkipChunks: skipChunk,
},
SkipChunks: skipChunk,
},
// This does not cut chunks properly, but those are assured against for non benchmarks only, where we use 100% case only.
ExpectedSeries: series[:seriesCut],
})
ExpectedSeries: expectedSeries,
})
}
}
storetestutil.TestServerSeries(t, st, bCases...)

Expand Down Expand Up @@ -2496,3 +2517,10 @@ func BenchmarkDownsampledBlockSeries(b *testing.B) {
}
}
}

func min(a, b int) int {
if a <= b {
return a
}
return b
}
9 changes: 8 additions & 1 deletion pkg/store/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ func init() {
func findSetMatches(pattern string) []string {
escaped := false
sets := []*strings.Builder{{}}
for i := 0; i < len(pattern); i++ {
init := 0
end := len(pattern)
// If the regex is wrapped in a group we can remove the first and last parentheses
if pattern[init] == '(' && pattern[end-1] == ')' {
init++
end--
}
for i := init; i < end; i++ {
if escaped {
switch {
case isRegexMetaCharacter(pattern[i]):
Expand Down
13 changes: 13 additions & 0 deletions pkg/store/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ func TestFindSetMatches(t *testing.T) {
"baz",
},
},
// Simple sets with group wrapper.
{
pattern: "(foo|bar|baz)",
exp: []string{
"foo",
"bar",
"baz",
},
},
// Simple sets containing escaped characters.
{
pattern: "fo\\.o|bar\\?|\\^baz",
Expand All @@ -38,6 +47,10 @@ func TestFindSetMatches(t *testing.T) {
pattern: "fo.o|bar?|^baz",
exp: nil,
},
{
pattern: "(fool|bar)|(baz)",
exp: nil,
},
{
pattern: "foo\\|bar\\|baz",
exp: []string{
Expand Down
2 changes: 1 addition & 1 deletion pkg/store/storepb/testutil/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func ReadSeriesFromBlock(t testing.TB, h tsdb.BlockReader, extLabels labels.Labe
func appendFloatSamples(t testing.TB, app storage.Appender, tsLabel int, opts HeadGenOptions) {
ref, err := app.Append(
0,
labels.FromStrings("foo", "bar", "i", fmt.Sprintf("%07d%s", tsLabel, LabelLongSuffix)),
labels.FromStrings("foo", "bar", "i", fmt.Sprintf("%07d%s", tsLabel, LabelLongSuffix), "j", fmt.Sprintf("%v", tsLabel)),
int64(tsLabel)*opts.ScrapeInterval.Milliseconds(),
opts.Random.Float64(),
)
Expand Down

0 comments on commit 5424fc9

Please sign in to comment.