blob: c6888d1a9730ca1c3692135a8dcd164ac9cbefa2 [file] [log] [blame]
Yumin Xiad72f0922016-03-30 22:19:14 +08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa997d292017-08-24 20:16:59 -04002/*
Davide Pesavento264af772021-02-09 21:48:24 -05003 * Copyright (c) 2014-2021, Regents of the University of California,
Yumin Xiad72f0922016-03-30 22:19:14 +08004 * 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 Newberry52ae3292017-11-16 13:54:52 -070026#include "benchmark-helpers.hpp"
Yumin Xiad72f0922016-03-30 22:19:14 +080027#include "table/fib.hpp"
Davide Pesaventoa997d292017-08-24 20:16:59 -040028#include "table/pit.hpp"
Yumin Xiad72f0922016-03-30 22:19:14 +080029
Davide Pesaventoa997d292017-08-24 20:16:59 -040030#include <iostream>
31
Davide Pesavento264af772021-02-09 21:48:24 -050032#ifdef NFD_HAVE_VALGRIND
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000033#include <valgrind/callgrind.h>
34#endif
35
Yumin Xiad72f0922016-03-30 22:19:14 +080036namespace nfd {
37namespace tests {
38
Eric Newberry52ae3292017-11-16 13:54:52 -070039class PitFibBenchmarkFixture
Yumin Xiad72f0922016-03-30 22:19:14 +080040{
41protected:
42 PitFibBenchmarkFixture()
43 : m_fib(m_nameTree)
44 , m_pit(m_nameTree)
45 {
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000046#ifdef _DEBUG
Davide Pesaventoa997d292017-08-24 20:16:59 -040047 std::cerr << "Benchmark compiled in debug mode is unreliable, please compile in release mode.\n";
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000048#endif
Yumin Xiad72f0922016-03-30 22:19:14 +080049 }
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 Shif9f7cf32016-08-08 05:51:06 +000064 extendName(prefix, fibPrefixLength);
65 m_fib.insert(prefix);
Yumin Xiad72f0922016-03-30 22:19:14 +080066
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 Xiad72f0922016-03-30 22:19:14 +080077 }
78 }
79
80private:
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
90protected:
91 std::vector<shared_ptr<Interest>> interests;
92 std::vector<shared_ptr<Data>> data;
Yumin Xiad72f0922016-03-30 22:19:14 +080093
94protected:
95 NameTree m_nameTree;
96 Fib m_fib;
97 Pit m_pit;
98};
99
100// This test case models PIT and FIB operations with simple Interest-Data exchanges.
101// A total of nRoundTrip Interests are received and forwarded, and the same number of Data are returned.
102BOOST_FIXTURE_TEST_CASE(SimpleExchanges, PitFibBenchmarkFixture)
103{
104 // number of Interest-Data exchanges
105 const size_t nRoundTrip = 1000000;
106 // number of iterations between processing incoming Interest and processing incoming Data
Davide Pesavento5f596072019-05-11 16:52:05 -0400107 const size_t replyGap = 20000;
108 // total amount of FIB entries
Yumin Xiad72f0922016-03-30 22:19:14 +0800109 // packet names are homogeneously extended from these FIB entries
110 const size_t nFibEntries = 2000;
111 // length of fibPrefix, must be >= 1
112 const size_t fibPrefixLength = 1;
113 // length of Interest Name >= fibPrefixLength
114 const size_t interestNameLength= 2;
115 // length of Data Name >= Interest Name
116 const size_t dataNameLength = 3;
117
118 generatePacketsAndPopulateFib(nRoundTrip, nFibEntries, fibPrefixLength,
119 interestNameLength, dataNameLength);
120
Davide Pesavento264af772021-02-09 21:48:24 -0500121#ifdef NFD_HAVE_VALGRIND
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000122 CALLGRIND_START_INSTRUMENTATION;
123#endif
124
Yumin Xiad72f0922016-03-30 22:19:14 +0800125 auto t1 = time::steady_clock::now();
126
Davide Pesavento5f596072019-05-11 16:52:05 -0400127 for (size_t i = 0; i < nRoundTrip + replyGap; ++i) {
Yumin Xiad72f0922016-03-30 22:19:14 +0800128 if (i < nRoundTrip) {
129 // process incoming Interest
Davide Pesavento5f596072019-05-11 16:52:05 -0400130 auto pitEntry = m_pit.insert(*interests[i]).first;
Yumin Xiad72f0922016-03-30 22:19:14 +0800131 m_fib.findLongestPrefixMatch(*pitEntry);
132 }
Davide Pesavento5f596072019-05-11 16:52:05 -0400133 if (i >= replyGap) {
Yumin Xiad72f0922016-03-30 22:19:14 +0800134 // process incoming Data
Davide Pesavento5f596072019-05-11 16:52:05 -0400135 auto matches = m_pit.findAllDataMatches(*data[i - replyGap]);
136 // delete matching PIT entries
137 for (const auto& pitEntry : matches) {
138 m_pit.erase(pitEntry.get());
139 }
Yumin Xiad72f0922016-03-30 22:19:14 +0800140 }
141 }
142
143 auto t2 = time::steady_clock::now();
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000144
Davide Pesavento264af772021-02-09 21:48:24 -0500145#ifdef NFD_HAVE_VALGRIND
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000146 CALLGRIND_STOP_INSTRUMENTATION;
147#endif
148
Davide Pesaventoa997d292017-08-24 20:16:59 -0400149 std::cout << time::duration_cast<time::microseconds>(t2 - t1) << std::endl;
Yumin Xiad72f0922016-03-30 22:19:14 +0800150}
151
152} // namespace tests
153} // namespace nfd