blob: 3e7a5a2126a96302c8aa050b7de0d0b63bfdb28c [file] [log] [blame]
Junxiao Shi85d17fc2015-01-21 22:18:17 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +00003 * Copyright (c) 2014-2016, 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
26#include "table/cs.hpp"
Junxiao Shi85d17fc2015-01-21 22:18:17 -070027
28#include "tests/test-common.hpp"
29
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000030#ifdef HAVE_VALGRIND
31#include <valgrind/callgrind.h>
32#endif
33
Junxiao Shi85d17fc2015-01-21 22:18:17 -070034namespace nfd {
35namespace tests {
36
37class CsBenchmarkFixture : public BaseFixture
38{
39protected:
40 CsBenchmarkFixture()
41 {
42#ifdef _DEBUG
43 BOOST_TEST_MESSAGE("Benchmark compiled in debug mode is unreliable, "
44 "please compile in release mode.");
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 Pesaventoaaa5dd32016-09-02 12:33:33 +000053#ifdef HAVE_VALGRIND
54 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
61#ifdef HAVE_VALGRIND
62 CALLGRIND_STOP_INSTRUMENTATION;
63#endif
64
Junxiao Shi85d17fc2015-01-21 22:18:17 -070065 return time::duration_cast<time::microseconds>(t2 - t1);
66 }
67
mzhang4eab72492015-02-25 11:16:09 -060068 void
69 find(const Interest& interest)
70 {
71 cs.find(interest, bind([]{}), bind([]{}));
72 }
73
Junxiao Shi85d17fc2015-01-21 22:18:17 -070074protected:
75 typedef std::function<Name(size_t)> NameGenerator;
76
77 class SimpleNameGenerator
78 {
79 public:
80 explicit
81 SimpleNameGenerator(const Name& prefix = "/cs/benchmark")
82 : m_prefix(prefix)
83 {
84 }
85
86 Name
87 operator()(size_t i) const
88 {
89 Name name(m_prefix);
90 name.appendNumber(i % 4);
91 name.appendNumber(i);
92 return name;
93 }
94
95 private:
96 Name m_prefix;
97 };
98
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000099 static std::vector<shared_ptr<Interest>>
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700100 makeInterestWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator())
101 {
102 std::vector<shared_ptr<Interest>> workload(count);
103 for (size_t i = 0; i < count; ++i) {
104 Name name = genName(i);
105 workload[i] = makeInterest(name);
106 }
107 return workload;
108 }
109
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000110 static std::vector<shared_ptr<Data>>
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700111 makeDataWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator())
112 {
113 std::vector<shared_ptr<Data>> workload(count);
114 for (size_t i = 0; i < count; ++i) {
115 Name name = genName(i);
116 workload[i] = makeData(name);
117 }
118 return workload;
119 }
120
121protected:
122 Cs cs;
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000123 static constexpr size_t CS_CAPACITY = 50000;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700124};
125
126BOOST_FIXTURE_TEST_SUITE(TableCsBenchmark, CsBenchmarkFixture)
127
128// find miss, then insert
129BOOST_AUTO_TEST_CASE(FindMissInsert)
130{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000131 constexpr size_t N_WORKLOAD = CS_CAPACITY * 2;
132 constexpr size_t REPEAT = 4;
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700133
134 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700135 std::vector<shared_ptr<Data>> dataWorkload[REPEAT];
136 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700137 dataWorkload[j] = makeDataWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700138 }
139
140 time::microseconds d = timedRun([&] {
141 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700142 for (size_t i = 0; i < N_WORKLOAD; ++i) {
mzhang4eab72492015-02-25 11:16:09 -0600143 find(*interestWorkload[i]);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700144 cs.insert(*dataWorkload[j][i], false);
145 }
146 }
147 });
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700148 BOOST_TEST_MESSAGE("find(miss)-insert " << (N_WORKLOAD * REPEAT) << ": " << d);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700149}
150
151// insert, then find hit
152BOOST_AUTO_TEST_CASE(InsertFindHit)
153{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000154 constexpr size_t N_WORKLOAD = CS_CAPACITY * 2;
155 constexpr size_t REPEAT = 4;
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700156
157 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700158 std::vector<shared_ptr<Data>> dataWorkload[REPEAT];
159 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700160 dataWorkload[j] = makeDataWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700161 }
162
163 time::microseconds d = timedRun([&] {
164 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700165 for (size_t i = 0; i < N_WORKLOAD; ++i) {
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700166 cs.insert(*dataWorkload[j][i], false);
mzhang4eab72492015-02-25 11:16:09 -0600167 find(*interestWorkload[i]);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700168 }
169 }
170 });
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700171 BOOST_TEST_MESSAGE("insert-find(hit) " << (N_WORKLOAD * REPEAT) << ": " << d);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700172}
173
174// find(leftmost) hit
175BOOST_AUTO_TEST_CASE(Leftmost)
176{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000177 constexpr size_t N_CHILDREN = 10;
178 constexpr size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN;
179 constexpr size_t REPEAT = 4;
180
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700181 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_INTERESTS);
182 for (auto&& interest : interestWorkload) {
183 interest->setChildSelector(0);
184 for (size_t j = 0; j < N_CHILDREN; ++j) {
185 Name name = interest->getName();
186 name.appendNumber(j);
187 shared_ptr<Data> data = makeData(name);
188 cs.insert(*data, false);
189 }
190 }
191 BOOST_REQUIRE(cs.size() == N_INTERESTS * N_CHILDREN);
192
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700193 time::microseconds d = timedRun([&] {
194 for (size_t j = 0; j < REPEAT; ++j) {
195 for (const auto& interest : interestWorkload) {
mzhang4eab72492015-02-25 11:16:09 -0600196 find(*interest);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700197 }
198 }
199 });
200 BOOST_TEST_MESSAGE("find(leftmost) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d);
201}
202
203// find(rightmost) hit
204BOOST_AUTO_TEST_CASE(Rightmost)
205{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000206 constexpr size_t N_CHILDREN = 10;
207 constexpr size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN;
208 constexpr size_t REPEAT = 4;
209
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700210 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_INTERESTS);
211 for (auto&& interest : interestWorkload) {
212 interest->setChildSelector(1);
213 for (size_t j = 0; j < N_CHILDREN; ++j) {
214 Name name = interest->getName();
215 name.appendNumber(j);
216 shared_ptr<Data> data = makeData(name);
217 cs.insert(*data, false);
218 }
219 }
220 BOOST_REQUIRE(cs.size() == N_INTERESTS * N_CHILDREN);
221
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700222 time::microseconds d = timedRun([&] {
223 for (size_t j = 0; j < REPEAT; ++j) {
224 for (const auto& interest : interestWorkload) {
mzhang4eab72492015-02-25 11:16:09 -0600225 find(*interest);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700226 }
227 }
228 });
229 BOOST_TEST_MESSAGE("find(rightmost) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d);
230}
231
232BOOST_AUTO_TEST_SUITE_END()
233
234} // namespace tests
235} // namespace nfd