gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/perf/linux/write_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 <fcntl.h> 16 #include <stdlib.h> 17 #include <sys/stat.h> 18 #include <unistd.h> 19 20 #include "gtest/gtest.h" 21 #include "benchmark/benchmark.h" 22 #include "test/util/logging.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 void BM_Write(benchmark::State& state) { 32 auto f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 33 FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(f.path(), O_WRONLY)); 34 35 const int size = state.range(0); 36 std::vector<char> buf(size); 37 RandomizeBuffer(buf.data(), size); 38 39 for (auto _ : state) { 40 TEST_CHECK(PwriteFd(fd.get(), buf.data(), size, 0) == size); 41 } 42 43 state.SetBytesProcessed(static_cast<int64_t>(size) * 44 static_cast<int64_t>(state.iterations())); 45 } 46 47 BENCHMARK(BM_Write)->Range(1, 1 << 26)->UseRealTime(); 48 49 void BM_Append(benchmark::State& state) { 50 auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 51 auto fd = ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_WRONLY | O_APPEND)); 52 53 const char data = 'a'; 54 for (auto _ : state) { 55 TEST_CHECK(WriteFd(fd.get(), &data, 1) == 1); 56 } 57 } 58 59 BENCHMARK(BM_Append); 60 61 } // namespace 62 63 } // namespace testing 64 } // namespace gvisor