blob: 6166899212c7f1f9fab6fb353cb5897570e41f5e [file] [log] [blame]
Junxiao Shi9f764b52016-08-10 02:59:48 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 Regents of the University of California.
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
24#define BOOST_TEST_MODULE Scheduler Benchmark
25
26#include "util/scheduler.hpp"
27
28#include "boost-test.hpp"
29
30namespace ndn {
31namespace util {
32namespace scheduler {
33namespace tests {
34
35BOOST_AUTO_TEST_CASE(ScheduleCancel)
36{
37 boost::asio::io_service io;
38 Scheduler sched(io);
39
40 const int nEvents = 1000000;
41 std::vector<EventId> eventIds(nEvents);
42
43 time::steady_clock::TimePoint t1 = time::steady_clock::now();
44 for (int i = 0; i < nEvents; ++i) {
45 eventIds[i] = sched.scheduleEvent(time::seconds(1), []{});
46 }
47 time::steady_clock::TimePoint t2 = time::steady_clock::now();
48 for (int i = 0; i < nEvents; ++i) {
49 sched.cancelEvent(eventIds[i]);
50 }
51 time::steady_clock::TimePoint t3 = time::steady_clock::now();
52
53 BOOST_TEST_MESSAGE("schedule " << nEvents << " events: " << (t2 - t1));
54 BOOST_TEST_MESSAGE("cancel " << nEvents << " events: " << (t3 - t2));
55}
56
57BOOST_AUTO_TEST_CASE(Execute)
58{
59 boost::asio::io_service io;
60 Scheduler sched(io);
61
62 const int nEvents = 1000000;
63 int nExpired = 0;
64
65 // Events should expire at t1, but execution finishes at t2. The difference is the overhead.
66 time::steady_clock::TimePoint t1 = time::steady_clock::now() + time::seconds(5);
67 time::steady_clock::TimePoint t2;
68 // +1ms ensures this extra event is executed last. In case the overhead is less than 1ms,
69 // it will be reported as 1ms.
70 sched.scheduleEvent(t1 - time::steady_clock::now() + time::milliseconds(1), [&] {
71 t2 = time::steady_clock::now();
72 BOOST_REQUIRE_EQUAL(nExpired, nEvents);
73 });
74
75 for (int i = 0; i < nEvents; ++i) {
76 sched.scheduleEvent(t1 - time::steady_clock::now(), [&] { ++nExpired; });
77 }
78
79 io.run();
80
81 BOOST_REQUIRE_EQUAL(nExpired, nEvents);
82 BOOST_TEST_MESSAGE("execute " << nEvents << " events: " << (t2 - t1));
83}
84
85} // namespace tests
86} // namespace scheduler
87} // namespace util
88} // namespace ndn