1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 // This benchmark exists to show that byte-buffer copy is size-independent
20
21 #include <memory>
22
23 #include <benchmark/benchmark.h>
24
25 #include <grpc/byte_buffer.h>
26 #include <grpc/byte_buffer_reader.h>
27 #include <grpc/slice.h>
28 #include <grpcpp/impl/grpc_library.h>
29 #include <grpcpp/support/byte_buffer.h>
30
31 #include "test/core/util/test_config.h"
32 #include "test/cpp/microbenchmarks/helpers.h"
33 #include "test/cpp/util/test_config.h"
34
35 namespace grpc {
36 namespace testing {
37
BM_ByteBuffer_Copy(benchmark::State & state)38 static void BM_ByteBuffer_Copy(benchmark::State& state) {
39 int num_slices = state.range(0);
40 size_t slice_size = state.range(1);
41 std::vector<grpc::Slice> slices;
42 while (num_slices > 0) {
43 num_slices--;
44 std::unique_ptr<char[]> buf(new char[slice_size]);
45 memset(buf.get(), 0, slice_size);
46 slices.emplace_back(buf.get(), slice_size);
47 }
48 grpc::ByteBuffer bb(slices.data(), num_slices);
49 for (auto _ : state) {
50 grpc::ByteBuffer cc(bb);
51 }
52 }
53 BENCHMARK(BM_ByteBuffer_Copy)->Ranges({{1, 64}, {1, 1024 * 1024}});
54
BM_ByteBufferReader_Next(benchmark::State & state)55 static void BM_ByteBufferReader_Next(benchmark::State& state) {
56 const int num_slices = state.range(0);
57 constexpr size_t kSliceSize = 16;
58 std::vector<grpc_slice> slices;
59 for (int i = 0; i < num_slices; ++i) {
60 std::unique_ptr<char[]> buf(new char[kSliceSize]);
61 slices.emplace_back(grpc_slice_from_copied_buffer(buf.get(), kSliceSize));
62 }
63 grpc_byte_buffer* bb = grpc_raw_byte_buffer_create(slices.data(), num_slices);
64 grpc_byte_buffer_reader reader;
65 GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, bb));
66 for (auto _ : state) {
67 grpc_slice* slice;
68 if (GPR_UNLIKELY(!grpc_byte_buffer_reader_peek(&reader, &slice))) {
69 grpc_byte_buffer_reader_destroy(&reader);
70 GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, bb));
71 continue;
72 }
73 }
74
75 grpc_byte_buffer_reader_destroy(&reader);
76 grpc_byte_buffer_destroy(bb);
77 for (auto& slice : slices) {
78 grpc_slice_unref(slice);
79 }
80 }
81 BENCHMARK(BM_ByteBufferReader_Next)->Ranges({{64 * 1024, 1024 * 1024}});
82
BM_ByteBufferReader_Peek(benchmark::State & state)83 static void BM_ByteBufferReader_Peek(benchmark::State& state) {
84 const int num_slices = state.range(0);
85 constexpr size_t kSliceSize = 16;
86 std::vector<grpc_slice> slices;
87 for (int i = 0; i < num_slices; ++i) {
88 std::unique_ptr<char[]> buf(new char[kSliceSize]);
89 slices.emplace_back(grpc_slice_from_copied_buffer(buf.get(), kSliceSize));
90 }
91 grpc_byte_buffer* bb = grpc_raw_byte_buffer_create(slices.data(), num_slices);
92 grpc_byte_buffer_reader reader;
93 GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, bb));
94 for (auto _ : state) {
95 grpc_slice* slice;
96 if (GPR_UNLIKELY(!grpc_byte_buffer_reader_peek(&reader, &slice))) {
97 grpc_byte_buffer_reader_destroy(&reader);
98 GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, bb));
99 continue;
100 }
101 }
102
103 grpc_byte_buffer_reader_destroy(&reader);
104 grpc_byte_buffer_destroy(bb);
105 for (auto& slice : slices) {
106 grpc_slice_unref(slice);
107 }
108 }
109 BENCHMARK(BM_ByteBufferReader_Peek)->Ranges({{64 * 1024, 1024 * 1024}});
110
111 } // namespace testing
112 } // namespace grpc
113
114 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
115 // and others do not. This allows us to support both modes.
116 namespace benchmark {
RunTheBenchmarksNamespaced()117 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
118 } // namespace benchmark
119
main(int argc,char ** argv)120 int main(int argc, char** argv) {
121 grpc::testing::TestEnvironment env(&argc, argv);
122 LibraryInitializer libInit;
123 ::benchmark::Initialize(&argc, argv);
124 grpc::testing::InitTest(&argc, &argv, false);
125
126 benchmark::RunTheBenchmarksNamespaced();
127 return 0;
128 }
129