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