1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3 * Copyright 2017 Couchbase, Inc.
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 #include <benchmark/benchmark.h>
19 #include <platform/make_unique.h>
20 #include <platform/pipe.h>
21 #include <algorithm>
22
23 // Benchmark copying data into a blob. This represents how we used to add
24 // to the old write buffer
PlainMemcpy(benchmark::State& state)25 void PlainMemcpy(benchmark::State& state) {
26 std::vector<uint8_t> blob(256);
27 std::vector<uint8_t> data(4096);
28
29 while (state.KeepRunning()) {
30 std::copy(blob.begin(), blob.end(), data.begin());
31 }
32 }
33 BENCHMARK(PlainMemcpy);
34
35 // Benchmark calling the produce part of the pipe to insert data into the
36 // send buffer.
Produce(benchmark::State& state)37 void Produce(benchmark::State& state) {
38 std::vector<uint8_t> blob(256);
39 cb::Pipe pipe(4096);
40
41 while (state.KeepRunning()) {
42 pipe.clear();
43 pipe.produce([&blob](void* ptr, size_t size) -> size_t {
44 std::copy(blob.begin(), blob.end(), static_cast<uint8_t*>(ptr));
45 return blob.size();
46 });
47 }
48 }
49 BENCHMARK(Produce);
50
51 // Benchmark calling the produce part of the pipe by using the wdata() and
52 // produced
ProduceWdata(benchmark::State& state)53 void ProduceWdata(benchmark::State& state) {
54 std::vector<uint8_t> blob(256);
55 cb::Pipe pipe(4096);
56
57 while (state.KeepRunning()) {
58 pipe.clear();
59 auto data = pipe.wdata();
60 std::copy(blob.begin(), blob.end(), data.begin());
61 pipe.produced(256);
62 }
63 }
64 BENCHMARK(ProduceWdata);
65
66
67 // Benchmark calling the consume part of the pipe to check the data just
68 // being sent points to the buffer (this represents the action we've added
69 // to see if just sent data
Consume(benchmark::State& state)70 void Consume(benchmark::State& state) {
71 std::vector<uint8_t> blob(256);
72 cb::Pipe pipe(4096);
73 pipe.produce([](void*, size_t) -> size_t { return 4; });
74 while (state.KeepRunning()) {
75 const char* nil = nullptr;
76 pipe.consume([&nil](const void* ptr, size_t size) -> size_t {
77 if (nil == ptr) {
78 return 0;
79 }
80 return 0;
81 });
82 }
83 }
84 BENCHMARK(Consume);
85
86 // Benchmark fetching the read end of the pipe to peek at the
87 // data
Rdata(benchmark::State& state)88 void Rdata(benchmark::State& state) {
89 cb::Pipe pipe(4096);
90 pipe.produce([](void*, size_t) -> size_t { return 4; });
91 while (state.KeepRunning()) {
92 pipe.rdata();
93 }
94 }
95 BENCHMARK(Rdata);
96
97
98 BENCHMARK_MAIN()
99