b1d3e7ceef27efb004185eb3b52445daf4a45ee1
[senf.git] / senf / PPI / RateAnalyzer.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief RateAnalyzer unit tests */
25
26 //#include "RateAnalyzer.test.hh"
27 //#include "RateAnalyzer.test.ih"
28
29 // Custom includes
30 #include "RateAnalyzer.hh"
31 #include "CloneSource.hh"
32 #include "RateFilter.hh"
33 #include "Setup.hh"
34
35 #include <senf/Utils/auto_unit_test.hh>
36 #include <boost/test/test_tools.hpp>
37 #include <boost/test/floating_point_comparison.hpp>
38
39 #define prefix_
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 namespace {
43
44     unsigned calls (0u);
45     float ppss[] = { 13.333333f, 20.f, 13.333333f, 20.f, 13.333333f, 20.f };
46
47     float pps;
48     float bps;
49
50     void collectPPS(float packetsPerSecond)
51     {
52         // 2 + 3 + 2 + 3 + 2 + 3 packets / interval
53         // -> 13.333333 + 20. + 13.333333 + 20. + 13.333333 + 20. / 6 pps
54         // -> 16.666666 pps
55         // -> 216.66666 bps
56         // This test sometime fails when system load is to high ...
57         // BOOST_CHECK_CLOSE( packetsPerSecond, ppss[calls], 0.1f );
58         pps += packetsPerSecond;
59         ++ calls;
60         if (calls >= sizeof(ppss) / sizeof(ppss[0]))
61             senf::scheduler::terminate();
62     }
63
64     void collectBPS(float bytesPerSecond)
65     {
66         bps += bytesPerSecond;
67     }
68
69     void collectSize(float min, float avg, float max)
70     {
71         BOOST_CHECK_CLOSE(min, 13.f, 0.01f);
72         BOOST_CHECK_CLOSE(avg, 13.f, 0.01f);
73         BOOST_CHECK_CLOSE(max, 13.f, 0.01f);
74     }
75 }
76
77 SENF_AUTO_UNIT_TEST(rateAnalyzer)
78 {
79     char const * enabled (getenv("SENF_TIMING_CRITICAL_TESTS"));
80     if (! enabled) {
81         BOOST_WARN_MESSAGE(false, "Set SENF_TIMING_CRITICAL_TESTS to not skip timing critical tests");
82     }
83     senf::DataPacket p (senf::DataPacket::create(13u));
84     senf::ppi::module::CloneSource source (p);
85     senf::ppi::module::RateFilter filter (senf::ClockService::milliseconds(58u));
86     senf::ppi::module::RateAnalyzer analyzer;
87     analyzer.startStatistics(senf::ClockService::milliseconds(150u));
88     analyzer.signals.packetsPerSecond.connect(&collectPPS);
89     analyzer.signals.bytesPerSecond.connect(&collectBPS);
90     analyzer.signals.bytesPerPacket.connect(&collectSize);
91
92     senf::ppi::connect(source, filter);
93     senf::ppi::connect(filter, analyzer);
94
95     senf::ppi::run();
96
97     BOOST_CHECK_EQUAL( calls, 6u );
98
99     pps /= calls;
100     bps /= calls;
101
102     if (enabled) {
103         BOOST_CHECK_CLOSE( pps, 16.67f, .1f );
104         BOOST_CHECK_CLOSE( bps, 216.67f, .1f );
105     }
106 }
107
108 //-/////////////////////////////////////////////////////////////////////////////////////////////////
109 #undef prefix_
110
111 \f
112 // Local Variables:
113 // mode: c++
114 // fill-column: 100
115 // comment-column: 40
116 // c-file-style: "senf"
117 // indent-tabs-mode: nil
118 // ispell-local-dictionary: "american"
119 // compile-command: "scons -u test"
120 // End: