proxygen
DigestBuilderBenchmark.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <chrono>
20 #include <condition_variable>
21 #include <thread>
22 
23 #include <boost/thread/barrier.hpp>
24 
25 #include <folly/Benchmark.h>
26 #include <folly/Range.h>
28 
29 DEFINE_int32(digest_merge_time_ns, 5500, "Time to merge into the digest");
30 
31 using namespace folly;
32 using namespace folly::detail;
33 
34 class FreeDigest {
35  public:
36  explicit FreeDigest(size_t) {}
37 
40  auto finish = start + std::chrono::nanoseconds{FLAGS_digest_merge_time_ns};
41  while (std::chrono::steady_clock::now() < finish) {
42  }
43  return FreeDigest(100);
44  }
45 };
46 
47 unsigned int append(unsigned int iters, size_t bufSize, size_t nThreads) {
48  iters = 1000000;
49  auto buffer = std::make_shared<DigestBuilder<FreeDigest>>(bufSize, 100);
50 
51  auto barrier = std::make_shared<boost::barrier>(nThreads + 1);
52 
53  std::vector<std::thread> threads;
54  threads.reserve(nThreads);
56  for (size_t i = 0; i < nThreads; ++i) {
57  threads.emplace_back([&]() {
58  barrier->wait();
59  for (size_t iter = 0; iter < iters; ++iter) {
60  buffer->append(iter);
61  }
62  barrier->wait();
63  });
64  }
65  barrier->wait();
66  }
67  barrier->wait();
68 
70  for (auto& thread : threads) {
71  thread.join();
72  }
73  }
74  return iters;
75 }
76 
77 BENCHMARK_NAMED_PARAM_MULTI(append, 1000x1, 1000, 1)
84 BENCHMARK_NAMED_PARAM_MULTI(append, 10000x1, 10000, 1)
88 BENCHMARK_RELATIVE_NAMED_PARAM_MULTI(append, 10000x16, 10000, 16)
89 BENCHMARK_RELATIVE_NAMED_PARAM_MULTI(append, 10000x32, 10000, 32)
90 
91 /*
92  * ./digest_buffer_benchmark
93  * ============================================================================
94  * folly/stats/test/DigestBuilderBenchmark.cpp relative time/iter iters/s
95  * ============================================================================
96  * append(1000x1) 16.32ns 61.26M
97  * append(1000x2) 96.42% 16.93ns 59.07M
98  * append(1000x4) 93.57% 17.44ns 57.33M
99  * append(1000x8) 93.43% 17.47ns 57.24M
100  * append(1000x16) 92.96% 17.56ns 56.95M
101  * append(1000x32) 29.23% 55.84ns 17.91M
102  * ----------------------------------------------------------------------------
103  * append(10000x1) 11.58ns 86.33M
104  * append(10000x2) 95.82% 12.09ns 82.72M
105  * append(10000x4) 89.00% 13.01ns 76.84M
106  * append(10000x8) 88.63% 13.07ns 76.51M
107  * append(10000x16) 88.22% 13.13ns 76.16M
108  * append(10000x32) 24.89% 46.54ns 21.49M
109  * ============================================================================
110  */
111 
112 int main(int argc, char* argv[]) {
113  gflags::ParseCommandLineFlags(&argc, &argv, true);
115  return 0;
116 }
std::vector< uint8_t > buffer(kBufferSize+16)
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
std::chrono::steady_clock::time_point now()
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void runBenchmarks()
Definition: Benchmark.cpp:456
unsigned int append(unsigned int iters, size_t bufSize, size_t nThreads)
std::vector< std::thread::id > threads
char ** argv
#define BENCHMARK_RELATIVE_NAMED_PARAM_MULTI(name, param_name,...)
Definition: Benchmark.h:545
auto start
FreeDigest merge(Range< const double * >) const
BENCHMARK_DRAW_LINE()
DEFINE_int32(digest_merge_time_ns, 5500,"Time to merge into the digest")
int main(int argc, char *argv[])
#define BENCHMARK_NAMED_PARAM_MULTI(name, param_name,...)
Definition: Benchmark.h:463