tests: PIT and FIB benchmark

Change-Id: I906f82f42dc6ca5dbf0871fae99c47fdda62c7a5
Refs: #3571
diff --git a/tests/other/pit-fib-benchmark.cpp b/tests/other/pit-fib-benchmark.cpp
new file mode 100644
index 0000000..210f8c6
--- /dev/null
+++ b/tests/other/pit-fib-benchmark.cpp
@@ -0,0 +1,141 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "table/pit.hpp"
+#include "table/fib.hpp"
+
+#include "tests/test-common.hpp"
+
+namespace nfd {
+namespace tests {
+
+class PitFibBenchmarkFixture : public BaseFixture
+{
+protected:
+  PitFibBenchmarkFixture()
+    : m_fib(m_nameTree)
+    , m_pit(m_nameTree)
+  {
+  }
+
+  void
+  generatePacketsAndPopulateFib(size_t nPackets,
+                                size_t nFibEntries,
+                                size_t fibPrefixLength,
+                                size_t interestNameLength,
+                                size_t dataNameLength)
+  {
+    BOOST_ASSERT(1 <= fibPrefixLength);
+    BOOST_ASSERT(fibPrefixLength <= interestNameLength);
+    BOOST_ASSERT(interestNameLength <= dataNameLength);
+
+    for (size_t i = 0; i < nPackets; i++) {
+      Name prefix(to_string(i / nFibEntries));
+      extendName(prefix, fibPrefixLength - 1);
+
+      Name interestName = prefix;
+      if (nPackets > nFibEntries) {
+        interestName.append(to_string(i));
+      }
+      extendName(interestName, interestNameLength);
+      interests.push_back(make_shared<Interest>(interestName));
+
+      Name dataName = interestName;
+      extendName(dataName, dataNameLength);
+      data.push_back(make_shared<Data>(dataName));
+
+      m_fib.insert(prefix);
+    }
+  }
+
+private:
+  static void
+  extendName(Name& name, size_t length)
+  {
+    BOOST_ASSERT(length >= name.size());
+    for (size_t i = name.size(); i < length; i++) {
+      name.append("dup");
+    }
+  }
+
+protected:
+  std::vector<shared_ptr<Interest>> interests;
+  std::vector<shared_ptr<Data>> data;
+  std::vector<shared_ptr<pit::Entry>> pitEntries;
+
+protected:
+  NameTree m_nameTree;
+  Fib m_fib;
+  Pit m_pit;
+};
+
+// This test case models PIT and FIB operations with simple Interest-Data exchanges.
+// A total of nRoundTrip Interests are received and forwarded, and the same number of Data are returned.
+BOOST_FIXTURE_TEST_CASE(SimpleExchanges, PitFibBenchmarkFixture)
+{
+  // number of Interest-Data exchanges
+  const size_t nRoundTrip = 1000000;
+  // number of iterations between processing incoming Interest and processing incoming Data
+  const size_t gap3 = 20000;
+  // number of iterations between processing incoming Data and deleting PIT entry
+  const size_t gap4 = 30000;
+  // total amount of FIB entires
+  // packet names are homogeneously extended from these FIB entries
+  const size_t nFibEntries = 2000;
+  // length of fibPrefix, must be >= 1
+  const size_t fibPrefixLength = 1;
+  // length of Interest Name >= fibPrefixLength
+  const size_t interestNameLength= 2;
+  // length of Data Name >= Interest Name
+  const size_t dataNameLength = 3;
+
+  generatePacketsAndPopulateFib(nRoundTrip, nFibEntries, fibPrefixLength,
+                                interestNameLength, dataNameLength);
+
+  auto t1 = time::steady_clock::now();
+
+  for (size_t i = 0; i < nRoundTrip + gap3 + gap4; ++i) {
+    if (i < nRoundTrip) {
+      // process incoming Interest
+      shared_ptr<pit::Entry> pitEntry = m_pit.insert(*interests[i]).first;
+      pitEntries.push_back(pitEntry);
+      m_fib.findLongestPrefixMatch(*pitEntry);
+    }
+    if (i >= gap3 && i < nRoundTrip + gap3) {
+      // process incoming Data
+      m_pit.findAllDataMatches(*data[i - gap3]);
+    }
+    if (i >= gap3 + gap4) {
+      // delete PIT entry
+      m_pit.erase(pitEntries[i - gap3 - gap4]);
+    }
+  }
+
+  auto t2 = time::steady_clock::now();
+  BOOST_TEST_MESSAGE(time::duration_cast<time::microseconds>(t2 - t1));
+}
+
+} // namespace tests
+} // namespace nfd
diff --git a/tests/other/wscript b/tests/other/wscript
index 6398f32..95fd9c5 100644
--- a/tests/other/wscript
+++ b/tests/other/wscript
@@ -27,7 +27,11 @@
 top = '../..'
 
 def build(bld):
-   for module, name in {"cs-benchmark": "CS Benchmark"}.items():
+   testPrograms = {
+      "cs-benchmark": "CS Benchmark",
+      "pit-fib-benchmark": "PIT & FIB Benchmark",
+   }
+   for module, name in testPrograms.items():
        # main()
        bld(target='unit-tests-%s-main' % module,
            name='unit-tests-%s-main' % module,