Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [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 | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [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" |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 27 | #include "table/fib.hpp" |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 28 | #include "table/pit.hpp" |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 29 | |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 30 | #include <iostream> |
| 31 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 32 | #ifdef NFD_HAVE_VALGRIND |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 33 | #include <valgrind/callgrind.h> |
| 34 | #endif |
| 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::tests { |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 37 | |
Eric Newberry | 52ae329 | 2017-11-16 13:54:52 -0700 | [diff] [blame] | 38 | class PitFibBenchmarkFixture |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 39 | { |
| 40 | protected: |
| 41 | PitFibBenchmarkFixture() |
| 42 | : m_fib(m_nameTree) |
| 43 | , m_pit(m_nameTree) |
| 44 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 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 |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
| 51 | generatePacketsAndPopulateFib(size_t nPackets, |
| 52 | size_t nFibEntries, |
| 53 | size_t fibPrefixLength, |
| 54 | size_t interestNameLength, |
| 55 | size_t dataNameLength) |
| 56 | { |
| 57 | BOOST_ASSERT(1 <= fibPrefixLength); |
| 58 | BOOST_ASSERT(fibPrefixLength <= interestNameLength); |
| 59 | BOOST_ASSERT(interestNameLength <= dataNameLength); |
| 60 | |
| 61 | for (size_t i = 0; i < nPackets; i++) { |
| 62 | Name prefix(to_string(i / nFibEntries)); |
Junxiao Shi | f9f7cf3 | 2016-08-08 05:51:06 +0000 | [diff] [blame] | 63 | extendName(prefix, fibPrefixLength); |
| 64 | m_fib.insert(prefix); |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 65 | |
| 66 | Name interestName = prefix; |
| 67 | if (nPackets > nFibEntries) { |
| 68 | interestName.append(to_string(i)); |
| 69 | } |
| 70 | extendName(interestName, interestNameLength); |
| 71 | interests.push_back(make_shared<Interest>(interestName)); |
| 72 | |
| 73 | Name dataName = interestName; |
| 74 | extendName(dataName, dataNameLength); |
| 75 | data.push_back(make_shared<Data>(dataName)); |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | static void |
| 81 | extendName(Name& name, size_t length) |
| 82 | { |
| 83 | BOOST_ASSERT(length >= name.size()); |
| 84 | for (size_t i = name.size(); i < length; i++) { |
| 85 | name.append("dup"); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | protected: |
| 90 | std::vector<shared_ptr<Interest>> interests; |
| 91 | std::vector<shared_ptr<Data>> data; |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 92 | |
| 93 | protected: |
| 94 | NameTree m_nameTree; |
| 95 | Fib m_fib; |
| 96 | Pit m_pit; |
| 97 | }; |
| 98 | |
| 99 | // This test case models PIT and FIB operations with simple Interest-Data exchanges. |
| 100 | // A total of nRoundTrip Interests are received and forwarded, and the same number of Data are returned. |
| 101 | BOOST_FIXTURE_TEST_CASE(SimpleExchanges, PitFibBenchmarkFixture) |
| 102 | { |
| 103 | // number of Interest-Data exchanges |
| 104 | const size_t nRoundTrip = 1000000; |
| 105 | // number of iterations between processing incoming Interest and processing incoming Data |
Davide Pesavento | 5f59607 | 2019-05-11 16:52:05 -0400 | [diff] [blame] | 106 | const size_t replyGap = 20000; |
| 107 | // total amount of FIB entries |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 108 | // packet names are homogeneously extended from these FIB entries |
| 109 | const size_t nFibEntries = 2000; |
| 110 | // length of fibPrefix, must be >= 1 |
| 111 | const size_t fibPrefixLength = 1; |
| 112 | // length of Interest Name >= fibPrefixLength |
| 113 | const size_t interestNameLength= 2; |
| 114 | // length of Data Name >= Interest Name |
| 115 | const size_t dataNameLength = 3; |
| 116 | |
| 117 | generatePacketsAndPopulateFib(nRoundTrip, nFibEntries, fibPrefixLength, |
| 118 | interestNameLength, dataNameLength); |
| 119 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 120 | #ifdef NFD_HAVE_VALGRIND |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 121 | CALLGRIND_START_INSTRUMENTATION; |
| 122 | #endif |
| 123 | |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 124 | auto t1 = time::steady_clock::now(); |
| 125 | |
Davide Pesavento | 5f59607 | 2019-05-11 16:52:05 -0400 | [diff] [blame] | 126 | for (size_t i = 0; i < nRoundTrip + replyGap; ++i) { |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 127 | if (i < nRoundTrip) { |
| 128 | // process incoming Interest |
Davide Pesavento | 5f59607 | 2019-05-11 16:52:05 -0400 | [diff] [blame] | 129 | auto pitEntry = m_pit.insert(*interests[i]).first; |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 130 | m_fib.findLongestPrefixMatch(*pitEntry); |
| 131 | } |
Davide Pesavento | 5f59607 | 2019-05-11 16:52:05 -0400 | [diff] [blame] | 132 | if (i >= replyGap) { |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 133 | // process incoming Data |
Davide Pesavento | 5f59607 | 2019-05-11 16:52:05 -0400 | [diff] [blame] | 134 | auto matches = m_pit.findAllDataMatches(*data[i - replyGap]); |
| 135 | // delete matching PIT entries |
| 136 | for (const auto& pitEntry : matches) { |
| 137 | m_pit.erase(pitEntry.get()); |
| 138 | } |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | auto t2 = time::steady_clock::now(); |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 143 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 144 | #ifdef NFD_HAVE_VALGRIND |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 145 | CALLGRIND_STOP_INSTRUMENTATION; |
| 146 | #endif |
| 147 | |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 148 | std::cout << time::duration_cast<time::microseconds>(t2 - t1) << std::endl; |
Yumin Xia | d72f092 | 2016-03-30 22:19:14 +0800 | [diff] [blame] | 149 | } |
| 150 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 151 | } // namespace nfd::tests |