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