tests: rename integrated -> integration

And move benchmarks to their own subdir

Change-Id: I23460e1135b47d505eacf0571ced1cbf810b7680
diff --git a/tests/benchmarks/encoding-bench.cpp b/tests/benchmarks/encoding-bench.cpp
new file mode 100644
index 0000000..3ab246d
--- /dev/null
+++ b/tests/benchmarks/encoding-bench.cpp
@@ -0,0 +1,133 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2020 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#define BOOST_TEST_MODULE ndn-cxx Encoding Benchmark
+#include "tests/boost-test.hpp"
+
+#include "ndn-cxx/encoding/tlv.hpp"
+#include "tests/benchmarks/timed-execute.hpp"
+
+#include <boost/mpl/vector.hpp>
+#include <boost/mpl/vector_c.hpp>
+
+#include <iostream>
+
+namespace ndn {
+namespace tlv {
+namespace tests {
+
+using namespace ndn::tests;
+
+template<size_t WIRE_SIZE>
+struct ReadVarNumberTest;
+
+template<>
+struct ReadVarNumberTest<1>
+{
+  static const uint8_t WIRE[];
+  static const uint64_t VALUE = 252;
+};
+const uint8_t ReadVarNumberTest<1>::WIRE[] = {0xfc};
+
+template<>
+struct ReadVarNumberTest<3>
+{
+  static const uint8_t WIRE[];
+  static const uint64_t VALUE = 253;
+};
+const uint8_t ReadVarNumberTest<3>::WIRE[] = {0xfd, 0x00, 0xfd};
+
+template<>
+struct ReadVarNumberTest<5>
+{
+  static const uint8_t WIRE[];
+  static const uint64_t VALUE = 65536;
+};
+const uint8_t ReadVarNumberTest<5>::WIRE[] = {0xfe, 0x00, 0x01, 0x00, 0x00};
+
+template<>
+struct ReadVarNumberTest<9>
+{
+  static const uint8_t WIRE[];
+  static const uint64_t VALUE = 4294967296;
+};
+const uint8_t ReadVarNumberTest<9>::WIRE[] = {0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00};
+
+template<size_t WIRE_SIZE, size_t ALIGNMENT_OFFSET>
+struct ReadVarNumberAlignTest : public ReadVarNumberTest<WIRE_SIZE>
+{
+  using AlignmentOffset = std::integral_constant<size_t, ALIGNMENT_OFFSET>;
+
+  static_assert(sizeof(ReadVarNumberTest<WIRE_SIZE>::WIRE) == WIRE_SIZE, "");
+};
+
+using ReadVarNumberTests = boost::mpl::vector<
+  ReadVarNumberAlignTest<1, 0>,
+  ReadVarNumberAlignTest<3, 0>,
+  ReadVarNumberAlignTest<3, 1>,
+  ReadVarNumberAlignTest<5, 0>,
+  ReadVarNumberAlignTest<5, 1>,
+  ReadVarNumberAlignTest<5, 2>,
+  ReadVarNumberAlignTest<5, 3>,
+  ReadVarNumberAlignTest<9, 0>,
+  ReadVarNumberAlignTest<9, 1>,
+  ReadVarNumberAlignTest<9, 2>,
+  ReadVarNumberAlignTest<9, 3>,
+  ReadVarNumberAlignTest<9, 4>,
+  ReadVarNumberAlignTest<9, 5>,
+  ReadVarNumberAlignTest<9, 6>,
+  ReadVarNumberAlignTest<9, 7>
+>;
+
+// Benchmark of ndn::tlv::readVarNumber with different number lengths and alignments.
+// For accurate results, it is required to compile ndn-cxx in release mode.
+// It is recommended to run the benchmark multiple times and take the average.
+BOOST_AUTO_TEST_CASE_TEMPLATE(ReadVarNumber, Test, ReadVarNumberTests)
+{
+  const int N_ITERATIONS = 100000000;
+
+  alignas(8) uint8_t buffer[16];
+  static_assert(Test::AlignmentOffset::value + sizeof(Test::WIRE) <= sizeof(buffer), "");
+  uint8_t* const begin = buffer + Test::AlignmentOffset::value;
+  std::memcpy(begin, Test::WIRE, sizeof(Test::WIRE));
+  const uint8_t* const end = begin + sizeof(Test::WIRE);
+
+  int nOks = 0;
+  int nCorrects = 0;
+  auto d = timedExecute([&] {
+    uint64_t number = 0;
+    for (int i = 0; i < N_ITERATIONS; ++i) {
+      const uint8_t* begin2 = begin; // make a copy because readVarNumber increments the pointer
+      nOks += readVarNumber(begin2, end, number);
+      nCorrects += number == Test::VALUE;
+      // use the number and the return value, so compiler won't optimize out their computation
+    }
+  });
+  BOOST_CHECK_EQUAL(nOks, N_ITERATIONS);
+  BOOST_CHECK_EQUAL(nCorrects, N_ITERATIONS);
+  std::cout << "size=" << sizeof(Test::WIRE)
+            << " offset=" << Test::AlignmentOffset::value
+            << " " << d << std::endl;
+}
+
+} // namespace tests
+} // namespace tlv
+} // namespace ndn
diff --git a/tests/benchmarks/scheduler-bench.cpp b/tests/benchmarks/scheduler-bench.cpp
new file mode 100644
index 0000000..8897920
--- /dev/null
+++ b/tests/benchmarks/scheduler-bench.cpp
@@ -0,0 +1,91 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2020 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#define BOOST_TEST_MODULE ndn-cxx Scheduler Benchmark
+#include "tests/boost-test.hpp"
+
+#include "ndn-cxx/util/scheduler.hpp"
+#include "tests/benchmarks/timed-execute.hpp"
+
+#include <boost/asio/io_service.hpp>
+#include <iostream>
+
+namespace ndn {
+namespace scheduler {
+namespace tests {
+
+using namespace ndn::tests;
+
+BOOST_AUTO_TEST_CASE(ScheduleCancel)
+{
+  boost::asio::io_service io;
+  Scheduler sched(io);
+
+  const size_t nEvents = 1000000;
+  std::vector<EventId> eventIds(nEvents);
+
+  auto d1 = timedExecute([&] {
+    for (size_t i = 0; i < nEvents; ++i) {
+      eventIds[i] = sched.schedule(1_s, []{});
+    }
+  });
+
+  auto d2 = timedExecute([&] {
+    for (size_t i = 0; i < nEvents; ++i) {
+      eventIds[i].cancel();
+    }
+  });
+
+  std::cout << "schedule " << nEvents << " events: " << d1 << std::endl;
+  std::cout << "cancel " << nEvents << " events: " << d2 << std::endl;
+}
+
+BOOST_AUTO_TEST_CASE(Execute)
+{
+  boost::asio::io_service io;
+  Scheduler sched(io);
+
+  const size_t nEvents = 1000000;
+  size_t nExpired = 0;
+
+  // Events should expire at t1, but execution finishes at t2. The difference is the overhead.
+  time::steady_clock::TimePoint t1 = time::steady_clock::now() + 5_s;
+  time::steady_clock::TimePoint t2;
+  // +1ms ensures this extra event is executed last. In case the overhead is less than 1ms,
+  // it will be reported as 1ms.
+  sched.schedule(t1 - time::steady_clock::now() + 1_ms, [&] {
+    t2 = time::steady_clock::now();
+    BOOST_REQUIRE_EQUAL(nExpired, nEvents);
+  });
+
+  for (size_t i = 0; i < nEvents; ++i) {
+    sched.schedule(t1 - time::steady_clock::now(), [&] { ++nExpired; });
+  }
+
+  io.run();
+
+  BOOST_REQUIRE_EQUAL(nExpired, nEvents);
+  std::cout << "execute " << nEvents << " events: " << (t2 - t1) << std::endl;
+}
+
+} // namespace tests
+} // namespace scheduler
+} // namespace ndn
diff --git a/tests/benchmarks/timed-execute.hpp b/tests/benchmarks/timed-execute.hpp
new file mode 100644
index 0000000..c0759e3
--- /dev/null
+++ b/tests/benchmarks/timed-execute.hpp
@@ -0,0 +1,43 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2020 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_TESTS_BENCHMARKS_TIMED_EXECUTE_HPP
+#define NDN_TESTS_BENCHMARKS_TIMED_EXECUTE_HPP
+
+#include "ndn-cxx/util/time.hpp"
+
+namespace ndn {
+namespace tests {
+
+template<typename F>
+time::nanoseconds
+timedExecute(const F& f)
+{
+  auto before = time::steady_clock::now();
+  f();
+  auto after = time::steady_clock::now();
+  return after - before;
+}
+
+} // namespace tests
+} // namespace ndn
+
+#endif // NDN_TESTS_BENCHMARKS_TIMED_EXECUTE_HPP
diff --git a/tests/benchmarks/wscript b/tests/benchmarks/wscript
new file mode 100644
index 0000000..73158d8
--- /dev/null
+++ b/tests/benchmarks/wscript
@@ -0,0 +1,12 @@
+# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
+
+top = '../..'
+
+def build(bld):
+    for test in bld.path.ant_glob('*.cpp'):
+        name = test.change_ext('').path_from(bld.path.get_bld())
+        bld.program(name='test-%s' % name,
+                    target=name,
+                    source=[test],
+                    use='tests-common',
+                    install_path=None)