blob: 793ccf1553f828d52f0ed3120a714262d216c8c1 [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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::tests {
Junxiao Shi85d17fc2015-01-21 22:18:17 -070036
Eric Newberry52ae3292017-11-16 13:54:52 -070037class CsBenchmarkFixture
Junxiao Shi85d17fc2015-01-21 22:18:17 -070038{
39protected:
40 CsBenchmarkFixture()
41 {
42#ifdef _DEBUG
Davide Pesaventoa997d292017-08-24 20:16:59 -040043 std::cerr << "Benchmark compiled in debug mode is unreliable, please compile in release mode.\n";
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000044#endif
Junxiao Shi85d17fc2015-01-21 22:18:17 -070045
46 cs.setLimit(CS_CAPACITY);
47 }
48
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000049 static time::microseconds
50 timedRun(const std::function<void()>& f)
Junxiao Shi85d17fc2015-01-21 22:18:17 -070051 {
Davide Pesavento264af772021-02-09 21:48:24 -050052#ifdef NFD_HAVE_VALGRIND
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000053 CALLGRIND_START_INSTRUMENTATION;
54#endif
55
56 auto t1 = time::steady_clock::now();
Junxiao Shi85d17fc2015-01-21 22:18:17 -070057 f();
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000058 auto t2 = time::steady_clock::now();
59
Davide Pesavento264af772021-02-09 21:48:24 -050060#ifdef NFD_HAVE_VALGRIND
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000061 CALLGRIND_STOP_INSTRUMENTATION;
62#endif
63
Junxiao Shi85d17fc2015-01-21 22:18:17 -070064 return time::duration_cast<time::microseconds>(t2 - t1);
65 }
66
Eric Newberry52ae3292017-11-16 13:54:52 -070067 static shared_ptr<Data>
68 makeData(const Name& name)
69 {
Davide Pesaventob7e72c32020-10-02 20:00:03 -040070 auto data = std::make_shared<Data>(name);
71 data->setSignatureInfo(ndn::SignatureInfo(tlv::NullSignature));
72 data->setSignatureValue(std::make_shared<ndn::Buffer>());
Eric Newberry52ae3292017-11-16 13:54:52 -070073 data->wireEncode();
74 return data;
75 }
76
mzhang4eab72492015-02-25 11:16:09 -060077 void
78 find(const Interest& interest)
79 {
Davide Pesavento412c9822021-07-02 00:21:05 -040080 cs.find(interest, [] (auto&&...) {}, [] (auto&&...) {});
mzhang4eab72492015-02-25 11:16:09 -060081 }
82
Junxiao Shi85d17fc2015-01-21 22:18:17 -070083protected:
Davide Pesavento412c9822021-07-02 00:21:05 -040084 using NameGenerator = std::function<Name (size_t)>;
Junxiao Shi85d17fc2015-01-21 22:18:17 -070085
86 class SimpleNameGenerator
87 {
88 public:
89 explicit
90 SimpleNameGenerator(const Name& prefix = "/cs/benchmark")
91 : m_prefix(prefix)
92 {
93 }
94
95 Name
96 operator()(size_t i) const
97 {
98 Name name(m_prefix);
99 name.appendNumber(i % 4);
100 name.appendNumber(i);
101 return name;
102 }
103
104 private:
105 Name m_prefix;
106 };
107
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000108 static std::vector<shared_ptr<Interest>>
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700109 makeInterestWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator())
110 {
111 std::vector<shared_ptr<Interest>> workload(count);
112 for (size_t i = 0; i < count; ++i) {
113 Name name = genName(i);
Davide Pesavento1b22a8c2022-03-07 21:23:23 -0500114 workload[i] = std::make_shared<Interest>(name);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700115 }
116 return workload;
117 }
118
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000119 static std::vector<shared_ptr<Data>>
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700120 makeDataWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator())
121 {
122 std::vector<shared_ptr<Data>> workload(count);
123 for (size_t i = 0; i < count; ++i) {
124 Name name = genName(i);
125 workload[i] = makeData(name);
126 }
127 return workload;
128 }
129
130protected:
131 Cs cs;
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000132 static constexpr size_t CS_CAPACITY = 50000;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700133};
134
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700135// find miss, then insert
Davide Pesaventoa997d292017-08-24 20:16:59 -0400136BOOST_FIXTURE_TEST_CASE(FindMissInsert, CsBenchmarkFixture)
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700137{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000138 constexpr size_t N_WORKLOAD = CS_CAPACITY * 2;
139 constexpr size_t REPEAT = 4;
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700140
Junxiao Shi9222f362019-04-16 15:15:23 +0000141 auto interestWorkload = makeInterestWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700142 std::vector<shared_ptr<Data>> dataWorkload[REPEAT];
143 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700144 dataWorkload[j] = makeDataWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700145 }
146
147 time::microseconds d = timedRun([&] {
148 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700149 for (size_t i = 0; i < N_WORKLOAD; ++i) {
mzhang4eab72492015-02-25 11:16:09 -0600150 find(*interestWorkload[i]);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700151 cs.insert(*dataWorkload[j][i], false);
152 }
153 }
154 });
Davide Pesaventoa997d292017-08-24 20:16:59 -0400155
156 std::cout << "find(miss)-insert " << (N_WORKLOAD * REPEAT) << ": " << d << std::endl;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700157}
158
159// insert, then find hit
Davide Pesaventoa997d292017-08-24 20:16:59 -0400160BOOST_FIXTURE_TEST_CASE(InsertFindHit, CsBenchmarkFixture)
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700161{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000162 constexpr size_t N_WORKLOAD = CS_CAPACITY * 2;
163 constexpr size_t REPEAT = 4;
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700164
165 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700166 std::vector<shared_ptr<Data>> dataWorkload[REPEAT];
167 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700168 dataWorkload[j] = makeDataWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700169 }
170
171 time::microseconds d = timedRun([&] {
172 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700173 for (size_t i = 0; i < N_WORKLOAD; ++i) {
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700174 cs.insert(*dataWorkload[j][i], false);
mzhang4eab72492015-02-25 11:16:09 -0600175 find(*interestWorkload[i]);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700176 }
177 }
178 });
Davide Pesaventoa997d292017-08-24 20:16:59 -0400179
180 std::cout << "insert-find(hit) " << (N_WORKLOAD * REPEAT) << ": " << d << std::endl;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700181}
182
Junxiao Shi9222f362019-04-16 15:15:23 +0000183// find(CanBePrefix) hit
184BOOST_FIXTURE_TEST_CASE(FindCanBePrefixHit, CsBenchmarkFixture)
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700185{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000186 constexpr size_t N_CHILDREN = 10;
187 constexpr size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN;
188 constexpr size_t REPEAT = 4;
189
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700190 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_INTERESTS);
191 for (auto&& interest : interestWorkload) {
Junxiao Shi9222f362019-04-16 15:15:23 +0000192 interest->setCanBePrefix(true);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700193 for (size_t j = 0; j < N_CHILDREN; ++j) {
194 Name name = interest->getName();
195 name.appendNumber(j);
Junxiao Shi9222f362019-04-16 15:15:23 +0000196 cs.insert(*makeData(name), false);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700197 }
198 }
199 BOOST_REQUIRE(cs.size() == N_INTERESTS * N_CHILDREN);
200
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700201 time::microseconds d = timedRun([&] {
202 for (size_t j = 0; j < REPEAT; ++j) {
203 for (const auto& interest : interestWorkload) {
mzhang4eab72492015-02-25 11:16:09 -0600204 find(*interest);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700205 }
206 }
207 });
Davide Pesaventoa997d292017-08-24 20:16:59 -0400208
Junxiao Shi9222f362019-04-16 15:15:23 +0000209 std::cout << "find(CanBePrefix-hit) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d << std::endl;
Davide Pesaventoa997d292017-08-24 20:16:59 -0400210}
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700211
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400212} // namespace nfd::tests