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