Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, 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 | |
Eric Newberry | 52ae329 | 2017-11-16 13:54:52 -0700 | [diff] [blame] | 26 | #include "benchmark-helpers.hpp" |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 27 | #include "table/cs.hpp" |
Eric Newberry | 52ae329 | 2017-11-16 13:54:52 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 29 | #include <iostream> |
| 30 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 31 | #ifdef NFD_HAVE_VALGRIND |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 32 | #include <valgrind/callgrind.h> |
| 33 | #endif |
| 34 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 35 | namespace nfd { |
| 36 | namespace tests { |
| 37 | |
Eric Newberry | 52ae329 | 2017-11-16 13:54:52 -0700 | [diff] [blame] | 38 | class CsBenchmarkFixture |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 39 | { |
| 40 | protected: |
| 41 | CsBenchmarkFixture() |
| 42 | { |
| 43 | #ifdef _DEBUG |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 44 | std::cerr << "Benchmark compiled in debug mode is unreliable, please compile in release mode.\n"; |
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 | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 53 | #ifdef NFD_HAVE_VALGRIND |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 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 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 61 | #ifdef NFD_HAVE_VALGRIND |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 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 | |
Eric Newberry | 52ae329 | 2017-11-16 13:54:52 -0700 | [diff] [blame] | 68 | static shared_ptr<Data> |
| 69 | makeData(const Name& name) |
| 70 | { |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 71 | auto data = std::make_shared<Data>(name); |
| 72 | data->setSignatureInfo(ndn::SignatureInfo(tlv::NullSignature)); |
| 73 | data->setSignatureValue(std::make_shared<ndn::Buffer>()); |
Eric Newberry | 52ae329 | 2017-11-16 13:54:52 -0700 | [diff] [blame] | 74 | data->wireEncode(); |
| 75 | return data; |
| 76 | } |
| 77 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 78 | void |
| 79 | find(const Interest& interest) |
| 80 | { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 81 | cs.find(interest, [] (auto&&...) {}, [] (auto&&...) {}); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 82 | } |
| 83 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 84 | protected: |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 85 | using NameGenerator = std::function<Name (size_t)>; |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 86 | |
| 87 | class SimpleNameGenerator |
| 88 | { |
| 89 | public: |
| 90 | explicit |
| 91 | SimpleNameGenerator(const Name& prefix = "/cs/benchmark") |
| 92 | : m_prefix(prefix) |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | Name |
| 97 | operator()(size_t i) const |
| 98 | { |
| 99 | Name name(m_prefix); |
| 100 | name.appendNumber(i % 4); |
| 101 | name.appendNumber(i); |
| 102 | return name; |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | Name m_prefix; |
| 107 | }; |
| 108 | |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 109 | static std::vector<shared_ptr<Interest>> |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 110 | makeInterestWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator()) |
| 111 | { |
| 112 | std::vector<shared_ptr<Interest>> workload(count); |
| 113 | for (size_t i = 0; i < count; ++i) { |
| 114 | Name name = genName(i); |
Davide Pesavento | b7e72c3 | 2020-10-02 20:00:03 -0400 | [diff] [blame] | 115 | auto interest = std::make_shared<Interest>(name); |
Junxiao Shi | 9222f36 | 2019-04-16 15:15:23 +0000 | [diff] [blame] | 116 | interest->setCanBePrefix(false); |
| 117 | workload[i] = interest; |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 118 | } |
| 119 | return workload; |
| 120 | } |
| 121 | |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 122 | static std::vector<shared_ptr<Data>> |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 123 | makeDataWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator()) |
| 124 | { |
| 125 | std::vector<shared_ptr<Data>> workload(count); |
| 126 | for (size_t i = 0; i < count; ++i) { |
| 127 | Name name = genName(i); |
| 128 | workload[i] = makeData(name); |
| 129 | } |
| 130 | return workload; |
| 131 | } |
| 132 | |
| 133 | protected: |
| 134 | Cs cs; |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 135 | static constexpr size_t CS_CAPACITY = 50000; |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 138 | // find miss, then insert |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 139 | BOOST_FIXTURE_TEST_CASE(FindMissInsert, CsBenchmarkFixture) |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 140 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 141 | constexpr size_t N_WORKLOAD = CS_CAPACITY * 2; |
| 142 | constexpr size_t REPEAT = 4; |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 143 | |
Junxiao Shi | 9222f36 | 2019-04-16 15:15:23 +0000 | [diff] [blame] | 144 | auto interestWorkload = makeInterestWorkload(N_WORKLOAD); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 145 | std::vector<shared_ptr<Data>> dataWorkload[REPEAT]; |
| 146 | for (size_t j = 0; j < REPEAT; ++j) { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 147 | dataWorkload[j] = makeDataWorkload(N_WORKLOAD); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | time::microseconds d = timedRun([&] { |
| 151 | for (size_t j = 0; j < REPEAT; ++j) { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 152 | for (size_t i = 0; i < N_WORKLOAD; ++i) { |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 153 | find(*interestWorkload[i]); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 154 | cs.insert(*dataWorkload[j][i], false); |
| 155 | } |
| 156 | } |
| 157 | }); |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 158 | |
| 159 | std::cout << "find(miss)-insert " << (N_WORKLOAD * REPEAT) << ": " << d << std::endl; |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // insert, then find hit |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 163 | BOOST_FIXTURE_TEST_CASE(InsertFindHit, CsBenchmarkFixture) |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 164 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 165 | constexpr size_t N_WORKLOAD = CS_CAPACITY * 2; |
| 166 | constexpr size_t REPEAT = 4; |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 167 | |
| 168 | std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 169 | std::vector<shared_ptr<Data>> dataWorkload[REPEAT]; |
| 170 | for (size_t j = 0; j < REPEAT; ++j) { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 171 | dataWorkload[j] = makeDataWorkload(N_WORKLOAD); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | time::microseconds d = timedRun([&] { |
| 175 | for (size_t j = 0; j < REPEAT; ++j) { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 176 | for (size_t i = 0; i < N_WORKLOAD; ++i) { |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 177 | cs.insert(*dataWorkload[j][i], false); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 178 | find(*interestWorkload[i]); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | }); |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 182 | |
| 183 | std::cout << "insert-find(hit) " << (N_WORKLOAD * REPEAT) << ": " << d << std::endl; |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Junxiao Shi | 9222f36 | 2019-04-16 15:15:23 +0000 | [diff] [blame] | 186 | // find(CanBePrefix) hit |
| 187 | BOOST_FIXTURE_TEST_CASE(FindCanBePrefixHit, CsBenchmarkFixture) |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 188 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 189 | constexpr size_t N_CHILDREN = 10; |
| 190 | constexpr size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN; |
| 191 | constexpr size_t REPEAT = 4; |
| 192 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 193 | std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_INTERESTS); |
| 194 | for (auto&& interest : interestWorkload) { |
Junxiao Shi | 9222f36 | 2019-04-16 15:15:23 +0000 | [diff] [blame] | 195 | interest->setCanBePrefix(true); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 196 | for (size_t j = 0; j < N_CHILDREN; ++j) { |
| 197 | Name name = interest->getName(); |
| 198 | name.appendNumber(j); |
Junxiao Shi | 9222f36 | 2019-04-16 15:15:23 +0000 | [diff] [blame] | 199 | cs.insert(*makeData(name), false); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | BOOST_REQUIRE(cs.size() == N_INTERESTS * N_CHILDREN); |
| 203 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 204 | time::microseconds d = timedRun([&] { |
| 205 | for (size_t j = 0; j < REPEAT; ++j) { |
| 206 | for (const auto& interest : interestWorkload) { |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 207 | find(*interest); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | }); |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 211 | |
Junxiao Shi | 9222f36 | 2019-04-16 15:15:23 +0000 | [diff] [blame] | 212 | std::cout << "find(CanBePrefix-hit) " << (N_INTERESTS * N_CHILDREN * REPEAT) << ": " << d << std::endl; |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 213 | } |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 214 | |
| 215 | } // namespace tests |
| 216 | } // namespace nfd |