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 | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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" |
| 26 | #include "tests/integrated/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. |
| 101 | // Run this benchmark with: |
| 102 | // ./encoding-benchmark -t 'ReadVarNumber*' |
| 103 | // For accurate results, it is required to compile ndn-cxx in release mode. |
| 104 | // It is recommended to run the benchmark multiple times and take the average. |
| 105 | BOOST_AUTO_TEST_CASE_TEMPLATE(ReadVarNumber, Test, ReadVarNumberTests) |
| 106 | { |
| 107 | const int N_ITERATIONS = 100000000; |
| 108 | |
| 109 | alignas(8) uint8_t buffer[16]; |
| 110 | static_assert(Test::AlignmentOffset::value + sizeof(Test::WIRE) <= sizeof(buffer), ""); |
| 111 | uint8_t* const begin = buffer + Test::AlignmentOffset::value; |
| 112 | std::memcpy(begin, Test::WIRE, sizeof(Test::WIRE)); |
| 113 | const uint8_t* const end = begin + sizeof(Test::WIRE); |
| 114 | |
| 115 | int nOks = 0; |
| 116 | int nCorrects = 0; |
| 117 | auto d = timedExecute([&] { |
| 118 | uint64_t number = 0; |
| 119 | for (int i = 0; i < N_ITERATIONS; ++i) { |
| 120 | const uint8_t* begin2 = begin; // make a copy because readVarNumber increments the pointer |
| 121 | nOks += readVarNumber(begin2, end, number); |
| 122 | nCorrects += number == Test::VALUE; |
| 123 | // use the number and the return value, so compiler won't optimize out their computation |
| 124 | } |
| 125 | }); |
| 126 | BOOST_CHECK_EQUAL(nOks, N_ITERATIONS); |
| 127 | BOOST_CHECK_EQUAL(nCorrects, N_ITERATIONS); |
| 128 | std::cout << "size=" << sizeof(Test::WIRE) |
| 129 | << " offset=" << Test::AlignmentOffset::value |
| 130 | << " " << d << std::endl; |
| 131 | } |
| 132 | |
| 133 | } // namespace tests |
| 134 | } // namespace tlv |
| 135 | } // namespace ndn |