Junxiao Shi | 770f904 | 2017-07-13 13:30:53 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 394977c | 2020-04-25 21:40:31 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 Regents of the University of California. |
Junxiao Shi | 770f904 | 2017-07-13 13:30:53 +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 | |
Junxiao Shi | 770f904 | 2017-07-13 13:30:53 +0000 | [diff] [blame] | 22 | #define BOOST_TEST_MODULE ndn-cxx Encoding Benchmark |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 23 | #include "tests/boost-test.hpp" |
Junxiao Shi | 770f904 | 2017-07-13 13:30:53 +0000 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/encoding/tlv.hpp" |
Davide Pesavento | 394977c | 2020-04-25 21:40:31 -0400 | [diff] [blame] | 26 | #include "tests/benchmarks/timed-execute.hpp" |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 27 | |
Junxiao Shi | 770f904 | 2017-07-13 13:30:53 +0000 | [diff] [blame] | 28 | #include <boost/mpl/vector.hpp> |
| 29 | #include <boost/mpl/vector_c.hpp> |
| 30 | |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 31 | #include <iostream> |
| 32 | |
Junxiao Shi | 770f904 | 2017-07-13 13:30:53 +0000 | [diff] [blame] | 33 | namespace ndn { |
| 34 | namespace tlv { |
| 35 | namespace tests { |
| 36 | |
| 37 | using namespace ndn::tests; |
| 38 | |
| 39 | template<size_t WIRE_SIZE> |
| 40 | struct ReadVarNumberTest; |
| 41 | |
| 42 | template<> |
| 43 | struct ReadVarNumberTest<1> |
| 44 | { |
| 45 | static const uint8_t WIRE[]; |
| 46 | static const uint64_t VALUE = 252; |
| 47 | }; |
| 48 | const uint8_t ReadVarNumberTest<1>::WIRE[] = {0xfc}; |
| 49 | |
| 50 | template<> |
| 51 | struct ReadVarNumberTest<3> |
| 52 | { |
| 53 | static const uint8_t WIRE[]; |
| 54 | static const uint64_t VALUE = 253; |
| 55 | }; |
| 56 | const uint8_t ReadVarNumberTest<3>::WIRE[] = {0xfd, 0x00, 0xfd}; |
| 57 | |
| 58 | template<> |
| 59 | struct ReadVarNumberTest<5> |
| 60 | { |
| 61 | static const uint8_t WIRE[]; |
| 62 | static const uint64_t VALUE = 65536; |
| 63 | }; |
| 64 | const uint8_t ReadVarNumberTest<5>::WIRE[] = {0xfe, 0x00, 0x01, 0x00, 0x00}; |
| 65 | |
| 66 | template<> |
| 67 | struct ReadVarNumberTest<9> |
| 68 | { |
| 69 | static const uint8_t WIRE[]; |
| 70 | static const uint64_t VALUE = 4294967296; |
| 71 | }; |
| 72 | const uint8_t ReadVarNumberTest<9>::WIRE[] = {0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}; |
| 73 | |
| 74 | template<size_t WIRE_SIZE, size_t ALIGNMENT_OFFSET> |
| 75 | struct ReadVarNumberAlignTest : public ReadVarNumberTest<WIRE_SIZE> |
| 76 | { |
| 77 | using AlignmentOffset = std::integral_constant<size_t, ALIGNMENT_OFFSET>; |
| 78 | |
| 79 | static_assert(sizeof(ReadVarNumberTest<WIRE_SIZE>::WIRE) == WIRE_SIZE, ""); |
| 80 | }; |
| 81 | |
| 82 | using ReadVarNumberTests = boost::mpl::vector< |
| 83 | ReadVarNumberAlignTest<1, 0>, |
| 84 | ReadVarNumberAlignTest<3, 0>, |
| 85 | ReadVarNumberAlignTest<3, 1>, |
| 86 | ReadVarNumberAlignTest<5, 0>, |
| 87 | ReadVarNumberAlignTest<5, 1>, |
| 88 | ReadVarNumberAlignTest<5, 2>, |
| 89 | ReadVarNumberAlignTest<5, 3>, |
| 90 | ReadVarNumberAlignTest<9, 0>, |
| 91 | ReadVarNumberAlignTest<9, 1>, |
| 92 | ReadVarNumberAlignTest<9, 2>, |
| 93 | ReadVarNumberAlignTest<9, 3>, |
| 94 | ReadVarNumberAlignTest<9, 4>, |
| 95 | ReadVarNumberAlignTest<9, 5>, |
| 96 | ReadVarNumberAlignTest<9, 6>, |
| 97 | ReadVarNumberAlignTest<9, 7> |
| 98 | >; |
| 99 | |
| 100 | // Benchmark of ndn::tlv::readVarNumber with different number lengths and alignments. |
Junxiao Shi | 770f904 | 2017-07-13 13:30:53 +0000 | [diff] [blame] | 101 | // For accurate results, it is required to compile ndn-cxx in release mode. |
| 102 | // It is recommended to run the benchmark multiple times and take the average. |
| 103 | BOOST_AUTO_TEST_CASE_TEMPLATE(ReadVarNumber, Test, ReadVarNumberTests) |
| 104 | { |
| 105 | const int N_ITERATIONS = 100000000; |
| 106 | |
| 107 | alignas(8) uint8_t buffer[16]; |
| 108 | static_assert(Test::AlignmentOffset::value + sizeof(Test::WIRE) <= sizeof(buffer), ""); |
| 109 | uint8_t* const begin = buffer + Test::AlignmentOffset::value; |
| 110 | std::memcpy(begin, Test::WIRE, sizeof(Test::WIRE)); |
| 111 | const uint8_t* const end = begin + sizeof(Test::WIRE); |
| 112 | |
| 113 | int nOks = 0; |
| 114 | int nCorrects = 0; |
| 115 | auto d = timedExecute([&] { |
| 116 | uint64_t number = 0; |
| 117 | for (int i = 0; i < N_ITERATIONS; ++i) { |
| 118 | const uint8_t* begin2 = begin; // make a copy because readVarNumber increments the pointer |
| 119 | nOks += readVarNumber(begin2, end, number); |
| 120 | nCorrects += number == Test::VALUE; |
| 121 | // use the number and the return value, so compiler won't optimize out their computation |
| 122 | } |
| 123 | }); |
| 124 | BOOST_CHECK_EQUAL(nOks, N_ITERATIONS); |
| 125 | BOOST_CHECK_EQUAL(nCorrects, N_ITERATIONS); |
| 126 | std::cout << "size=" << sizeof(Test::WIRE) |
| 127 | << " offset=" << Test::AlignmentOffset::value |
| 128 | << " " << d << std::endl; |
| 129 | } |
| 130 | |
| 131 | } // namespace tests |
| 132 | } // namespace tlv |
| 133 | } // namespace ndn |