blob: 1adb452dba909accc7cb1408bff1fda010425420 [file] [log] [blame]
Junxiao Shi770f9042017-07-13 13:30:53 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento21b7f1b2023-04-28 23:21:02 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Junxiao Shi770f9042017-07-13 13:30:53 +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
Junxiao Shi770f9042017-07-13 13:30:53 +000022#define BOOST_TEST_MODULE ndn-cxx Encoding Benchmark
Davide Pesavento7e780642018-11-24 15:51:34 -050023#include "tests/boost-test.hpp"
Junxiao Shi770f9042017-07-13 13:30:53 +000024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "ndn-cxx/encoding/tlv.hpp"
Davide Pesavento394977c2020-04-25 21:40:31 -040026#include "tests/benchmarks/timed-execute.hpp"
Davide Pesaventocdcde902017-08-23 15:40:22 -040027
Junxiao Shi770f9042017-07-13 13:30:53 +000028#include <boost/mpl/vector.hpp>
29#include <boost/mpl/vector_c.hpp>
30
Davide Pesaventocdcde902017-08-23 15:40:22 -040031#include <iostream>
32
Junxiao Shi770f9042017-07-13 13:30:53 +000033namespace ndn {
34namespace tlv {
35namespace tests {
36
37using namespace ndn::tests;
38
39template<size_t WIRE_SIZE>
40struct ReadVarNumberTest;
41
42template<>
43struct ReadVarNumberTest<1>
44{
Davide Pesavento21b7f1b2023-04-28 23:21:02 -040045 static constexpr uint8_t WIRE[] = {0xfc};
46 static constexpr uint64_t VALUE = 252;
Junxiao Shi770f9042017-07-13 13:30:53 +000047};
Junxiao Shi770f9042017-07-13 13:30:53 +000048
49template<>
50struct ReadVarNumberTest<3>
51{
Davide Pesavento21b7f1b2023-04-28 23:21:02 -040052 static constexpr uint8_t WIRE[] = {0xfd, 0x00, 0xfd};
53 static constexpr uint64_t VALUE = 253;
Junxiao Shi770f9042017-07-13 13:30:53 +000054};
Junxiao Shi770f9042017-07-13 13:30:53 +000055
56template<>
57struct ReadVarNumberTest<5>
58{
Davide Pesavento21b7f1b2023-04-28 23:21:02 -040059 static constexpr uint8_t WIRE[] = {0xfe, 0x00, 0x01, 0x00, 0x00};
60 static constexpr uint64_t VALUE = 65536;
Junxiao Shi770f9042017-07-13 13:30:53 +000061};
Junxiao Shi770f9042017-07-13 13:30:53 +000062
63template<>
64struct ReadVarNumberTest<9>
65{
Davide Pesavento21b7f1b2023-04-28 23:21:02 -040066 static constexpr uint8_t WIRE[] = {0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00};
67 static constexpr uint64_t VALUE = 4294967296;
Junxiao Shi770f9042017-07-13 13:30:53 +000068};
Junxiao Shi770f9042017-07-13 13:30:53 +000069
70template<size_t WIRE_SIZE, size_t ALIGNMENT_OFFSET>
71struct ReadVarNumberAlignTest : public ReadVarNumberTest<WIRE_SIZE>
72{
73 using AlignmentOffset = std::integral_constant<size_t, ALIGNMENT_OFFSET>;
74
75 static_assert(sizeof(ReadVarNumberTest<WIRE_SIZE>::WIRE) == WIRE_SIZE, "");
76};
77
78using ReadVarNumberTests = boost::mpl::vector<
79 ReadVarNumberAlignTest<1, 0>,
80 ReadVarNumberAlignTest<3, 0>,
81 ReadVarNumberAlignTest<3, 1>,
82 ReadVarNumberAlignTest<5, 0>,
83 ReadVarNumberAlignTest<5, 1>,
84 ReadVarNumberAlignTest<5, 2>,
85 ReadVarNumberAlignTest<5, 3>,
86 ReadVarNumberAlignTest<9, 0>,
87 ReadVarNumberAlignTest<9, 1>,
88 ReadVarNumberAlignTest<9, 2>,
89 ReadVarNumberAlignTest<9, 3>,
90 ReadVarNumberAlignTest<9, 4>,
91 ReadVarNumberAlignTest<9, 5>,
92 ReadVarNumberAlignTest<9, 6>,
93 ReadVarNumberAlignTest<9, 7>
94>;
95
96// Benchmark of ndn::tlv::readVarNumber with different number lengths and alignments.
Junxiao Shi770f9042017-07-13 13:30:53 +000097// For accurate results, it is required to compile ndn-cxx in release mode.
98// It is recommended to run the benchmark multiple times and take the average.
99BOOST_AUTO_TEST_CASE_TEMPLATE(ReadVarNumber, Test, ReadVarNumberTests)
100{
Davide Pesavento21b7f1b2023-04-28 23:21:02 -0400101 constexpr int N_ITERATIONS = 100000000;
Junxiao Shi770f9042017-07-13 13:30:53 +0000102
103 alignas(8) uint8_t buffer[16];
104 static_assert(Test::AlignmentOffset::value + sizeof(Test::WIRE) <= sizeof(buffer), "");
105 uint8_t* const begin = buffer + Test::AlignmentOffset::value;
106 std::memcpy(begin, Test::WIRE, sizeof(Test::WIRE));
107 const uint8_t* const end = begin + sizeof(Test::WIRE);
108
109 int nOks = 0;
110 int nCorrects = 0;
111 auto d = timedExecute([&] {
112 uint64_t number = 0;
113 for (int i = 0; i < N_ITERATIONS; ++i) {
114 const uint8_t* begin2 = begin; // make a copy because readVarNumber increments the pointer
115 nOks += readVarNumber(begin2, end, number);
116 nCorrects += number == Test::VALUE;
117 // use the number and the return value, so compiler won't optimize out their computation
118 }
119 });
120 BOOST_CHECK_EQUAL(nOks, N_ITERATIONS);
121 BOOST_CHECK_EQUAL(nCorrects, N_ITERATIONS);
122 std::cout << "size=" << sizeof(Test::WIRE)
123 << " offset=" << Test::AlignmentOffset::value
124 << " " << d << std::endl;
125}
126
127} // namespace tests
128} // namespace tlv
129} // namespace ndn