Add a benchmark for the shard_cache_mutex option.

Change-Id: Ia96ef5928efd57ca0804de0f553a4718377a4d2c
Reviewed-on: https://code-review.googlesource.com/c/36452
Reviewed-by: Paul Wankadia <junyer@google.com>
diff --git a/re2/testing/regexp_benchmark.cc b/re2/testing/regexp_benchmark.cc
index 8b82e0b..95044e9 100644
--- a/re2/testing/regexp_benchmark.cc
+++ b/re2/testing/regexp_benchmark.cc
@@ -1574,4 +1574,36 @@
 BENCHMARK(PossibleMatchRange_Prefix);
 BENCHMARK(PossibleMatchRange_NoProg);
 
+static const RE2& GetShardCacheMutexRE(bool shard_cache_mutex) {
+  auto GetRE = [=]() {
+    RE2::Options options;
+    options.set_shard_cache_mutex(shard_cache_mutex);
+    return new RE2("foo*bar+qux?", options);
+  };
+  if (!shard_cache_mutex) {
+    static const auto* const re = GetRE();
+    return *re;
+  } else {
+    static const auto* const re = GetRE();
+    return *re;
+  }
+}
+
+void ShardCacheMutex_False(int n) {
+  const RE2& re = GetShardCacheMutexRE(false);
+  for (int i = 0; i < n; i++) {
+    RE2::PartialMatch("", re);
+  }
+}
+
+void ShardCacheMutex_True(int n) {
+  const RE2& re = GetShardCacheMutexRE(true);
+  for (int i = 0; i < n; i++) {
+    RE2::PartialMatch("", re);
+  }
+}
+
+BENCHMARK(ShardCacheMutex_False)->ThreadRange(1, NumCPUs());
+BENCHMARK(ShardCacheMutex_True)->ThreadRange(1, NumCPUs());
+
 }  // namespace re2