Whitespce cleanup: Remove whitespace at end-on-line, remove tabs, wrap
[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 "PPI.hh"
32
33 #include <senf/Utils/auto_unit_test.hh>
34 #include <boost/test/test_tools.hpp>
35 #include <boost/test/floating_point_comparison.hpp>
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 namespace {
41
42     unsigned calls (0u);
43     float ppss[] = { 13.333333f, 20.f, 13.333333f, 20.f, 13.333333f, 20.f };
44
45     float pps;
46     float bps;
47
48     void collectPPS(float packetsPerSecond)
49     {
50         // 2 + 3 + 2 + 3 + 2 + 3 packets / interval
51         // -> 13.333333 + 20. + 13.333333 + 20. + 13.333333 + 20. / 6 pps
52         // -> 16.666666 pps
53         // -> 216.66666 bps
54         // This test sometime fails when system load is to high ...
55         // BOOST_CHECK_CLOSE( packetsPerSecond, ppss[calls], 0.1f );
56         pps += packetsPerSecond;
57         ++ calls;
58         if (calls >= sizeof(ppss) / sizeof(ppss[0]))
59             senf::scheduler::terminate();
60     }
61
62     void collectBPS(float bytesPerSecond)
63     {
64         bps += bytesPerSecond;
65     }
66
67     void collectSize(float min, float avg, float max)
68     {
69         BOOST_CHECK_CLOSE(min, 13.f, 0.01f);
70         BOOST_CHECK_CLOSE(avg, 13.f, 0.01f);
71         BOOST_CHECK_CLOSE(max, 13.f, 0.01f);
72     }
73 }
74
75 SENF_AUTO_UNIT_TEST(rateAnalyzer)
76 {
77     char const * enabled (getenv("SENF_TIMING_CRITICAL_TESTS"));
78     if (! enabled) {
79         BOOST_WARN_MESSAGE(false, "Set SENF_TIMING_CRITICAL_TESTS to not skip timing critical tests");
80     }
81     senf::DataPacket p (senf::DataPacket::create(13u));
82     senf::ppi::module::CloneSource source (p);
83     senf::ppi::module::RateFilter filter (senf::ClockService::milliseconds(58u));
84     senf::ppi::module::RateAnalyzer analyzer;
85     analyzer.startStatistics(senf::ClockService::milliseconds(150u));
86     analyzer.signals.packetsPerSecond.connect(&collectPPS);
87     analyzer.signals.bytesPerSecond.connect(&collectBPS);
88     analyzer.signals.bytesPerPacket.connect(&collectSize);
89
90     senf::ppi::connect(source, filter);
91     senf::ppi::connect(filter, analyzer);
92
93     senf::ppi::run();
94
95     BOOST_CHECK_EQUAL( calls, 6u );
96
97     pps /= calls;
98     bps /= calls;
99
100     if (enabled) {
101         BOOST_CHECK_CLOSE( pps, 16.67f, .1f );
102         BOOST_CHECK_CLOSE( bps, 216.67f, .1f );
103     }
104 }
105
106 ///////////////////////////////cc.e////////////////////////////////////////
107 #undef prefix_
108
109 \f
110 // Local Variables:
111 // mode: c++
112 // fill-column: 100
113 // comment-column: 40
114 // c-file-style: "senf"
115 // indent-tabs-mode: nil
116 // ispell-local-dictionary: "american"
117 // compile-command: "scons -u test"
118 // End: