blob: b861f04f7c7f28821506258ff95868211a07b01c [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 Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 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
Davide Pesaventobdcedf42017-10-15 14:56:28 -040031#include <boost/asio/io_service.hpp>
Davide Pesaventocdcde902017-08-23 15:40:22 -040032#include <iostream>
33
Junxiao Shi9f764b52016-08-10 02:59:48 +000034namespace ndn {
35namespace util {
36namespace scheduler {
37namespace tests {
38
Junxiao Shi770f9042017-07-13 13:30:53 +000039using namespace ndn::tests;
40
Junxiao Shi9f764b52016-08-10 02:59:48 +000041BOOST_AUTO_TEST_CASE(ScheduleCancel)
42{
43 boost::asio::io_service io;
44 Scheduler sched(io);
45
Davide Pesaventobdcedf42017-10-15 14:56:28 -040046 const size_t nEvents = 1000000;
Junxiao Shi9f764b52016-08-10 02:59:48 +000047 std::vector<EventId> eventIds(nEvents);
48
Junxiao Shi770f9042017-07-13 13:30:53 +000049 auto d1 = timedExecute([&] {
Davide Pesaventobdcedf42017-10-15 14:56:28 -040050 for (size_t i = 0; i < nEvents; ++i) {
Davide Pesavento0f830802018-01-16 23:58:58 -050051 eventIds[i] = sched.scheduleEvent(1_s, []{});
Junxiao Shi770f9042017-07-13 13:30:53 +000052 }
53 });
Junxiao Shi9f764b52016-08-10 02:59:48 +000054
Junxiao Shi770f9042017-07-13 13:30:53 +000055 auto d2 = timedExecute([&] {
Davide Pesaventobdcedf42017-10-15 14:56:28 -040056 for (size_t i = 0; i < nEvents; ++i) {
Junxiao Shi770f9042017-07-13 13:30:53 +000057 sched.cancelEvent(eventIds[i]);
58 }
59 });
60
61 std::cout << "schedule " << nEvents << " events: " << d1 << std::endl;
62 std::cout << "cancel " << nEvents << " events: " << d2 << std::endl;
Junxiao Shi9f764b52016-08-10 02:59:48 +000063}
64
65BOOST_AUTO_TEST_CASE(Execute)
66{
67 boost::asio::io_service io;
68 Scheduler sched(io);
69
Davide Pesaventobdcedf42017-10-15 14:56:28 -040070 const size_t nEvents = 1000000;
71 size_t nExpired = 0;
Junxiao Shi9f764b52016-08-10 02:59:48 +000072
73 // Events should expire at t1, but execution finishes at t2. The difference is the overhead.
Davide Pesavento0f830802018-01-16 23:58:58 -050074 time::steady_clock::TimePoint t1 = time::steady_clock::now() + 5_s;
Junxiao Shi9f764b52016-08-10 02:59:48 +000075 time::steady_clock::TimePoint t2;
76 // +1ms ensures this extra event is executed last. In case the overhead is less than 1ms,
77 // it will be reported as 1ms.
Davide Pesavento0f830802018-01-16 23:58:58 -050078 sched.scheduleEvent(t1 - time::steady_clock::now() + 1_ms, [&] {
Junxiao Shi9f764b52016-08-10 02:59:48 +000079 t2 = time::steady_clock::now();
80 BOOST_REQUIRE_EQUAL(nExpired, nEvents);
81 });
82
Davide Pesaventobdcedf42017-10-15 14:56:28 -040083 for (size_t i = 0; i < nEvents; ++i) {
Junxiao Shi9f764b52016-08-10 02:59:48 +000084 sched.scheduleEvent(t1 - time::steady_clock::now(), [&] { ++nExpired; });
85 }
86
87 io.run();
88
89 BOOST_REQUIRE_EQUAL(nExpired, nEvents);
Junxiao Shi770f9042017-07-13 13:30:53 +000090 std::cout << "execute " << nEvents << " events: " << (t2 - t1) << std::endl;
Junxiao Shi9f764b52016-08-10 02:59:48 +000091}
92
93} // namespace tests
94} // namespace scheduler
95} // namespace util
96} // namespace ndn