github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/perf/linux/pipe_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 <stdlib.h> 16 #include <sys/stat.h> 17 #include <unistd.h> 18 19 #include <cerrno> 20 21 #include "gtest/gtest.h" 22 #include "benchmark/benchmark.h" 23 #include "test/util/logging.h" 24 #include "test/util/test_util.h" 25 #include "test/util/thread_util.h" 26 27 namespace gvisor { 28 namespace testing { 29 30 namespace { 31 32 void BM_Pipe(benchmark::State& state) { 33 int fds[2]; 34 TEST_CHECK(pipe(fds) == 0); 35 36 const int size = state.range(0); 37 std::vector<char> wbuf(size); 38 std::vector<char> rbuf(size); 39 RandomizeBuffer(wbuf.data(), size); 40 41 ScopedThread t([&] { 42 auto const fd = fds[1]; 43 for (int i = 0; i < state.max_iterations; i++) { 44 TEST_CHECK(WriteFd(fd, wbuf.data(), wbuf.size()) == size); 45 } 46 }); 47 48 for (auto _ : state) { 49 TEST_CHECK(ReadFd(fds[0], rbuf.data(), rbuf.size()) == size); 50 } 51 52 t.Join(); 53 54 close(fds[0]); 55 close(fds[1]); 56 57 state.SetBytesProcessed(static_cast<int64_t>(size) * 58 static_cast<int64_t>(state.iterations())); 59 } 60 61 BENCHMARK(BM_Pipe)->Range(1, 1 << 20)->UseRealTime(); 62 63 } // namespace 64 65 } // namespace testing 66 } // namespace gvisor