File: bench-packets.cc

package info (click to toggle)
ns3 3.46-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 105,864 kB
  • sloc: cpp: 624,863; python: 14,863; ansic: 6,772; makefile: 1,950; sh: 987; javascript: 167; perl: 102
file content (401 lines) | stat: -rw-r--r-- 9,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * Copyright (c) 2006 INRIA
 *
 * SPDX-License-Identifier: GPL-2.0-only
 *
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
 */

// This program can be used to benchmark packet serialization/deserialization
// operations using Headers and Tags, for various numbers of packets 'n'
// Sample usage:  ./ns3 run 'bench-packets --n=10000'

#include "ns3/command-line.h"
#include "ns3/packet-metadata.h"
#include "ns3/packet.h"
#include "ns3/system-wall-clock-ms.h"

#include <algorithm>
#include <cstdlib> // for exit ()
#include <iostream>
#include <limits>
#include <sstream>
#include <string>

using namespace ns3;

/// BenchHeader class used for benchmarking packet serialization/deserialization
template <int N>
class BenchHeader : public Header
{
  public:
    BenchHeader();
    /**
     * Returns true if the header has been deserialized and the
     * deserialization was correct.  If Deserialize() has not yet been
     * called on the header, will return false.
     *
     * @returns true if success, false if failed or if deserialization not tried
     */
    bool IsOk() const;

    /**
     * Register this type.
     * @return The TypeId.
     */
    static TypeId GetTypeId();
    TypeId GetInstanceTypeId() const override;
    void Print(std::ostream& os) const override;
    uint32_t GetSerializedSize() const override;
    void Serialize(Buffer::Iterator start) const override;
    uint32_t Deserialize(Buffer::Iterator start) override;

  private:
    /**
     * Get type name function
     * @returns the type name string
     */
    static std::string GetTypeName();
    bool m_ok; ///< variable to track whether deserialization succeeded
};

template <int N>
BenchHeader<N>::BenchHeader()
    : m_ok(false)
{
}

template <int N>
bool
BenchHeader<N>::IsOk() const
{
    return m_ok;
}

template <int N>
std::string
BenchHeader<N>::GetTypeName()
{
    std::ostringstream oss;
    oss << "ns3::BenchHeader<" << N << ">";
    return oss.str();
}

template <int N>
TypeId
BenchHeader<N>::GetTypeId()
{
    static TypeId tid = TypeId(GetTypeName())
                            .SetParent<Header>()
                            .SetGroupName("Utils")
                            .HideFromDocumentation()
                            .AddConstructor<BenchHeader<N>>();
    return tid;
}

template <int N>
TypeId
BenchHeader<N>::GetInstanceTypeId() const
{
    return GetTypeId();
}

template <int N>
void
BenchHeader<N>::Print(std::ostream& os) const
{
    NS_ASSERT(false);
}

template <int N>
uint32_t
BenchHeader<N>::GetSerializedSize() const
{
    return N;
}

template <int N>
void
BenchHeader<N>::Serialize(Buffer::Iterator start) const
{
    start.WriteU8(N, N);
}

template <int N>
uint32_t
BenchHeader<N>::Deserialize(Buffer::Iterator start)
{
    m_ok = true;
    for (int i = 0; i < N; i++)
    {
        if (start.ReadU8() != N)
        {
            m_ok = false;
        }
    }
    return N;
}

/// BenchTag class used for benchmarking packet serialization/deserialization
template <int N>
class BenchTag : public Tag
{
  public:
    /**
     * Get the bench tag name.
     * @return the name.
     */
    static std::string GetName()
    {
        std::ostringstream oss;
        oss << "anon::BenchTag<" << N << ">";
        return oss.str();
    }

    /**
     * Register this type.
     * @return The TypeId.
     */
    static TypeId GetTypeId()
    {
        static TypeId tid = TypeId(GetName())
                                .SetParent<Tag>()
                                .SetGroupName("Utils")
                                .HideFromDocumentation()
                                .AddConstructor<BenchTag<N>>();
        return tid;
    }

    TypeId GetInstanceTypeId() const override
    {
        return GetTypeId();
    }

    uint32_t GetSerializedSize() const override
    {
        return N;
    }

    void Serialize(TagBuffer buf) const override
    {
        for (uint32_t i = 0; i < N; ++i)
        {
            buf.WriteU8(N);
        }
    }

    void Deserialize(TagBuffer buf) override
    {
        for (uint32_t i = 0; i < N; ++i)
        {
            buf.ReadU8();
        }
    }

    void Print(std::ostream& os) const override
    {
        os << "N=" << N;
    }

    BenchTag()
        : Tag()
    {
    }
};

static void
benchD(uint32_t n)
{
    BenchHeader<25> ipv4;
    BenchHeader<8> udp;
    BenchTag<16> tag1;
    BenchTag<17> tag2;

    for (uint32_t i = 0; i < n; i++)
    {
        Ptr<Packet> p = Create<Packet>(2000);
        p->AddPacketTag(tag1);
        p->AddHeader(udp);
        p->RemovePacketTag(tag1);
        p->AddPacketTag(tag2);
        p->AddHeader(ipv4);
        Ptr<Packet> o = p->Copy();
        o->RemoveHeader(ipv4);
        p->RemovePacketTag(tag2);
        o->RemoveHeader(udp);
    }
}

static void
benchA(uint32_t n)
{
    BenchHeader<25> ipv4;
    BenchHeader<8> udp;

    // The original version of this program did not use BenchHeader::IsOK ()
    // Below are two asserts that suggest how it can be used.
    NS_ASSERT_MSG(ipv4.IsOk() == false, "IsOk() should be false before deserialization");
    for (uint32_t i = 0; i < n; i++)
    {
        Ptr<Packet> p = Create<Packet>(2000);
        p->AddHeader(udp);
        p->AddHeader(ipv4);
        Ptr<Packet> o = p->Copy();
        o->RemoveHeader(ipv4);
        o->RemoveHeader(udp);
    }
    NS_ASSERT_MSG(ipv4.IsOk() == true, "IsOk() should be true after deserialization");
}

static void
benchB(uint32_t n)
{
    BenchHeader<25> ipv4;
    BenchHeader<8> udp;

    for (uint32_t i = 0; i < n; i++)
    {
        Ptr<Packet> p = Create<Packet>(2000);
        p->AddHeader(udp);
        p->AddHeader(ipv4);
    }
}

static void
C2(Ptr<Packet> p)
{
    BenchHeader<8> udp;

    p->RemoveHeader(udp);
}

static void
C1(Ptr<Packet> p)
{
    BenchHeader<25> ipv4;
    p->RemoveHeader(ipv4);
    C2(p);
}

static void
benchC(uint32_t n)
{
    BenchHeader<25> ipv4;
    BenchHeader<8> udp;

    for (uint32_t i = 0; i < n; i++)
    {
        Ptr<Packet> p = Create<Packet>(2000);
        p->AddHeader(udp);
        p->AddHeader(ipv4);
        C1(p);
    }
}

static void
benchFragment(uint32_t n)
{
    BenchHeader<25> ipv4;
    BenchHeader<8> udp;

    for (uint32_t i = 0; i < n; i++)
    {
        Ptr<Packet> p = Create<Packet>(2000);
        p->AddHeader(udp);
        p->AddHeader(ipv4);

        Ptr<Packet> frag0 = p->CreateFragment(0, 250);
        Ptr<Packet> frag1 = p->CreateFragment(250, 250);
        Ptr<Packet> frag2 = p->CreateFragment(500, 500);
        Ptr<Packet> frag3 = p->CreateFragment(1000, 500);
        Ptr<Packet> frag4 = p->CreateFragment(1500, 500);

        /* Mix fragments in different order */
        frag2->AddAtEnd(frag3);
        frag4->AddAtEnd(frag1);
        frag2->AddAtEnd(frag4);
        frag0->AddAtEnd(frag2);

        frag0->RemoveHeader(ipv4);
        frag0->RemoveHeader(udp);
    }
}

static void
benchByteTags(uint32_t n)
{
    for (uint32_t i = 0; i < n; i++)
    {
        Ptr<Packet> p = Create<Packet>(2000);
        for (uint32_t j = 0; j < 100; j++)
        {
            BenchTag<0> tag;
            p->AddByteTag(tag);
        }
        Ptr<Packet> q = Create<Packet>(1000);

        // This should trigger adjustment of all byte tags
        q->AddAtEnd(p);
    }
}

static uint64_t
runBenchOneIteration(void (*bench)(uint32_t), uint32_t n)
{
    SystemWallClockMs time;
    time.Start();
    (*bench)(n);
    uint64_t deltaMs = time.End();
    return deltaMs;
}

static void
runBench(void (*bench)(uint32_t), uint32_t n, uint32_t minIterations, const char* name)
{
    uint64_t minDelay = std::numeric_limits<uint64_t>::max();
    for (uint32_t i = 0; i < minIterations; i++)
    {
        uint64_t delay = runBenchOneIteration(bench, n);
        minDelay = std::min(minDelay, delay);
    }
    double ps = n;
    ps *= 1000;
    ps /= minDelay;
    std::cout << ps << " packets/s"
              << " (" << minDelay << " ms elapsed)\t" << name << std::endl;
}

int
main(int argc, char* argv[])
{
    uint32_t n = 0;
    uint32_t minIterations = 1;
    bool enablePrinting = false;

    CommandLine cmd(__FILE__);
    cmd.Usage("Benchmark Packet class");
    cmd.AddValue("n", "number of iterations", n);
    cmd.AddValue("min-iterations",
                 "number of subiterations to minimize iteration time over",
                 minIterations);
    cmd.AddValue("enable-printing", "enable packet printing", enablePrinting);
    cmd.Parse(argc, argv);

    if (n == 0)
    {
        std::cerr << "Error-- number of packets must be specified "
                  << "by command-line argument --n=(number of packets)" << std::endl;
        exit(1);
    }
    std::cout << "Running bench-packets with n=" << n << std::endl;
    std::cout << "All tests begin by adding UDP and IPv4 headers." << std::endl;

    runBench(&benchA, n, minIterations, "Copy packet, remove headers");
    runBench(&benchB, n, minIterations, "Just add headers");
    runBench(&benchC, n, minIterations, "Remove by func call");
    runBench(&benchD, n, minIterations, "Intermixed add/remove headers and tags");
    runBench(&benchFragment, n, minIterations, "Fragmentation and concatenation");
    runBench(&benchByteTags, n, minIterations, "Benchmark byte tags");

    return 0;
}