Benchmark for ReadFdToString

Test: atest libbase_benchmark
Change-Id: Ibfe8f7b3e5446f0f9320950d076b015d9e3ff99e
diff --git a/Android.bp b/Android.bp
index d2ad009..d2cdadd 100644
--- a/Android.bp
+++ b/Android.bp
@@ -284,6 +284,7 @@
     defaults: ["libbase_cflags_defaults"],
 
     srcs: [
+        "file_benchmark.cpp",
         "format_benchmark.cpp",
         "function_ref_benchmark.cpp",
     ],
diff --git a/file_benchmark.cpp b/file_benchmark.cpp
new file mode 100644
index 0000000..86252ce
--- /dev/null
+++ b/file_benchmark.cpp
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <benchmark/benchmark.h>
+
+#include "android-base/file.h"
+#include "android-base/logging.h"
+
+static void BenchmarkReadFdToString(benchmark::State& state) {
+  android::base::unique_fd fd(memfd_create("memfile", 0));
+  CHECK(fd.get() > 0);
+  CHECK_EQ(ftruncate(fd, state.range(0)), 0);
+  for (auto _ : state) {
+    CHECK_EQ(lseek(fd, 0, SEEK_SET), 0);
+    std::string str;
+    benchmark::DoNotOptimize(android::base::ReadFdToString(fd, &str));
+  }
+  state.SetBytesProcessed(state.iterations() * state.range(0));
+}
+
+BENCHMARK_RANGE(BenchmarkReadFdToString, 0, 1024 * 1024);