Junxiao Shi | 9f764b5 | 2016-08-10 02:59:48 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 770f904 | 2017-07-13 13:30:53 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Junxiao Shi | 9f764b5 | 2016-08-10 02:59:48 +0000 | [diff] [blame] | 4 | * |
| 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 | |
| 22 | #define BOOST_TEST_MAIN 1 |
| 23 | #define BOOST_TEST_DYN_LINK 1 |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 24 | #define BOOST_TEST_MODULE ndn-cxx Scheduler Benchmark |
Junxiao Shi | 9f764b5 | 2016-08-10 02:59:48 +0000 | [diff] [blame] | 25 | |
| 26 | #include "util/scheduler.hpp" |
| 27 | |
| 28 | #include "boost-test.hpp" |
Junxiao Shi | 770f904 | 2017-07-13 13:30:53 +0000 | [diff] [blame] | 29 | #include "timed-execute.hpp" |
Junxiao Shi | 9f764b5 | 2016-08-10 02:59:48 +0000 | [diff] [blame] | 30 | |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame^] | 31 | #include <iostream> |
| 32 | |
Junxiao Shi | 9f764b5 | 2016-08-10 02:59:48 +0000 | [diff] [blame] | 33 | namespace ndn { |
| 34 | namespace util { |
| 35 | namespace scheduler { |
| 36 | namespace tests { |
| 37 | |
Junxiao Shi | 770f904 | 2017-07-13 13:30:53 +0000 | [diff] [blame] | 38 | using namespace ndn::tests; |
| 39 | |
Junxiao Shi | 9f764b5 | 2016-08-10 02:59:48 +0000 | [diff] [blame] | 40 | BOOST_AUTO_TEST_CASE(ScheduleCancel) |
| 41 | { |
| 42 | boost::asio::io_service io; |
| 43 | Scheduler sched(io); |
| 44 | |
| 45 | const int nEvents = 1000000; |
| 46 | std::vector<EventId> eventIds(nEvents); |
| 47 | |
Junxiao Shi | 770f904 | 2017-07-13 13:30:53 +0000 | [diff] [blame] | 48 | auto d1 = timedExecute([&] { |
| 49 | for (int i = 0; i < nEvents; ++i) { |
| 50 | eventIds[i] = sched.scheduleEvent(time::seconds(1), []{}); |
| 51 | } |
| 52 | }); |
Junxiao Shi | 9f764b5 | 2016-08-10 02:59:48 +0000 | [diff] [blame] | 53 | |
Junxiao Shi | 770f904 | 2017-07-13 13:30:53 +0000 | [diff] [blame] | 54 | auto d2 = timedExecute([&] { |
| 55 | for (int i = 0; i < nEvents; ++i) { |
| 56 | sched.cancelEvent(eventIds[i]); |
| 57 | } |
| 58 | }); |
| 59 | |
| 60 | std::cout << "schedule " << nEvents << " events: " << d1 << std::endl; |
| 61 | std::cout << "cancel " << nEvents << " events: " << d2 << std::endl; |
Junxiao Shi | 9f764b5 | 2016-08-10 02:59:48 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | BOOST_AUTO_TEST_CASE(Execute) |
| 65 | { |
| 66 | boost::asio::io_service io; |
| 67 | Scheduler sched(io); |
| 68 | |
| 69 | const int nEvents = 1000000; |
| 70 | int nExpired = 0; |
| 71 | |
| 72 | // Events should expire at t1, but execution finishes at t2. The difference is the overhead. |
| 73 | time::steady_clock::TimePoint t1 = time::steady_clock::now() + time::seconds(5); |
| 74 | time::steady_clock::TimePoint t2; |
| 75 | // +1ms ensures this extra event is executed last. In case the overhead is less than 1ms, |
| 76 | // it will be reported as 1ms. |
| 77 | sched.scheduleEvent(t1 - time::steady_clock::now() + time::milliseconds(1), [&] { |
| 78 | t2 = time::steady_clock::now(); |
| 79 | BOOST_REQUIRE_EQUAL(nExpired, nEvents); |
| 80 | }); |
| 81 | |
| 82 | for (int i = 0; i < nEvents; ++i) { |
| 83 | sched.scheduleEvent(t1 - time::steady_clock::now(), [&] { ++nExpired; }); |
| 84 | } |
| 85 | |
| 86 | io.run(); |
| 87 | |
| 88 | BOOST_REQUIRE_EQUAL(nExpired, nEvents); |
Junxiao Shi | 770f904 | 2017-07-13 13:30:53 +0000 | [diff] [blame] | 89 | std::cout << "execute " << nEvents << " events: " << (t2 - t1) << std::endl; |
Junxiao Shi | 9f764b5 | 2016-08-10 02:59:48 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | } // namespace tests |
| 93 | } // namespace scheduler |
| 94 | } // namespace util |
| 95 | } // namespace ndn |