blob: e9226efc8e97f4896b059eaaab9408c6710fd84e [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/*
3 * Copyright (c) 2014-2017, 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
29#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
Junxiao Shi85d17fc2015-01-21 22:18:17 -070030
Davide Pesaventoa997d292017-08-24 20:16:59 -040031#include <iostream>
32
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000033#ifdef HAVE_VALGRIND
34#include <valgrind/callgrind.h>
35#endif
36
Junxiao Shi85d17fc2015-01-21 22:18:17 -070037namespace nfd {
38namespace tests {
39
Eric Newberry52ae3292017-11-16 13:54:52 -070040class CsBenchmarkFixture
Junxiao Shi85d17fc2015-01-21 22:18:17 -070041{
42protected:
43 CsBenchmarkFixture()
44 {
45#ifdef _DEBUG
Davide Pesaventoa997d292017-08-24 20:16:59 -040046 std::cerr << "Benchmark compiled in debug mode is unreliable, please compile in release mode.\n";
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000047#endif
Junxiao Shi85d17fc2015-01-21 22:18:17 -070048
49 cs.setLimit(CS_CAPACITY);
50 }
51
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000052 static time::microseconds
53 timedRun(const std::function<void()>& f)
Junxiao Shi85d17fc2015-01-21 22:18:17 -070054 {
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000055#ifdef HAVE_VALGRIND
56 CALLGRIND_START_INSTRUMENTATION;
57#endif
58
59 auto t1 = time::steady_clock::now();
Junxiao Shi85d17fc2015-01-21 22:18:17 -070060 f();
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000061 auto t2 = time::steady_clock::now();
62
63#ifdef HAVE_VALGRIND
64 CALLGRIND_STOP_INSTRUMENTATION;
65#endif
66
Junxiao Shi85d17fc2015-01-21 22:18:17 -070067 return time::duration_cast<time::microseconds>(t2 - t1);
68 }
69
Eric Newberry52ae3292017-11-16 13:54:52 -070070 static shared_ptr<Data>
71 makeData(const Name& name)
72 {
73 auto data = make_shared<Data>(name);
74 ndn::SignatureSha256WithRsa fakeSignature;
75 fakeSignature.setValue(ndn::encoding::makeEmptyBlock(tlv::SignatureValue));
76 data->setSignature(fakeSignature);
77 data->wireEncode();
78 return data;
79 }
80
mzhang4eab72492015-02-25 11:16:09 -060081 void
82 find(const Interest& interest)
83 {
84 cs.find(interest, bind([]{}), bind([]{}));
85 }
86
Junxiao Shi85d17fc2015-01-21 22:18:17 -070087protected:
88 typedef std::function<Name(size_t)> NameGenerator;
89
90 class SimpleNameGenerator
91 {
92 public:
93 explicit
94 SimpleNameGenerator(const Name& prefix = "/cs/benchmark")
95 : m_prefix(prefix)
96 {
97 }
98
99 Name
100 operator()(size_t i) const
101 {
102 Name name(m_prefix);
103 name.appendNumber(i % 4);
104 name.appendNumber(i);
105 return name;
106 }
107
108 private:
109 Name m_prefix;
110 };
111
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000112 static std::vector<shared_ptr<Interest>>
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700113 makeInterestWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator())
114 {
115 std::vector<shared_ptr<Interest>> workload(count);
116 for (size_t i = 0; i < count; ++i) {
117 Name name = genName(i);
Eric Newberry52ae3292017-11-16 13:54:52 -0700118 workload[i] = make_shared<Interest>(name);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700119 }
120 return workload;
121 }
122
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000123 static std::vector<shared_ptr<Data>>
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700124 makeDataWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator())
125 {
126 std::vector<shared_ptr<Data>> workload(count);
127 for (size_t i = 0; i < count; ++i) {
128 Name name = genName(i);
129 workload[i] = makeData(name);
130 }
131 return workload;
132 }
133
134protected:
135 Cs cs;
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000136 static constexpr size_t CS_CAPACITY = 50000;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700137};
138
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700139// find miss, then insert
Davide Pesaventoa997d292017-08-24 20:16:59 -0400140BOOST_FIXTURE_TEST_CASE(FindMissInsert, CsBenchmarkFixture)
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700141{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000142 constexpr size_t N_WORKLOAD = CS_CAPACITY * 2;
143 constexpr size_t REPEAT = 4;
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700144
145 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700146 std::vector<shared_ptr<Data>> dataWorkload[REPEAT];
147 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700148 dataWorkload[j] = makeDataWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700149 }
150
151 time::microseconds d = timedRun([&] {
152 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700153 for (size_t i = 0; i < N_WORKLOAD; ++i) {
mzhang4eab72492015-02-25 11:16:09 -0600154 find(*interestWorkload[i]);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700155 cs.insert(*dataWorkload[j][i], false);
156 }
157 }
158 });
Davide Pesaventoa997d292017-08-24 20:16:59 -0400159
160 std::cout << "find(miss)-insert " << (N_WORKLOAD * REPEAT) << ": " << d << std::endl;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700161}
162
163// insert, then find hit
Davide Pesaventoa997d292017-08-24 20:16:59 -0400164BOOST_FIXTURE_TEST_CASE(InsertFindHit, CsBenchmarkFixture)
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700165{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000166 constexpr size_t N_WORKLOAD = CS_CAPACITY * 2;
167 constexpr size_t REPEAT = 4;
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700168
169 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700170 std::vector<shared_ptr<Data>> dataWorkload[REPEAT];
171 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700172 dataWorkload[j] = makeDataWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700173 }
174
175 time::microseconds d = timedRun([&] {
176 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700177 for (size_t i = 0; i < N_WORKLOAD; ++i) {
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700178 cs.insert(*dataWorkload[j][i], false);
mzhang4eab72492015-02-25 11:16:09 -0600179 find(*interestWorkload[i]);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700180 }
181 }
182 });
Davide Pesaventoa997d292017-08-24 20:16:59 -0400183
184 std::cout << "insert-find(hit) " << (N_WORKLOAD * REPEAT) << ": " << d << std::endl;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700185}
186
187// find(leftmost) hit
Davide Pesaventoa997d292017-08-24 20:16:59 -0400188BOOST_FIXTURE_TEST_CASE(Leftmost, CsBenchmarkFixture)
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700189{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000190 constexpr size_t N_CHILDREN = 10;
191 constexpr size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN;
192 constexpr size_t REPEAT = 4;
193
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700194 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_INTERESTS);
195 for (auto&& interest : interestWorkload) {
196 interest->setChildSelector(0);
197 for (size_t j = 0; j < N_CHILDREN; ++j) {
198 Name name = interest->getName();
199 name.appendNumber(j);
200 shared_ptr<Data> data = makeData(name);
201 cs.insert(*data, false);
202 }
203 }
204 BOOST_REQUIRE(cs.size() == N_INTERESTS * N_CHILDREN);
205
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700206 time::microseconds d = timedRun([&] {
207 for (size_t j = 0; j < REPEAT; ++j) {
208 for (const auto& interest : interestWorkload) {
mzhang4eab72492015-02-25 11:16:09 -0600209 find(*interest);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700210 }
211 }
212 });
Davide Pesaventoa997d292017-08-24 20:16:59 -0400213
214 std::cout << "find(leftmost) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d << std::endl;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700215}
216
217// find(rightmost) hit
Davide Pesaventoa997d292017-08-24 20:16:59 -0400218BOOST_FIXTURE_TEST_CASE(Rightmost, CsBenchmarkFixture)
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700219{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000220 constexpr size_t N_CHILDREN = 10;
221 constexpr size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN;
222 constexpr size_t REPEAT = 4;
223
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700224 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_INTERESTS);
225 for (auto&& interest : interestWorkload) {
226 interest->setChildSelector(1);
227 for (size_t j = 0; j < N_CHILDREN; ++j) {
228 Name name = interest->getName();
229 name.appendNumber(j);
230 shared_ptr<Data> data = makeData(name);
231 cs.insert(*data, false);
232 }
233 }
234 BOOST_REQUIRE(cs.size() == N_INTERESTS * N_CHILDREN);
235
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700236 time::microseconds d = timedRun([&] {
237 for (size_t j = 0; j < REPEAT; ++j) {
238 for (const auto& interest : interestWorkload) {
mzhang4eab72492015-02-25 11:16:09 -0600239 find(*interest);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700240 }
241 }
242 });
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700243
Davide Pesaventoa997d292017-08-24 20:16:59 -0400244 std::cout << "find(rightmost) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d << std::endl;
245}
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700246
247} // namespace tests
248} // namespace nfd