Kea  1.9.9-git
memfile_lease_mgr_benchmark.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2019 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
13 #include <dhcpsrv/testutils/lease_file_io.h>
14 
15 using namespace isc::dhcp;
16 using namespace isc::dhcp::bench;
17 using namespace isc::dhcp::test;
18 using namespace std;
19 
20 namespace {
21 
23 class MemfileLeaseMgrBenchmark : public GenericLeaseMgrBenchmark {
24 public:
28  MemfileLeaseMgrBenchmark() : io4_(""), io6_("") {
29  }
30 
36  void SetUp(::benchmark::State const&) override {
37 
38  io4_ = LeaseFileIO(getLeaseFilePath("leasefile4_0.csv"));
39  io6_ = LeaseFileIO(getLeaseFilePath("leasefile6_0.csv"));
40 
41  // Remove lease files and products of Lease File Cleanup.
42  removeFiles(getLeaseFilePath("leasefile4_0.csv"));
43  removeFiles(getLeaseFilePath("leasefile6_0.csv"));
44 
45  try {
47  startBackend(V4);
48  } catch (...) {
49  std::cerr << "ERROR: unable to start memfile backend." << std::endl;
50  throw;
51  }
52  lmptr_ = &(LeaseMgrFactory::instance());
53  }
54 
55  void SetUp(::benchmark::State& s) override {
56  ::benchmark::State const& cs = s;
57  SetUp(cs);
58  }
59 
63  void startBackend(Universe u) {
64  try {
65  LeaseMgrFactory::create(getConfigString(u));
66  } catch (...) {
67  std::cerr << "*** ERROR: unable to create instance of the Memfile "
68  << "lease database backend." << std::endl;
69  throw;
70  }
71  lmptr_ = &(LeaseMgrFactory::instance());
72  }
73 
84  static std::string getConfigString(Universe u) {
85  std::ostringstream s;
86  s << "type=memfile " << (u == V4 ? "universe=4 " : "universe=6 ")
87  << "name=" << getLeaseFilePath(u == V4 ? "leasefile4_0.csv" : "leasefile6_0.csv")
88  << " lfc-interval=0";
89  return (s.str());
90  }
91 
98  static std::string getLeaseFilePath(const std::string& filename) {
99  std::ostringstream s;
100  s << TEST_DATA_BUILDDIR << "/" << filename;
101  return (s.str());
102  }
103 
109  void removeFiles(const std::string& base_name) const {
110  // Generate suffixes and append them to the base name. The
111  // resulting file names are the ones that may exist as a
112  // result of LFC.
113  for (int i = static_cast<int>(Memfile_LeaseMgr::FILE_CURRENT);
114  i <= static_cast<int>(Memfile_LeaseMgr::FILE_FINISH); ++i) {
116  LeaseFileIO io(Memfile_LeaseMgr::appendSuffix(base_name, type));
117  io.removeFile();
118  }
119  }
120 
126  void TearDown(::benchmark::State const&) override {
127  try {
128  lmptr_->rollback();
129  } catch (...) {
130  std::cerr << "WARNING: rollback has failed. This is surprising as "
131  << "memfile doesn't support rollback." << std::endl;
132  }
133 
135 
136  // Remove lease files and products of Lease File Cleanup.
137  removeFiles(getLeaseFilePath("leasefile4_0.csv"));
138  removeFiles(getLeaseFilePath("leasefile6_0.csv"));
139  }
140 
141  void TearDown(::benchmark::State& s) override {
142  ::benchmark::State const& cs = s;
143  TearDown(cs);
144  }
145 
147  LeaseFileIO io4_;
148 
150  LeaseFileIO io6_;
151 };
152 
153 // Defines a benchmark that measures IPv4 leases insertion.
154 BENCHMARK_DEFINE_F(MemfileLeaseMgrBenchmark, insertLeases4)(benchmark::State& state) {
155  const size_t lease_count = state.range(0);
156  while (state.KeepRunning()) {
157  setUp4(state, lease_count);
158  benchInsertLeases4();
159  }
160 }
161 
162 // Defines a benchmark that measures IPv4 leases update.
163 BENCHMARK_DEFINE_F(MemfileLeaseMgrBenchmark, updateLeases4)(benchmark::State& state) {
164  const size_t lease_count = state.range(0);
165  while (state.KeepRunning()) {
166  setUpWithInserts4(state, lease_count);
167  benchUpdateLeases4();
168  }
169 }
170 
171 // Defines a benchmark that measures IPv4 leases retrieval by address.
172 BENCHMARK_DEFINE_F(MemfileLeaseMgrBenchmark, getLease4_address)(benchmark::State& state) {
173  const size_t lease_count = state.range(0);
174  while (state.KeepRunning()) {
175  setUpWithInserts4(state, lease_count);
176  benchGetLease4_address();
177  }
178 }
179 
180 // Defines a benchmark that measures IPv4 leases retrieval by hardware address.
181 BENCHMARK_DEFINE_F(MemfileLeaseMgrBenchmark, getLease4_hwaddr)(benchmark::State& state) {
182  const size_t lease_count = state.range(0);
183  while (state.KeepRunning()) {
184  setUpWithInserts4(state, lease_count);
185  benchGetLease4_hwaddr();
186  }
187 }
188 
189 // Defines a benchmark that measures IPv4 leases retrieval by hardware address
190 // and subnet-id.
191 BENCHMARK_DEFINE_F(MemfileLeaseMgrBenchmark, getLease4_hwaddr_subnetid)(benchmark::State& state) {
192  const size_t lease_count = state.range(0);
193  while (state.KeepRunning()) {
194  setUpWithInserts4(state, lease_count);
195  benchGetLease4_hwaddr_subnetid();
196  }
197 }
198 
199 // Defines a benchmark that measures IPv4 leases retrieval by client-id.
200 BENCHMARK_DEFINE_F(MemfileLeaseMgrBenchmark, getLease4_clientid)(benchmark::State& state) {
201  const size_t lease_count = state.range(0);
202  while (state.KeepRunning()) {
203  setUpWithInserts4(state, lease_count);
204  benchGetLease4_clientid();
205  }
206 }
207 
208 // Defines a benchmark that measures IPv4 leases retrieval by client-id and
209 // subnet-id.
210 BENCHMARK_DEFINE_F(MemfileLeaseMgrBenchmark, getLease4_clientid_subnetid)(benchmark::State& state) {
211  const size_t lease_count = state.range(0);
212  while (state.KeepRunning()) {
213  setUpWithInserts4(state, lease_count);
214  benchGetLease4_clientid_subnetid();
215  }
216 }
217 
218 // Defines a benchmark that measures retrieval of expired IPv4 leases.
219 BENCHMARK_DEFINE_F(MemfileLeaseMgrBenchmark, getExpiredLeases4)(benchmark::State& state) {
220  const size_t lease_count = state.range(0);
221  while (state.KeepRunning()) {
222  setUpWithInserts4(state, lease_count);
223  benchGetExpiredLeases4();
224  }
225 }
226 
227 // Defines a benchmark that measures IPv6 leases insertion.
228 BENCHMARK_DEFINE_F(MemfileLeaseMgrBenchmark, insertLeases6)(benchmark::State& state) {
229  const size_t lease_count = state.range(0);
230  while (state.KeepRunning()) {
231  setUp6(state, lease_count);
232  benchInsertLeases6();
233  }
234 }
235 
236 // Defines a benchmark that measures IPv6 leases update.
237 BENCHMARK_DEFINE_F(MemfileLeaseMgrBenchmark, updateLeases6)(benchmark::State& state) {
238  const size_t lease_count = state.range(0);
239  while (state.KeepRunning()) {
240  setUpWithInserts6(state, lease_count);
241  benchUpdateLeases6();
242  }
243 }
244 
245 // Defines a benchmark that measures IPv6 leases retrieval by type and address.
246 BENCHMARK_DEFINE_F(MemfileLeaseMgrBenchmark, getLease6_type_address)(benchmark::State& state) {
247  const size_t lease_count = state.range(0);
248  while (state.KeepRunning()) {
249  setUpWithInserts6(state, lease_count);
250  benchGetLease6_type_address();
251  }
252 }
253 
254 // Defines a benchmark that measures IPv6 leases retrieval by type, duid and iaid.
255 BENCHMARK_DEFINE_F(MemfileLeaseMgrBenchmark, getLease6_type_duid_iaid)(benchmark::State& state) {
256  const size_t lease_count = state.range(0);
257  while (state.KeepRunning()) {
258  setUpWithInserts6(state, lease_count);
259  benchGetLease6_type_duid_iaid();
260  }
261 }
262 
263 // Defines a benchmark that measures IPv6 leases retrieval by lease type, duid, iaid
264 // and subnet-id.
265 BENCHMARK_DEFINE_F(MemfileLeaseMgrBenchmark, getLease6_type_duid_iaid_subnetid)
266  (benchmark::State& state) {
267  const size_t lease_count = state.range(0);
268  while (state.KeepRunning()) {
269  setUpWithInserts6(state, lease_count);
270  benchGetLease6_type_duid_iaid_subnetid();
271  }
272 }
273 
274 // Defines a benchmark that measures retrieval of expired IPv6 leases.
275 BENCHMARK_DEFINE_F(MemfileLeaseMgrBenchmark, getExpiredLeases6)(benchmark::State& state) {
276  const size_t lease_count = state.range(0);
277  while (state.KeepRunning()) {
278  setUpWithInserts6(state, lease_count);
279  benchGetExpiredLeases6();
280  }
281 }
282 
285 
287 BENCHMARK_REGISTER_F(MemfileLeaseMgrBenchmark, insertLeases4)
288  ->Range(MIN_LEASE_COUNT, MAX_LEASE_COUNT)->Unit(UNIT);
289 
291 BENCHMARK_REGISTER_F(MemfileLeaseMgrBenchmark, updateLeases4)
292  ->Range(MIN_LEASE_COUNT, MAX_LEASE_COUNT)->Unit(UNIT);
293 
295 BENCHMARK_REGISTER_F(MemfileLeaseMgrBenchmark, getLease4_address)
296  ->Range(MIN_LEASE_COUNT, MAX_LEASE_COUNT)->Unit(UNIT);
297 
299 BENCHMARK_REGISTER_F(MemfileLeaseMgrBenchmark, getLease4_hwaddr)
300  ->Range(MIN_LEASE_COUNT, MAX_LEASE_COUNT)->Unit(UNIT);
301 
304 BENCHMARK_REGISTER_F(MemfileLeaseMgrBenchmark, getLease4_hwaddr_subnetid)
305  ->Range(MIN_LEASE_COUNT, MAX_LEASE_COUNT)->Unit(UNIT);
306 
308 BENCHMARK_REGISTER_F(MemfileLeaseMgrBenchmark, getLease4_clientid)
309  ->Range(MIN_LEASE_COUNT, MAX_LEASE_COUNT)->Unit(UNIT);
310 
312 BENCHMARK_REGISTER_F(MemfileLeaseMgrBenchmark, getLease4_clientid_subnetid)
313  ->Range(MIN_LEASE_COUNT, MAX_LEASE_COUNT)->Unit(UNIT);
314 
316 BENCHMARK_REGISTER_F(MemfileLeaseMgrBenchmark, getExpiredLeases4)
317  ->Range(MIN_LEASE_COUNT, MAX_LEASE_COUNT)->Unit(UNIT);
318 
320 BENCHMARK_REGISTER_F(MemfileLeaseMgrBenchmark, insertLeases6)
321  ->Range(MIN_LEASE_COUNT, MAX_LEASE_COUNT)->Unit(UNIT);
322 
324 BENCHMARK_REGISTER_F(MemfileLeaseMgrBenchmark, updateLeases6)
325  ->Range(MIN_LEASE_COUNT, MAX_LEASE_COUNT)->Unit(UNIT);
326 
328 BENCHMARK_REGISTER_F(MemfileLeaseMgrBenchmark, getLease6_type_address)
329  ->Range(MIN_LEASE_COUNT, MAX_LEASE_COUNT)->Unit(UNIT);
330 
332 BENCHMARK_REGISTER_F(MemfileLeaseMgrBenchmark, getLease6_type_duid_iaid)
333  ->Range(MIN_LEASE_COUNT, MAX_LEASE_COUNT)->Unit(UNIT);
334 
337 BENCHMARK_REGISTER_F(MemfileLeaseMgrBenchmark, getLease6_type_duid_iaid_subnetid)
338  ->Range(MIN_LEASE_COUNT, MAX_LEASE_COUNT)->Unit(UNIT);
339 
341 BENCHMARK_REGISTER_F(MemfileLeaseMgrBenchmark, getExpiredLeases6)
342  ->Range(MIN_LEASE_COUNT, MAX_LEASE_COUNT)->Unit(UNIT);
343 
344 } // namespace
static void destroy()
Destroy lease manager.
STL namespace.
LFCFileType
Types of the lease files used by the Lease File Cleanup.
constexpr benchmark::TimeUnit UNIT
A time unit used - all results to be expressed in us (microseconds)
Definition: parameters.h:35
constexpr size_t MAX_LEASE_COUNT
A maximum number of leases used in a benchmark.
Definition: parameters.h:27
A base class for a fixture for specific lease manager benchmarks.
static void create(const std::string &dbaccess)
Create an instance of a lease manager.
static LeaseMgr & instance()
Return current lease manager.
static std::string appendSuffix(const std::string &file_name, const LFCFileType &file_type)
Appends appropriate suffix to the file name.
constexpr size_t MIN_LEASE_COUNT
A minimum number of leases used in a benchmark.
Definition: parameters.h:25