blob: 8f503a0e474ce40c799be935ae7563b9b97a06ce [file] [log] [blame]
Junxiao Shi770f9042017-07-13 13:30:53 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento7e780642018-11-24 15:51:34 -05003 * Copyright (c) 2013-2018 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"
26#include "tests/integrated/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{
45 static const uint8_t WIRE[];
46 static const uint64_t VALUE = 252;
47};
48const uint8_t ReadVarNumberTest<1>::WIRE[] = {0xfc};
49
50template<>
51struct ReadVarNumberTest<3>
52{
53 static const uint8_t WIRE[];
54 static const uint64_t VALUE = 253;
55};
56const uint8_t ReadVarNumberTest<3>::WIRE[] = {0xfd, 0x00, 0xfd};
57
58template<>
59struct ReadVarNumberTest<5>
60{
61 static const uint8_t WIRE[];
62 static const uint64_t VALUE = 65536;
63};
64const uint8_t ReadVarNumberTest<5>::WIRE[] = {0xfe, 0x00, 0x01, 0x00, 0x00};
65
66template<>
67struct ReadVarNumberTest<9>
68{
69 static const uint8_t WIRE[];
70 static const uint64_t VALUE = 4294967296;
71};
72const uint8_t ReadVarNumberTest<9>::WIRE[] = {0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00};
73
74template<size_t WIRE_SIZE, size_t ALIGNMENT_OFFSET>
75struct 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
82using 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.
105BOOST_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