blob: 78f7c1b0b1f273055c8337e45f457f8306108f3f [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
56protected:
57 typedef std::function<Name(size_t)> NameGenerator;
58
59 class SimpleNameGenerator
60 {
61 public:
62 explicit
63 SimpleNameGenerator(const Name& prefix = "/cs/benchmark")
64 : m_prefix(prefix)
65 {
66 }
67
68 Name
69 operator()(size_t i) const
70 {
71 Name name(m_prefix);
72 name.appendNumber(i % 4);
73 name.appendNumber(i);
74 return name;
75 }
76
77 private:
78 Name m_prefix;
79 };
80
81 std::vector<shared_ptr<Interest>>
82 makeInterestWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator())
83 {
84 std::vector<shared_ptr<Interest>> workload(count);
85 for (size_t i = 0; i < count; ++i) {
86 Name name = genName(i);
87 workload[i] = makeInterest(name);
88 }
89 return workload;
90 }
91
92 std::vector<shared_ptr<Data>>
93 makeDataWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator())
94 {
95 std::vector<shared_ptr<Data>> workload(count);
96 for (size_t i = 0; i < count; ++i) {
97 Name name = genName(i);
98 workload[i] = makeData(name);
99 }
100 return workload;
101 }
102
103protected:
104 Cs cs;
105 static const size_t CS_CAPACITY = 50000;
106};
107
108BOOST_FIXTURE_TEST_SUITE(TableCsBenchmark, CsBenchmarkFixture)
109
110// find miss, then insert
111BOOST_AUTO_TEST_CASE(FindMissInsert)
112{
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700113 const size_t N_WORKLOAD = CS_CAPACITY * 2;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700114 const size_t REPEAT = 4;
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700115
116 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700117 std::vector<shared_ptr<Data>> dataWorkload[REPEAT];
118 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700119 dataWorkload[j] = makeDataWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700120 }
121
122 time::microseconds d = timedRun([&] {
123 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700124 for (size_t i = 0; i < N_WORKLOAD; ++i) {
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700125 cs.find(*interestWorkload[i]);
126 cs.insert(*dataWorkload[j][i], false);
127 }
128 }
129 });
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700130 BOOST_TEST_MESSAGE("find(miss)-insert " << (N_WORKLOAD * REPEAT) << ": " << d);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700131}
132
133// insert, then find hit
134BOOST_AUTO_TEST_CASE(InsertFindHit)
135{
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700136 const size_t N_WORKLOAD = CS_CAPACITY * 2;
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700137 const size_t REPEAT = 4;
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700138
139 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700140 std::vector<shared_ptr<Data>> dataWorkload[REPEAT];
141 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700142 dataWorkload[j] = makeDataWorkload(N_WORKLOAD);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700143 }
144
145 time::microseconds d = timedRun([&] {
146 for (size_t j = 0; j < REPEAT; ++j) {
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700147 for (size_t i = 0; i < N_WORKLOAD; ++i) {
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700148 cs.insert(*dataWorkload[j][i], false);
149 cs.find(*interestWorkload[i]);
150 }
151 }
152 });
Junxiao Shi5af9bd32015-01-28 19:57:31 -0700153 BOOST_TEST_MESSAGE("insert-find(hit) " << (N_WORKLOAD * REPEAT) << ": " << d);
Junxiao Shi85d17fc2015-01-21 22:18:17 -0700154}
155
156// find(leftmost) hit
157BOOST_AUTO_TEST_CASE(Leftmost)
158{
159 const size_t N_CHILDREN = 10;
160 const size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN;
161 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_INTERESTS);
162 for (auto&& interest : interestWorkload) {
163 interest->setChildSelector(0);
164 for (size_t j = 0; j < N_CHILDREN; ++j) {
165 Name name = interest->getName();
166 name.appendNumber(j);
167 shared_ptr<Data> data = makeData(name);
168 cs.insert(*data, false);
169 }
170 }
171 BOOST_REQUIRE(cs.size() == N_INTERESTS * N_CHILDREN);
172
173 const size_t REPEAT = 4;
174
175 time::microseconds d = timedRun([&] {
176 for (size_t j = 0; j < REPEAT; ++j) {
177 for (const auto& interest : interestWorkload) {
178 cs.find(*interest);
179 }
180 }
181 });
182 BOOST_TEST_MESSAGE("find(leftmost) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d);
183}
184
185// find(rightmost) hit
186BOOST_AUTO_TEST_CASE(Rightmost)
187{
188 const size_t N_CHILDREN = 10;
189 const size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN;
190 std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_INTERESTS);
191 for (auto&& interest : interestWorkload) {
192 interest->setChildSelector(1);
193 for (size_t j = 0; j < N_CHILDREN; ++j) {
194 Name name = interest->getName();
195 name.appendNumber(j);
196 shared_ptr<Data> data = makeData(name);
197 cs.insert(*data, false);
198 }
199 }
200 BOOST_REQUIRE(cs.size() == N_INTERESTS * N_CHILDREN);
201
202 const size_t REPEAT = 4;
203
204 time::microseconds d = timedRun([&] {
205 for (size_t j = 0; j < REPEAT; ++j) {
206 for (const auto& interest : interestWorkload) {
207 cs.find(*interest);
208 }
209 }
210 });
211 BOOST_TEST_MESSAGE("find(rightmost) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d);
212}
213
214BOOST_AUTO_TEST_SUITE_END()
215
216} // namespace tests
217} // namespace nfd