gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/perf/linux/stat_benchmark.cc (about) 1 // Copyright 2020 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include <sys/stat.h> 16 #include <sys/types.h> 17 #include <unistd.h> 18 19 #include "gtest/gtest.h" 20 #include "absl/strings/str_cat.h" 21 #include "benchmark/benchmark.h" 22 #include "test/util/fs_util.h" 23 #include "test/util/temp_path.h" 24 #include "test/util/test_util.h" 25 26 namespace gvisor { 27 namespace testing { 28 29 namespace { 30 31 // Creates a file in a nested directory hierarchy at least `depth` directories 32 // deep, and stats that file multiple times. 33 void BM_Stat(benchmark::State& state) { 34 // Create nested directories with given depth. 35 int depth = state.range(0); 36 const TempPath top_dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()); 37 std::string dir_path = top_dir.path(); 38 39 while (depth-- > 0) { 40 // Don't use TempPath because it will make paths too long to use. 41 // 42 // The top_dir destructor will clean up this whole tree. 43 dir_path = JoinPath(dir_path, absl::StrCat(depth)); 44 ASSERT_NO_ERRNO(Mkdir(dir_path, 0755)); 45 } 46 47 // Create the file that will be stat'd. 48 const TempPath file = 49 ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir_path)); 50 51 struct stat st; 52 for (auto _ : state) { 53 ASSERT_THAT(stat(file.path().c_str(), &st), SyscallSucceeds()); 54 } 55 } 56 57 BENCHMARK(BM_Stat)->Range(1, 100)->UseRealTime(); 58 59 } // namespace 60 61 } // namespace testing 62 } // namespace gvisor