blob: 3074d22605558e16597411112d62153674f72edd [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/*
3 * Copyright (c) 2013-2017 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
22#define BOOST_TEST_MAIN 1
23#define BOOST_TEST_DYN_LINK 1
Davide Pesaventoeee3e822016-11-26 19:19:34 +010024#define BOOST_TEST_MODULE ndn-cxx Scheduler Benchmark
Junxiao Shi9f764b52016-08-10 02:59:48 +000025
26#include "util/scheduler.hpp"
27
28#include "boost-test.hpp"
Junxiao Shi770f9042017-07-13 13:30:53 +000029#include "timed-execute.hpp"
Junxiao Shi9f764b52016-08-10 02:59:48 +000030
31namespace ndn {
32namespace util {
33namespace scheduler {
34namespace tests {
35
Junxiao Shi770f9042017-07-13 13:30:53 +000036using namespace ndn::tests;
37
Junxiao Shi9f764b52016-08-10 02:59:48 +000038BOOST_AUTO_TEST_CASE(ScheduleCancel)
39{
40 boost::asio::io_service io;
41 Scheduler sched(io);
42
43 const int nEvents = 1000000;
44 std::vector<EventId> eventIds(nEvents);
45
Junxiao Shi770f9042017-07-13 13:30:53 +000046 auto d1 = timedExecute([&] {
47 for (int i = 0; i < nEvents; ++i) {
48 eventIds[i] = sched.scheduleEvent(time::seconds(1), []{});
49 }
50 });
Junxiao Shi9f764b52016-08-10 02:59:48 +000051
Junxiao Shi770f9042017-07-13 13:30:53 +000052 auto d2 = timedExecute([&] {
53 for (int i = 0; i < nEvents; ++i) {
54 sched.cancelEvent(eventIds[i]);
55 }
56 });
57
58 std::cout << "schedule " << nEvents << " events: " << d1 << std::endl;
59 std::cout << "cancel " << nEvents << " events: " << d2 << std::endl;
Junxiao Shi9f764b52016-08-10 02:59:48 +000060}
61
62BOOST_AUTO_TEST_CASE(Execute)
63{
64 boost::asio::io_service io;
65 Scheduler sched(io);
66
67 const int nEvents = 1000000;
68 int nExpired = 0;
69
70 // Events should expire at t1, but execution finishes at t2. The difference is the overhead.
71 time::steady_clock::TimePoint t1 = time::steady_clock::now() + time::seconds(5);
72 time::steady_clock::TimePoint t2;
73 // +1ms ensures this extra event is executed last. In case the overhead is less than 1ms,
74 // it will be reported as 1ms.
75 sched.scheduleEvent(t1 - time::steady_clock::now() + time::milliseconds(1), [&] {
76 t2 = time::steady_clock::now();
77 BOOST_REQUIRE_EQUAL(nExpired, nEvents);
78 });
79
80 for (int i = 0; i < nEvents; ++i) {
81 sched.scheduleEvent(t1 - time::steady_clock::now(), [&] { ++nExpired; });
82 }
83
84 io.run();
85
86 BOOST_REQUIRE_EQUAL(nExpired, nEvents);
Junxiao Shi770f9042017-07-13 13:30:53 +000087 std::cout << "execute " << nEvents << " events: " << (t2 - t1) << std::endl;
Junxiao Shi9f764b52016-08-10 02:59:48 +000088}
89
90} // namespace tests
91} // namespace scheduler
92} // namespace util
93} // namespace ndn