File indexing completed on 2025-01-05 04:37:31

0001 /*
0002     SPDX-FileCopyrightText: 2010 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QObject>
0008 #include <QRandomGenerator>
0009 #include <QtTest>
0010 
0011 #include <ctime>
0012 
0013 #include <boost/scoped_array.hpp>
0014 
0015 #include <util/functions.h>
0016 #include <util/log.h>
0017 #include <utp/delaywindow.h>
0018 
0019 using namespace utp;
0020 using namespace bt;
0021 
0022 class DelayWindowTest : public QObject
0023 {
0024     Q_OBJECT
0025 public:
0026     DelayWindowTest(QObject *parent = nullptr)
0027         : QObject(parent)
0028     {
0029     }
0030 
0031 private Q_SLOTS:
0032     void initTestCase()
0033     {
0034         bt::InitLog("delaywindowtest.log", false, true);
0035     }
0036 
0037     void cleanupTestCase()
0038     {
0039     }
0040 
0041     void testWindow()
0042     {
0043         bt::Uint32 base_delay = MAX_DELAY;
0044         DelayWindow wnd;
0045 
0046         for (int i = 0; i < 100; i++) {
0047             bt::Uint32 val = QRandomGenerator::global()->generate();
0048             Header hdr;
0049             hdr.timestamp_difference_microseconds = val;
0050             if (val < base_delay)
0051                 base_delay = val;
0052 
0053             bt::Uint32 ret = wnd.update(&hdr, bt::Now());
0054             QVERIFY(ret == base_delay);
0055         }
0056     }
0057 #if 1
0058     void testPerformance()
0059     {
0060         const int SAMPLE_COUNT = 2400000;
0061 
0062         boost::scoped_array<bt::Uint32> delay_samples(new bt::Uint32[SAMPLE_COUNT]);
0063         for (int i = 0; i < SAMPLE_COUNT; i++)
0064             delay_samples[i] = QRandomGenerator::global()->bounded(1000000);
0065 
0066         boost::scoped_array<bt::Uint32> returned_delay_new(new bt::Uint32[SAMPLE_COUNT]);
0067         boost::scoped_array<bt::Uint32> returned_delay_old(new bt::Uint32[SAMPLE_COUNT]);
0068         boost::scoped_array<bt::Uint32> returned_delay_circular(new bt::Uint32[SAMPLE_COUNT]);
0069 
0070         {
0071             DelayWindow wnd;
0072             bt::TimeStamp start = bt::Now();
0073             for (int i = 0; i < SAMPLE_COUNT; i++) {
0074                 Header hdr;
0075                 hdr.timestamp_difference_microseconds = delay_samples[i];
0076                 returned_delay_new[i] = wnd.update(&hdr, i);
0077             }
0078             bt::TimeStamp duration = bt::Now() - start;
0079             Out(SYS_GEN | LOG_DEBUG) << "New algorithm took: " << duration << endl;
0080         }
0081     }
0082 #endif
0083 
0084     void testTimeout()
0085     {
0086         DelayWindow wnd;
0087 
0088         Header hdr;
0089         hdr.timestamp_difference_microseconds = 1000;
0090         bt::TimeStamp ts = 1000;
0091         QVERIFY(wnd.update(&hdr, ts) == 1000);
0092 
0093         hdr.timestamp_difference_microseconds = 2000;
0094         QVERIFY(wnd.update(&hdr, ts + 1000) == 1000);
0095 
0096         // Now simulate timeout, oldest must get removed
0097         hdr.timestamp_difference_microseconds = 3000;
0098         QVERIFY(wnd.update(&hdr, ts + utp::DELAY_WINDOW_SIZE + 1) == 2000);
0099     }
0100 
0101 private:
0102 };
0103 
0104 QTEST_MAIN(DelayWindowTest)
0105 
0106 #include "delaywindowtest.moc"