blob: e1e3433d02f3ce5c3a0a3c5cee47e40cc7f44559 [file] [log] [blame]
Junxiao Shi9f764b52016-08-10 02:59:48 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi770f9042017-07-13 13:30:53 +00002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Junxiao Shi9f764b52016-08-10 02:59:48 +00004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Davide Pesaventoeee3e822016-11-26 19:19:34 +010022#define BOOST_TEST_MODULE ndn-cxx Scheduler Benchmark
Davide Pesavento7e780642018-11-24 15:51:34 -050023#include "tests/boost-test.hpp"
Junxiao Shi9f764b52016-08-10 02:59:48 +000024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "ndn-cxx/util/scheduler.hpp"
Davide Pesavento394977c2020-04-25 21:40:31 -040026#include "tests/benchmarks/timed-execute.hpp"
Junxiao Shi9f764b52016-08-10 02:59:48 +000027
Davide Pesavento2f46d652023-11-09 23:40:01 -050028#include <boost/asio/io_context.hpp>
Davide Pesaventocdcde902017-08-23 15:40:22 -040029#include <iostream>
30
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040031namespace ndn::tests {
Junxiao Shi770f9042017-07-13 13:30:53 +000032
Junxiao Shi9f764b52016-08-10 02:59:48 +000033BOOST_AUTO_TEST_CASE(ScheduleCancel)
34{
Davide Pesavento2f46d652023-11-09 23:40:01 -050035 boost::asio::io_context io;
Junxiao Shi9f764b52016-08-10 02:59:48 +000036 Scheduler sched(io);
37
Davide Pesaventobdcedf42017-10-15 14:56:28 -040038 const size_t nEvents = 1000000;
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040039 std::vector<scheduler::EventId> eventIds(nEvents);
Junxiao Shi9f764b52016-08-10 02:59:48 +000040
Junxiao Shi770f9042017-07-13 13:30:53 +000041 auto d1 = timedExecute([&] {
Davide Pesaventobdcedf42017-10-15 14:56:28 -040042 for (size_t i = 0; i < nEvents; ++i) {
Junxiao Shia5f233e2019-03-18 09:39:22 -060043 eventIds[i] = sched.schedule(1_s, []{});
Junxiao Shi770f9042017-07-13 13:30:53 +000044 }
45 });
Junxiao Shi9f764b52016-08-10 02:59:48 +000046
Junxiao Shi770f9042017-07-13 13:30:53 +000047 auto d2 = timedExecute([&] {
Davide Pesaventobdcedf42017-10-15 14:56:28 -040048 for (size_t i = 0; i < nEvents; ++i) {
Junxiao Shia5f233e2019-03-18 09:39:22 -060049 eventIds[i].cancel();
Junxiao Shi770f9042017-07-13 13:30:53 +000050 }
51 });
52
53 std::cout << "schedule " << nEvents << " events: " << d1 << std::endl;
54 std::cout << "cancel " << nEvents << " events: " << d2 << std::endl;
Junxiao Shi9f764b52016-08-10 02:59:48 +000055}
56
57BOOST_AUTO_TEST_CASE(Execute)
58{
Davide Pesavento2f46d652023-11-09 23:40:01 -050059 boost::asio::io_context io;
Junxiao Shi9f764b52016-08-10 02:59:48 +000060 Scheduler sched(io);
61
Davide Pesaventobdcedf42017-10-15 14:56:28 -040062 const size_t nEvents = 1000000;
63 size_t nExpired = 0;
Junxiao Shi9f764b52016-08-10 02:59:48 +000064
65 // Events should expire at t1, but execution finishes at t2. The difference is the overhead.
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040066 time::steady_clock::time_point t1 = time::steady_clock::now() + 5_s;
67 time::steady_clock::time_point t2;
Junxiao Shi9f764b52016-08-10 02:59:48 +000068 // +1ms ensures this extra event is executed last. In case the overhead is less than 1ms,
69 // it will be reported as 1ms.
Junxiao Shia5f233e2019-03-18 09:39:22 -060070 sched.schedule(t1 - time::steady_clock::now() + 1_ms, [&] {
Junxiao Shi9f764b52016-08-10 02:59:48 +000071 t2 = time::steady_clock::now();
72 BOOST_REQUIRE_EQUAL(nExpired, nEvents);
73 });
74
Davide Pesaventobdcedf42017-10-15 14:56:28 -040075 for (size_t i = 0; i < nEvents; ++i) {
Junxiao Shia5f233e2019-03-18 09:39:22 -060076 sched.schedule(t1 - time::steady_clock::now(), [&] { ++nExpired; });
Junxiao Shi9f764b52016-08-10 02:59:48 +000077 }
78
79 io.run();
80
81 BOOST_REQUIRE_EQUAL(nExpired, nEvents);
Junxiao Shi770f9042017-07-13 13:30:53 +000082 std::cout << "execute " << nEvents << " events: " << (t2 - t1) << std::endl;
Junxiao Shi9f764b52016-08-10 02:59:48 +000083}
84
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040085} // namespace ndn::tests