From f2309f4e7bb84c6d1f0544eca84a79aa1d5c299f Mon Sep 17 00:00:00 2001 From: Roman Gershman Date: Tue, 28 Jan 2025 17:43:41 +0200 Subject: [PATCH] chore: add reflex matcher to the benchmarks (#4520) Add a test covering stringmatchlen. Signed-off-by: Roman Gershman --- src/core/dfly_core_test.cc | 44 ++++++++++++++++++++++++++++++++++++ src/server/dragonfly_test.cc | 33 +++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/src/core/dfly_core_test.cc b/src/core/dfly_core_test.cc index cfeee87ef..f9eebec28 100644 --- a/src/core/dfly_core_test.cc +++ b/src/core/dfly_core_test.cc @@ -6,8 +6,14 @@ #include "core/intent_lock.h" #include "core/tx_queue.h" +extern "C" { +#include "redis/util.h" +} + namespace dfly { +using namespace std; + class TxQueueTest : public ::testing::Test { protected: TxQueueTest() { @@ -65,4 +71,42 @@ TEST_F(IntentLockTest, Basic) { ASSERT_TRUE(lk_.Check(IntentLock::EXCLUSIVE)); } +class StringMatchTest : public ::testing::Test { + protected: + // wrapper around stringmatchlen with stringview arguments + int MatchLen(string_view pattern, string_view str, bool nocase) { + return stringmatchlen(pattern.data(), pattern.size(), str.data(), str.size(), nocase); + } +}; + +TEST_F(StringMatchTest, Basic) { + // ExactMatch + EXPECT_EQ(MatchLen("hello", "hello", 0), 1); + EXPECT_EQ(MatchLen("hello", "world", 0), 0); + + // Wildcards + EXPECT_EQ(MatchLen("*", "hello", 0), 1); + EXPECT_EQ(MatchLen("h*", "hello", 0), 1); + EXPECT_EQ(MatchLen("h*o", "hello", 0), 1); + EXPECT_EQ(MatchLen("hel*o*", "hello*", 0), 1); + + // Single character wildcard + EXPECT_EQ(MatchLen("h[aeiou]llo", "hello", 0), 1); + EXPECT_EQ(MatchLen("h[aeiou]llo", "hallo", 0), 1); + EXPECT_EQ(MatchLen("h[^aeiou]llo", "hallo", 0), 0); + EXPECT_EQ(MatchLen("h[a-z]llo", "hello", 0), 1); + + EXPECT_EQ(MatchLen("h[A-Z]llo", "Hello", 1), 1); + + EXPECT_EQ(MatchLen("h\\*llo", "h*llo", 0), 1); + EXPECT_EQ(MatchLen("h\\\\llo", "h\\llo", 0), 1); + + // ? + EXPECT_EQ(MatchLen("h?llo", "hello", 0), 1); + EXPECT_EQ(MatchLen("h??llo", "ha llo", 0), 1); + EXPECT_EQ(MatchLen("h??llo", "hallo", 0), 0); + EXPECT_EQ(MatchLen("h\\?llo", "hallo", 0), 0); + EXPECT_EQ(MatchLen("h\\?llo", "h?llo", 0), 1); +} + } // namespace dfly diff --git a/src/server/dragonfly_test.cc b/src/server/dragonfly_test.cc index 4efaf4301..1db8353ea 100644 --- a/src/server/dragonfly_test.cc +++ b/src/server/dragonfly_test.cc @@ -13,6 +13,7 @@ extern "C" { #include #include #include +#include #include "base/flags.h" #include "base/gtest.h" @@ -891,5 +892,37 @@ static void BM_MatchFindSubstr(benchmark::State& state) { } BENCHMARK(BM_MatchFindSubstr)->Arg(1000)->Arg(10000); +static void BM_MatchReflexFind(benchmark::State& state) { + absl::InsecureBitGen eng; + string random_val = GetRandomHex(eng, state.range(0)); + reflex::Matcher matcher("foobar"); + matcher.input("xxxxxxfoobaryyyyyyyy"); + CHECK_GT(matcher.find(), 0u); + matcher.input("xxxxxxfoobayyyyyyyy"); + CHECK_EQ(0u, matcher.find()); + + while (state.KeepRunning()) { + matcher.input(random_val); + DoNotOptimize(matcher.find()); + } +} +BENCHMARK(BM_MatchReflexFind)->Arg(1000)->Arg(10000); + +static void BM_MatchReflexMatch(benchmark::State& state) { + absl::InsecureBitGen eng; + string random_val = GetRandomHex(eng, state.range(0)); + reflex::Matcher matcher(".*foobar.*"); + matcher.input("xxxxxxfoobaryyyyyyyy"); + CHECK_GT(matcher.matches(), 0u); + matcher.input("xxxxxxfoobayyyyyyyy"); + CHECK_EQ(0u, matcher.matches()); + + matcher.input(random_val); + while (state.KeepRunning()) { + matcher.input(random_val); + DoNotOptimize(matcher.matches()); + } +} +BENCHMARK(BM_MatchReflexMatch)->Arg(1000)->Arg(10000); } // namespace dfly