blob: fe4eeaa3731ab5a3c7bf90e375abdc4a2b52e3ed [file] [log] [blame]
Junxiao Shi85d17fc2015-01-21 22:18:17 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa997d292017-08-24 20:16:59 -04002/*
Davide Pesavento1b22a8c2022-03-07 21:23:23 -05003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi85d17fc2015-01-21 22:18:17 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
Eric Newberry52ae3292017-11-16 13:54:52 -070026#include "benchmark-helpers.hpp"
Junxiao Shi85d17fc2015-01-21 22:18:17 -070027#include "table/cs.hpp"
Eric Newberry52ae3292017-11-16 13:54:52 -070028
Davide Pesaventoa997d292017-08-24 20:16:59 -040029#include <iostream>
30
Davide Pesavento264af772021-02-09 21:48:24 -050031#ifdef NFD_HAVE_VALGRIND
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000032#include <valgrind/callgrind.h>
33#endif
34
Junxiao Shi85d17fc2015-01-21 22:18:17 -070035namespace nfd {
36namespace tests {
37
Eric Newberry52ae3292017-11-16 13:54:52 -070038class CsBenchmarkFixture
Junxiao Shi85d17fc2015-01-21 22:18:17 -070039{
40protected:
41 CsBenchmarkFixture()
42 {
43#ifdef _DEBUG
Davide Pesaventoa997d292017-08-24 20:16:59 -040044 std::cerr << "Benchmark compiled in debug mode is unreliable, please compile in release mode.\n";
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000045#endif
Junxiao Shi85d17fc2015-01-21 22:18:17 -070046
47 cs.setLimit(CS_CAPACITY);
48 }
49
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000050 static time::microseconds
51 timedRun(const std::function<void()>& f)
Junxiao Shi85d17fc2015-01-21 22:18:17 -070052 {
Davide Pesavento264af772021-02-09 21:48:24 -050053#ifdef NFD_HAVE_VALGRIND
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000054 CALLGRIND_START_INSTRUMENTATION;
55#endif
56
57 auto t1 = time::steady_clock::now();
Junxiao Shi85d17fc2015-01-21 22:18:17 -070058 f();
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000059 auto t2 = time::steady_clock::now();
60
Davide Pesavento264af772021-02-09 21:48:24 -050061#ifdef NFD_HAVE_VALGRIND
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000062 CALLGRIND_STOP_INSTRUMENTATION;
63#endif
64
Junxiao Shi85d17fc2015-01-21 22:18:17 -070065 return time::duration_cast<time::microseconds>(t2 - t1);
66 }
67
Eric Newberry52ae3292017-11-16 13:54:52 -070068 static shared_ptr<Data>
69 makeData(const Name& name)
70 {
Davide Pesaventob7e72c32020-10-02 20:00:03 -040071 auto data = std::make_shared<Data>(name);
72 data->setSignatureInfo(ndn::SignatureInfo(tlv::NullSignature));
73 data->setSignatureValue(std::make_shared<ndn::Buffer>());
Eric Newberry52ae3292017-11-16 13:54:52 -070074 data->wireEncode();
75 return data;
76 }
77
mzhang4eab72492015-02-25 11:16:09 -060078 void
79 find(const Interest& interest)
80 {
Davide Pesavento412c9822021-07-02 00:21:05 -040081 cs.find(interest, [] (auto&&...) {}, [] (auto&&...) {});
mzhang4eab72492015-02-25 11:16:09 -060082 }
83
Junxiao Shi85d17fc2015-01-21 22:18:17 -070084protected:
Davide Pesavento412c9822021-07-02 00:21:05 -040085 using NameGenerator = std::function<Name (size_t)>;
Junxiao Shi85d17fc2015-01-21 22:18:17 -070086
87 class SimpleNameGenerator
88 {
89 public:
90 explicit
91 SimpleNameGenerator(const Name& prefix = "/cs/benchmark")
92 : m_prefix(prefix)
93 {
94 }
95
96 Name
97 operator()(size_t i) const
98 {
99 Name name(m_prefix);
100 name.appendNumber(i % 4);
101 name.appendNumber(i);
102 return name;
103 }
104
105 private:
106 Name m_prefix;
107 };
108
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000109 static std::vector<shared_ptr<Interest>>
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700110 makeInterestWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator())
111 {
112 std::vector<shared_ptr<Interest>> workload(count);
113 for (size_t i = 0; i < count; ++i) {
114 Name name = genName(i);
Davide Pesavento1b22a8c2022-03-07 21:23:23 -0500115 workload[i] = std::make_shared<Interest>(name);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700116 }
117 return workload;
118 }
119
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000120 static std::vector<shared_ptr<Data>>
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700121 makeDataWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator())
122 {
123 std::vector<shared_ptr<Data>> workload(count);
124 for (size_t i = 0; i < count; ++i) {
125 Name name = genName(i);
126 workload[i] = makeData(name);
127 }
128 return workload;
129 }
130
131protected:
132 Cs cs;
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000133 static constexpr size_t CS_CAPACITY = 50000;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700134};
135
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700136// find miss, then insert
Davide Pesaventoa997d292017-08-24 20:16:59 -0400137BOOST_FIXTURE_TEST_CASE(FindMissInsert, CsBenchmarkFixture)
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700138{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000139 constexpr size_t N_WORKLOAD = CS_CAPACITY * 2;
140 constexpr size_t REPEAT = 4;
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700141
Junxiao Shi9222f362019-04-16 15:15:23 +0000142 auto interestWorkload = makeInterestWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700143 std::vector<shared_ptr<Data>> dataWorkload[REPEAT];
144 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700145 dataWorkload[j] = makeDataWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700146 }
147
148 time::microseconds d = timedRun([&] {
149 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700150 for (size_t i = 0; i < N_WORKLOAD; ++i) {
mzhang4eab72492015-02-25 11:16:09 -0600151 find(*interestWorkload[i]);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700152 cs.insert(*dataWorkload[j][i], false);
153 }
154 }
155 });
Davide Pesaventoa997d292017-08-24 20:16:59 -0400156
157 std::cout << "find(miss)-insert " << (N_WORKLOAD * REPEAT) << ": " << d << std::endl;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700158}
159
160// insert, then find hit
Davide Pesaventoa997d292017-08-24 20:16:59 -0400161BOOST_FIXTURE_TEST_CASE(InsertFindHit, CsBenchmarkFixture)
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700162{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000163 constexpr size_t N_WORKLOAD = CS_CAPACITY * 2;
164 constexpr size_t REPEAT = 4;
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700165
166 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700167 std::vector<shared_ptr<Data>> dataWorkload[REPEAT];
168 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700169 dataWorkload[j] = makeDataWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700170 }
171
172 time::microseconds d = timedRun([&] {
173 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700174 for (size_t i = 0; i < N_WORKLOAD; ++i) {
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700175 cs.insert(*dataWorkload[j][i], false);
mzhang4eab72492015-02-25 11:16:09 -0600176 find(*interestWorkload[i]);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700177 }
178 }
179 });
Davide Pesaventoa997d292017-08-24 20:16:59 -0400180
181 std::cout << "insert-find(hit) " << (N_WORKLOAD * REPEAT) << ": " << d << std::endl;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700182}
183
Junxiao Shi9222f362019-04-16 15:15:23 +0000184// find(CanBePrefix) hit
185BOOST_FIXTURE_TEST_CASE(FindCanBePrefixHit, CsBenchmarkFixture)
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700186{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000187 constexpr size_t N_CHILDREN = 10;
188 constexpr size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN;
189 constexpr size_t REPEAT = 4;
190
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700191 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_INTERESTS);
192 for (auto&& interest : interestWorkload) {
Junxiao Shi9222f362019-04-16 15:15:23 +0000193 interest->setCanBePrefix(true);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700194 for (size_t j = 0; j < N_CHILDREN; ++j) {
195 Name name = interest->getName();
196 name.appendNumber(j);
Junxiao Shi9222f362019-04-16 15:15:23 +0000197 cs.insert(*makeData(name), false);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700198 }
199 }
200 BOOST_REQUIRE(cs.size() == N_INTERESTS * N_CHILDREN);
201
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700202 time::microseconds d = timedRun([&] {
203 for (size_t j = 0; j < REPEAT; ++j) {
204 for (const auto& interest : interestWorkload) {
mzhang4eab72492015-02-25 11:16:09 -0600205 find(*interest);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700206 }
207 }
208 });
Davide Pesaventoa997d292017-08-24 20:16:59 -0400209
Junxiao Shi9222f362019-04-16 15:15:23 +0000210 std::cout << "find(CanBePrefix-hit) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d << std::endl;
Davide Pesaventoa997d292017-08-24 20:16:59 -0400211}
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700212
213} // namespace tests
214} // namespace nfd