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 | /* |
Junxiao Shi | 9222f36 | 2019-04-16 15:15:23 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | |
| 29 | #include <ndn-cxx/security/signature-sha256-with-rsa.hpp> |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 31 | #include <iostream> |
| 32 | |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 33 | #ifdef HAVE_VALGRIND |
| 34 | #include <valgrind/callgrind.h> |
| 35 | #endif |
| 36 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 37 | namespace nfd { |
| 38 | namespace tests { |
| 39 | |
Eric Newberry | 52ae329 | 2017-11-16 13:54:52 -0700 | [diff] [blame] | 40 | class CsBenchmarkFixture |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 41 | { |
| 42 | protected: |
| 43 | CsBenchmarkFixture() |
| 44 | { |
| 45 | #ifdef _DEBUG |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 46 | 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] | 47 | #endif |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 48 | |
| 49 | cs.setLimit(CS_CAPACITY); |
| 50 | } |
| 51 | |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 52 | static time::microseconds |
| 53 | timedRun(const std::function<void()>& f) |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 54 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 55 | #ifdef HAVE_VALGRIND |
| 56 | CALLGRIND_START_INSTRUMENTATION; |
| 57 | #endif |
| 58 | |
| 59 | auto t1 = time::steady_clock::now(); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 60 | f(); |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 61 | auto t2 = time::steady_clock::now(); |
| 62 | |
| 63 | #ifdef HAVE_VALGRIND |
| 64 | CALLGRIND_STOP_INSTRUMENTATION; |
| 65 | #endif |
| 66 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 67 | return time::duration_cast<time::microseconds>(t2 - t1); |
| 68 | } |
| 69 | |
Eric Newberry | 52ae329 | 2017-11-16 13:54:52 -0700 | [diff] [blame] | 70 | static shared_ptr<Data> |
| 71 | makeData(const Name& name) |
| 72 | { |
| 73 | auto data = make_shared<Data>(name); |
| 74 | ndn::SignatureSha256WithRsa fakeSignature; |
| 75 | fakeSignature.setValue(ndn::encoding::makeEmptyBlock(tlv::SignatureValue)); |
| 76 | data->setSignature(fakeSignature); |
| 77 | data->wireEncode(); |
| 78 | return data; |
| 79 | } |
| 80 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 81 | void |
| 82 | find(const Interest& interest) |
| 83 | { |
| 84 | cs.find(interest, bind([]{}), bind([]{})); |
| 85 | } |
| 86 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 87 | protected: |
| 88 | typedef std::function<Name(size_t)> NameGenerator; |
| 89 | |
| 90 | class SimpleNameGenerator |
| 91 | { |
| 92 | public: |
| 93 | explicit |
| 94 | SimpleNameGenerator(const Name& prefix = "/cs/benchmark") |
| 95 | : m_prefix(prefix) |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | Name |
| 100 | operator()(size_t i) const |
| 101 | { |
| 102 | Name name(m_prefix); |
| 103 | name.appendNumber(i % 4); |
| 104 | name.appendNumber(i); |
| 105 | return name; |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | Name m_prefix; |
| 110 | }; |
| 111 | |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 112 | static std::vector<shared_ptr<Interest>> |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 113 | makeInterestWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator()) |
| 114 | { |
| 115 | std::vector<shared_ptr<Interest>> workload(count); |
| 116 | for (size_t i = 0; i < count; ++i) { |
| 117 | Name name = genName(i); |
Junxiao Shi | 9222f36 | 2019-04-16 15:15:23 +0000 | [diff] [blame] | 118 | auto interest = make_shared<Interest>(name); |
| 119 | interest->setCanBePrefix(false); |
| 120 | workload[i] = interest; |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 121 | } |
| 122 | return workload; |
| 123 | } |
| 124 | |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 125 | static std::vector<shared_ptr<Data>> |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 126 | makeDataWorkload(size_t count, const NameGenerator& genName = SimpleNameGenerator()) |
| 127 | { |
| 128 | std::vector<shared_ptr<Data>> workload(count); |
| 129 | for (size_t i = 0; i < count; ++i) { |
| 130 | Name name = genName(i); |
| 131 | workload[i] = makeData(name); |
| 132 | } |
| 133 | return workload; |
| 134 | } |
| 135 | |
| 136 | protected: |
| 137 | Cs cs; |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 138 | static constexpr size_t CS_CAPACITY = 50000; |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 141 | // find miss, then insert |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 142 | BOOST_FIXTURE_TEST_CASE(FindMissInsert, CsBenchmarkFixture) |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 143 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 144 | constexpr size_t N_WORKLOAD = CS_CAPACITY * 2; |
| 145 | constexpr size_t REPEAT = 4; |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 146 | |
Junxiao Shi | 9222f36 | 2019-04-16 15:15:23 +0000 | [diff] [blame] | 147 | auto interestWorkload = makeInterestWorkload(N_WORKLOAD); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 148 | std::vector<shared_ptr<Data>> dataWorkload[REPEAT]; |
| 149 | for (size_t j = 0; j < REPEAT; ++j) { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 150 | dataWorkload[j] = makeDataWorkload(N_WORKLOAD); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | time::microseconds d = timedRun([&] { |
| 154 | for (size_t j = 0; j < REPEAT; ++j) { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 155 | for (size_t i = 0; i < N_WORKLOAD; ++i) { |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 156 | find(*interestWorkload[i]); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 157 | cs.insert(*dataWorkload[j][i], false); |
| 158 | } |
| 159 | } |
| 160 | }); |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 161 | |
| 162 | std::cout << "find(miss)-insert " << (N_WORKLOAD * REPEAT) << ": " << d << std::endl; |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // insert, then find hit |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 166 | BOOST_FIXTURE_TEST_CASE(InsertFindHit, CsBenchmarkFixture) |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 167 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 168 | constexpr size_t N_WORKLOAD = CS_CAPACITY * 2; |
| 169 | constexpr size_t REPEAT = 4; |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 170 | |
| 171 | std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_WORKLOAD); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 172 | std::vector<shared_ptr<Data>> dataWorkload[REPEAT]; |
| 173 | for (size_t j = 0; j < REPEAT; ++j) { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 174 | dataWorkload[j] = makeDataWorkload(N_WORKLOAD); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | time::microseconds d = timedRun([&] { |
| 178 | for (size_t j = 0; j < REPEAT; ++j) { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 179 | for (size_t i = 0; i < N_WORKLOAD; ++i) { |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 180 | cs.insert(*dataWorkload[j][i], false); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 181 | find(*interestWorkload[i]); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | }); |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 185 | |
| 186 | std::cout << "insert-find(hit) " << (N_WORKLOAD * REPEAT) << ": " << d << std::endl; |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Junxiao Shi | 9222f36 | 2019-04-16 15:15:23 +0000 | [diff] [blame] | 189 | // find(CanBePrefix) hit |
| 190 | BOOST_FIXTURE_TEST_CASE(FindCanBePrefixHit, CsBenchmarkFixture) |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 191 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 192 | constexpr size_t N_CHILDREN = 10; |
| 193 | constexpr size_t N_INTERESTS = CS_CAPACITY / N_CHILDREN; |
| 194 | constexpr size_t REPEAT = 4; |
| 195 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 196 | std::vector<shared_ptr<Interest>> interestWorkload = makeInterestWorkload(N_INTERESTS); |
| 197 | for (auto&& interest : interestWorkload) { |
Junxiao Shi | 9222f36 | 2019-04-16 15:15:23 +0000 | [diff] [blame] | 198 | interest->setCanBePrefix(true); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 199 | for (size_t j = 0; j < N_CHILDREN; ++j) { |
| 200 | Name name = interest->getName(); |
| 201 | name.appendNumber(j); |
Junxiao Shi | 9222f36 | 2019-04-16 15:15:23 +0000 | [diff] [blame] | 202 | cs.insert(*makeData(name), false); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | BOOST_REQUIRE(cs.size() == N_INTERESTS * N_CHILDREN); |
| 206 | |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 207 | time::microseconds d = timedRun([&] { |
| 208 | for (size_t j = 0; j < REPEAT; ++j) { |
| 209 | for (const auto& interest : interestWorkload) { |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 210 | find(*interest); |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | }); |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 214 | |
Junxiao Shi | 9222f36 | 2019-04-16 15:15:23 +0000 | [diff] [blame] | 215 | 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] | 216 | } |
Junxiao Shi | 85d17fc | 2015-01-21 22:18:17 -0700 | [diff] [blame] | 217 | |
| 218 | } // namespace tests |
| 219 | } // namespace nfd |