proxygen
BitsBenchmark.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-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 
17 #include <atomic>
18 #include <memory>
19 #include <random>
20 
21 #include <glog/logging.h>
22 
23 #include <folly/Benchmark.h>
25 
26 std::random_device rd;
27 
28 const size_t kBufferSize = 1 << 10;
29 std::vector<uint8_t> buffer(kBufferSize + 16);
30 
31 template <class T>
32 void benchmarkSet(size_t n, T) {
33  size_t size = sizeof(T) * 6.9; // use 6.9 bits/byte
34  const size_t k = 16;
35  T values[k];
37  std::mt19937 gen(rd());
38  T max, min;
40  max = (T(1) << (size - 1)) - 1;
41  min = -(T(1) << (size - 1));
42  } else {
43  max = (T(1) << size) - 1;
44  min = 0;
45  }
46  CHECK_LE(folly::findLastSet(max), size);
47  CHECK_LE(folly::findLastSet(-min), size);
48  std::uniform_int_distribution<T> dis(min, max);
49  for (size_t i = 0; i < k; ++i) {
50  values[i] = dis(gen);
51  }
52  }
53 
54  for (size_t i = 0; i < n; ++i) {
55  size_t bit = (i * 2973) % (kBufferSize * 8);
56  size_t drop = i % size;
58  reinterpret_cast<T*>(buffer.data()),
59  bit,
60  size - drop,
61  values[i % k] >> drop);
62  }
63 
65  folly::Bits<T>::test(reinterpret_cast<T*>(buffer.data()), 512));
66 }
67 
74 
76 
77 std::atomic<int64_t> sum(0);
78 
79 template <class T>
80 void benchmarkGet(size_t n, T x) {
81  size_t size = sizeof(T) * 6.9; // use 6.9 bits/byte
82  for (size_t i = 0; i < n; ++i) {
83  size_t bit = (i * 2973) % (kBufferSize * 8);
84  size_t drop = i % size;
86  reinterpret_cast<T*>(buffer.data()), bit, size - drop);
87  }
89 }
90 
97 
98 #if 0
99 ============================================================================
100 folly/experimental/test/BitsBenchmark.cpp relative time/iter iters/s
101 ============================================================================
102 benchmarkSet(u16) 8.58ns 116.59M
103 benchmarkSet(i16) 88.42% 9.70ns 103.08M
104 benchmarkSet(u32) 8.37ns 119.45M
105 benchmarkSet(i32) 88.23% 9.49ns 105.39M
106 benchmarkSet(u64) 9.23ns 108.34M
107 benchmarkSet(i64) 82.77% 11.15ns 89.68M
108 ----------------------------------------------------------------------------
109 benchmarkGet(u16) 6.32ns 158.13M
110 benchmarkGet(i16) 80.40% 7.87ns 127.14M
111 benchmarkGet(u32) 6.34ns 157.65M
112 benchmarkGet(i32) 84.61% 7.50ns 133.39M
113 benchmarkGet(u64) 7.32ns 136.58M
114 benchmarkGet(i64) 85.78% 8.53ns 117.16M
115 ============================================================================
116 #endif
117 
118 int main(int argc, char* argv[]) {
119  gflags::ParseCommandLineFlags(&argc, &argv, true);
121  return sum.load();
122 }
Definition: InvokeTest.cpp:58
std::vector< uint8_t > buffer(kBufferSize+16)
#define T(v)
Definition: http_parser.c:233
std::atomic< int64_t > sum(0)
static void set(T *p, size_t bit)
Definition: Bits.h:220
LogLevel max
Definition: LogLevel.cpp:31
BENCHMARK_DRAW_LINE()
static UnderlyingType get(const T *p, size_t bitStart, size_t count)
Definition: Bits.h:285
void benchmarkSet(size_t n, T)
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
#define BENCHMARK_RELATIVE_NAMED_PARAM(name, param_name,...)
Definition: Benchmark.h:531
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void runBenchmarks()
Definition: Benchmark.cpp:456
const size_t kBufferSize
char ** argv
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
int main(int argc, char *argv[])
LogLevel min
Definition: LogLevel.cpp:30
#define BENCHMARK_NAMED_PARAM(name, param_name,...)
Definition: Benchmark.h:449
void benchmarkGet(size_t n, T x)
constexpr unsigned int findLastSet(T const v)
Definition: Bits.h:105
static const char *const value
Definition: Conv.cpp:50
std::random_device rd
static set< string > s
KeyT k
std::chrono::nanoseconds time()
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
std::vector< int > values(1'000)