blob: c28921b1f98f008b8f66d98f467026b5de111602 [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
26#include "table/cs.hpp"
Junxiao Shi85d17fc2015-01-21 22:18:17 -070027#include "tests/test-common.hpp"
28
Davide Pesaventoa997d292017-08-24 20:16:59 -040029#include <iostream>
30
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000031#ifdef HAVE_VALGRIND
32#include <valgrind/callgrind.h>
33#endif
34
Junxiao Shi85d17fc2015-01-21 22:18:17 -070035namespace nfd {
36namespace tests {
37
38class CsBenchmarkFixture : public BaseFixture
39{
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 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
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700126// find miss, then insert
Davide Pesaventoa997d292017-08-24 20:16:59 -0400127BOOST_FIXTURE_TEST_CASE(FindMissInsert, CsBenchmarkFixture)
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700128{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000129 constexpr size_t N_WORKLOAD = CS_CAPACITY * 2;
130 constexpr size_t REPEAT = 4;
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700131
132 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700133 std::vector<shared_ptr<Data>> dataWorkload[REPEAT];
134 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700135 dataWorkload[j] = makeDataWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700136 }
137
138 time::microseconds d = timedRun([&] {
139 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700140 for (size_t i = 0; i < N_WORKLOAD; ++i) {
mzhang4eab72492015-02-25 11:16:09 -0600141 find(*interestWorkload[i]);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700142 cs.insert(*dataWorkload[j][i], false);
143 }
144 }
145 });
Davide Pesaventoa997d292017-08-24 20:16:59 -0400146
147 std::cout << "find(miss)-insert " << (N_WORKLOAD * REPEAT) << ": " << d << std::endl;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700148}
149
150// insert, then find hit
Davide Pesaventoa997d292017-08-24 20:16:59 -0400151BOOST_FIXTURE_TEST_CASE(InsertFindHit, CsBenchmarkFixture)
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700152{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000153 constexpr size_t N_WORKLOAD = CS_CAPACITY * 2;
154 constexpr size_t REPEAT = 4;
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700155
156 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700157 std::vector<shared_ptr<Data>> dataWorkload[REPEAT];
158 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700159 dataWorkload[j] = makeDataWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700160 }
161
162 time::microseconds d = timedRun([&] {
163 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700164 for (size_t i = 0; i < N_WORKLOAD; ++i) {
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700165 cs.insert(*dataWorkload[j][i], false);
mzhang4eab72492015-02-25 11:16:09 -0600166 find(*interestWorkload[i]);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700167 }
168 }
169 });
Davide Pesaventoa997d292017-08-24 20:16:59 -0400170
171 std::cout << "insert-find(hit) " << (N_WORKLOAD * REPEAT) << ": " << d << std::endl;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700172}
173
174// find(leftmost) hit
Davide Pesaventoa997d292017-08-24 20:16:59 -0400175BOOST_FIXTURE_TEST_CASE(Leftmost, CsBenchmarkFixture)
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700176{
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 });
Davide Pesaventoa997d292017-08-24 20:16:59 -0400200
201 std::cout << "find(leftmost) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d << std::endl;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700202}
203
204// find(rightmost) hit
Davide Pesaventoa997d292017-08-24 20:16:59 -0400205BOOST_FIXTURE_TEST_CASE(Rightmost, CsBenchmarkFixture)
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700206{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000207 constexpr size_t N_CHILDREN = 10;
208 constexpr size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN;
209 constexpr size_t REPEAT = 4;
210
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700211 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_INTERESTS);
212 for (auto&& interest : interestWorkload) {
213 interest->setChildSelector(1);
214 for (size_t j = 0; j < N_CHILDREN; ++j) {
215 Name name = interest->getName();
216 name.appendNumber(j);
217 shared_ptr<Data> data = makeData(name);
218 cs.insert(*data, false);
219 }
220 }
221 BOOST_REQUIRE(cs.size() == N_INTERESTS * N_CHILDREN);
222
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700223 time::microseconds d = timedRun([&] {
224 for (size_t j = 0; j < REPEAT; ++j) {
225 for (const auto& interest : interestWorkload) {
mzhang4eab72492015-02-25 11:16:09 -0600226 find(*interest);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700227 }
228 }
229 });
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700230
Davide Pesaventoa997d292017-08-24 20:16:59 -0400231 std::cout << "find(rightmost) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d << std::endl;
232}
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700233
234} // namespace tests
235} // namespace nfd