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