blob: fcd9d20aa0ca4c1c5145350114d66d9edf57f6c6 [file] [log] [blame]
Junxiao Shi85d17fc2015-01-21 22:18:17 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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"
27#include <ndn-cxx/security/key-chain.hpp>
28
29#include "tests/test-common.hpp"
30
31namespace nfd {
32namespace tests {
33
34class CsBenchmarkFixture : public BaseFixture
35{
36protected:
37 CsBenchmarkFixture()
38 {
39#ifdef _DEBUG
40 BOOST_TEST_MESSAGE("Benchmark compiled in debug mode is unreliable, "
41 "please compile in release mode.");
42#endif // _DEBUG
43
44 cs.setLimit(CS_CAPACITY);
45 }
46
47 time::microseconds
48 timedRun(std::function<void()> f)
49 {
50 time::steady_clock::TimePoint t1 = time::steady_clock::now();
51 f();
52 time::steady_clock::TimePoint t2 = time::steady_clock::now();
53 return time::duration_cast<time::microseconds>(t2 - t1);
54 }
55
mzhang4eab72492015-02-25 11:16:09 -060056 void
57 find(const Interest& interest)
58 {
59 cs.find(interest, bind([]{}), bind([]{}));
60 }
61
Junxiao Shi85d17fc2015-01-21 22:18:17 -070062protected:
63 typedef std::function<Name(size_t)> NameGenerator;
64
65 class SimpleNameGenerator
66 {
67 public:
68 explicit
69 SimpleNameGenerator(const Name& prefix = "/cs/benchmark")
70 : m_prefix(prefix)
71 {
72 }
73
74 Name
75 operator()(size_t i) const
76 {
77 Name name(m_prefix);
78 name.appendNumber(i % 4);
79 name.appendNumber(i);
80 return name;
81 }
82
83 private:
84 Name m_prefix;
85 };
86
87 std::vector<shared_ptr<Interest>>
88 makeInterestWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator())
89 {
90 std::vector<shared_ptr<Interest>> workload(count);
91 for (size_t i = 0; i < count; ++i) {
92 Name name = genName(i);
93 workload[i] = makeInterest(name);
94 }
95 return workload;
96 }
97
98 std::vector<shared_ptr<Data>>
99 makeDataWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator())
100 {
101 std::vector<shared_ptr<Data>> workload(count);
102 for (size_t i = 0; i < count; ++i) {
103 Name name = genName(i);
104 workload[i] = makeData(name);
105 }
106 return workload;
107 }
108
109protected:
110 Cs cs;
111 static const size_t CS_CAPACITY = 50000;
112};
113
114BOOST_FIXTURE_TEST_SUITE(TableCsBenchmark, CsBenchmarkFixture)
115
116// find miss, then insert
117BOOST_AUTO_TEST_CASE(FindMissInsert)
118{
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700119 const size_t N_WORKLOAD = CS_CAPACITY * 2;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700120 const size_t REPEAT = 4;
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700121
122 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700123 std::vector<shared_ptr<Data>> dataWorkload[REPEAT];
124 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700125 dataWorkload[j] = makeDataWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700126 }
127
128 time::microseconds d = timedRun([&] {
129 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700130 for (size_t i = 0; i < N_WORKLOAD; ++i) {
mzhang4eab72492015-02-25 11:16:09 -0600131 find(*interestWorkload[i]);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700132 cs.insert(*dataWorkload[j][i], false);
133 }
134 }
135 });
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700136 BOOST_TEST_MESSAGE("find(miss)-insert " << (N_WORKLOAD * REPEAT) << ": " << d);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700137}
138
139// insert, then find hit
140BOOST_AUTO_TEST_CASE(InsertFindHit)
141{
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700142 const size_t N_WORKLOAD = CS_CAPACITY * 2;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700143 const 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) {
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700154 cs.insert(*dataWorkload[j][i], false);
mzhang4eab72492015-02-25 11:16:09 -0600155 find(*interestWorkload[i]);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700156 }
157 }
158 });
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700159 BOOST_TEST_MESSAGE("insert-find(hit) " << (N_WORKLOAD * REPEAT) << ": " << d);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700160}
161
162// find(leftmost) hit
163BOOST_AUTO_TEST_CASE(Leftmost)
164{
165 const size_t N_CHILDREN = 10;
166 const size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN;
167 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_INTERESTS);
168 for (auto&& interest : interestWorkload) {
169 interest->setChildSelector(0);
170 for (size_t j = 0; j < N_CHILDREN; ++j) {
171 Name name = interest->getName();
172 name.appendNumber(j);
173 shared_ptr<Data> data = makeData(name);
174 cs.insert(*data, false);
175 }
176 }
177 BOOST_REQUIRE(cs.size() == N_INTERESTS * N_CHILDREN);
178
179 const size_t REPEAT = 4;
180
181 time::microseconds d = timedRun([&] {
182 for (size_t j = 0; j < REPEAT; ++j) {
183 for (const auto& interest : interestWorkload) {
mzhang4eab72492015-02-25 11:16:09 -0600184 find(*interest);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700185 }
186 }
187 });
188 BOOST_TEST_MESSAGE("find(leftmost) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d);
189}
190
191// find(rightmost) hit
192BOOST_AUTO_TEST_CASE(Rightmost)
193{
194 const size_t N_CHILDREN = 10;
195 const size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN;
196 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_INTERESTS);
197 for (auto&& interest : interestWorkload) {
198 interest->setChildSelector(1);
199 for (size_t j = 0; j < N_CHILDREN; ++j) {
200 Name name = interest->getName();
201 name.appendNumber(j);
202 shared_ptr<Data> data = makeData(name);
203 cs.insert(*data, false);
204 }
205 }
206 BOOST_REQUIRE(cs.size() == N_INTERESTS * N_CHILDREN);
207
208 const size_t REPEAT = 4;
209
210 time::microseconds d = timedRun([&] {
211 for (size_t j = 0; j < REPEAT; ++j) {
212 for (const auto& interest : interestWorkload) {
mzhang4eab72492015-02-25 11:16:09 -0600213 find(*interest);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700214 }
215 }
216 });
217 BOOST_TEST_MESSAGE("find(rightmost) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d);
218}
219
220BOOST_AUTO_TEST_SUITE_END()
221
222} // namespace tests
223} // namespace nfd