Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 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" |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 27 | |
| 28 | #include "tests/test-common.hpp" |
| 29 | |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 30 | #ifdef HAVE_VALGRIND |
| 31 | #include <valgrind/callgrind.h> |
| 32 | #endif |
| 33 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 34 | namespace nfd { |
| 35 | namespace tests { |
| 36 | |
| 37 | class CsBenchmarkFixture : public BaseFixture |
| 38 | { |
| 39 | protected: |
| 40 | CsBenchmarkFixture() |
| 41 | { |
| 42 | #ifdef _DEBUG |
| 43 | BOOST_TEST_MESSAGE("Benchmark compiled in debug mode is unreliable, " |
| 44 | "please compile in release mode."); |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 45 | #endif |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 46 | |
| 47 | cs.setLimit(CS_CAPACITY); |
| 48 | } |
| 49 | |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 50 | static time::microseconds |
| 51 | timedRun(const std::function<void()>& f) |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 52 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 53 | #ifdef HAVE_VALGRIND |
| 54 | CALLGRIND_START_INSTRUMENTATION; |
| 55 | #endif |
| 56 | |
| 57 | auto t1 = time::steady_clock::now(); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 58 | f(); |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 59 | auto t2 = time::steady_clock::now(); |
| 60 | |
| 61 | #ifdef HAVE_VALGRIND |
| 62 | CALLGRIND_STOP_INSTRUMENTATION; |
| 63 | #endif |
| 64 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 65 | return time::duration_cast<time::microseconds>(t2 - t1); |
| 66 | } |
| 67 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 68 | void |
| 69 | find(const Interest& interest) |
| 70 | { |
| 71 | cs.find(interest, bind([]{}), bind([]{})); |
| 72 | } |
| 73 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 74 | protected: |
| 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 Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 99 | static std::vector<shared_ptr<Interest>> |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 100 | 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 Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 110 | static std::vector<shared_ptr<Data>> |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 111 | 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 | |
| 121 | protected: |
| 122 | Cs cs; |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 123 | static constexpr size_t CS_CAPACITY = 50000; |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | BOOST_FIXTURE_TEST_SUITE(TableCsBenchmark, CsBenchmarkFixture) |
| 127 | |
| 128 | // find miss, then insert |
| 129 | BOOST_AUTO_TEST_CASE(FindMissInsert) |
| 130 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 131 | constexpr size_t N_WORKLOAD = CS_CAPACITY * 2; |
| 132 | constexpr size_t REPEAT = 4; |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 133 | |
| 134 | std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 135 | std::vector<shared_ptr<Data>> dataWorkload[REPEAT]; |
| 136 | for (size_t j = 0; j < REPEAT; ++j) { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 137 | dataWorkload[j] = makeDataWorkload(N_WORKLOAD); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | time::microseconds d = timedRun([&] { |
| 141 | for (size_t j = 0; j < REPEAT; ++j) { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 142 | for (size_t i = 0; i < N_WORKLOAD; ++i) { |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 143 | find(*interestWorkload[i]); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 144 | cs.insert(*dataWorkload[j][i], false); |
| 145 | } |
| 146 | } |
| 147 | }); |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 148 | BOOST_TEST_MESSAGE("find(miss)-insert " << (N_WORKLOAD * REPEAT) << ": " << d); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // insert, then find hit |
| 152 | BOOST_AUTO_TEST_CASE(InsertFindHit) |
| 153 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 154 | constexpr size_t N_WORKLOAD = CS_CAPACITY * 2; |
| 155 | constexpr size_t REPEAT = 4; |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 156 | |
| 157 | std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 158 | std::vector<shared_ptr<Data>> dataWorkload[REPEAT]; |
| 159 | for (size_t j = 0; j < REPEAT; ++j) { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 160 | dataWorkload[j] = makeDataWorkload(N_WORKLOAD); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | time::microseconds d = timedRun([&] { |
| 164 | for (size_t j = 0; j < REPEAT; ++j) { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 165 | for (size_t i = 0; i < N_WORKLOAD; ++i) { |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 166 | cs.insert(*dataWorkload[j][i], false); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 167 | find(*interestWorkload[i]); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | }); |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 171 | BOOST_TEST_MESSAGE("insert-find(hit) " << (N_WORKLOAD * REPEAT) << ": " << d); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // find(leftmost) hit |
| 175 | BOOST_AUTO_TEST_CASE(Leftmost) |
| 176 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 177 | constexpr size_t N_CHILDREN = 10; |
| 178 | constexpr size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN; |
| 179 | constexpr size_t REPEAT = 4; |
| 180 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 181 | 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 Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 193 | time::microseconds d = timedRun([&] { |
| 194 | for (size_t j = 0; j < REPEAT; ++j) { |
| 195 | for (const auto& interest : interestWorkload) { |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 196 | find(*interest); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | }); |
| 200 | BOOST_TEST_MESSAGE("find(leftmost) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d); |
| 201 | } |
| 202 | |
| 203 | // find(rightmost) hit |
| 204 | BOOST_AUTO_TEST_CASE(Rightmost) |
| 205 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 206 | constexpr size_t N_CHILDREN = 10; |
| 207 | constexpr size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN; |
| 208 | constexpr size_t REPEAT = 4; |
| 209 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 210 | 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 Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 222 | time::microseconds d = timedRun([&] { |
| 223 | for (size_t j = 0; j < REPEAT; ++j) { |
| 224 | for (const auto& interest : interestWorkload) { |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 225 | find(*interest); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | }); |
| 229 | BOOST_TEST_MESSAGE("find(rightmost) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d); |
| 230 | } |
| 231 | |
| 232 | BOOST_AUTO_TEST_SUITE_END() |
| 233 | |
| 234 | } // namespace tests |
| 235 | } // namespace nfd |