switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / PPI / RateAnalyzer.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief RateAnalyzer unit tests */
30
31 //#include "RateAnalyzer.test.hh"
32 //#include "RateAnalyzer.test.ih"
33
34 // Custom includes
35 #include "RateAnalyzer.hh"
36 #include "CloneSource.hh"
37 #include "RateFilter.hh"
38 #include "Setup.hh"
39
40 #include <senf/Utils/auto_unit_test.hh>
41 #include <boost/test/test_tools.hpp>
42 #include <boost/test/floating_point_comparison.hpp>
43
44 #define prefix_
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46
47 namespace {
48
49     unsigned calls (0u);
50     float ppss[] = { 13.333333f, 20.f, 13.333333f, 20.f, 13.333333f, 20.f };
51
52     float pps;
53     float bps;
54
55     void collectPPS(float packetsPerSecond)
56     {
57         // 2 + 3 + 2 + 3 + 2 + 3 packets / interval
58         // -> 13.333333 + 20. + 13.333333 + 20. + 13.333333 + 20. / 6 pps
59         // -> 16.666666 pps
60         // -> 216.66666 bps
61         // This test sometime fails when system load is to high ...
62         // BOOST_CHECK_CLOSE( packetsPerSecond, ppss[calls], 0.1f );
63         pps += packetsPerSecond;
64         ++ calls;
65         if (calls >= sizeof(ppss) / sizeof(ppss[0]))
66             senf::scheduler::terminate();
67     }
68
69     void collectBPS(float bytesPerSecond)
70     {
71         bps += bytesPerSecond;
72     }
73
74     void collectSize(float min, float avg, float max)
75     {
76         BOOST_CHECK_CLOSE(min, 13.f, 0.01f);
77         BOOST_CHECK_CLOSE(avg, 13.f, 0.01f);
78         BOOST_CHECK_CLOSE(max, 13.f, 0.01f);
79     }
80 }
81
82 SENF_AUTO_UNIT_TEST(rateAnalyzer)
83 {
84     char const * enabled (getenv("SENF_TIMING_CRITICAL_TESTS"));
85     if (! enabled) {
86         BOOST_WARN_MESSAGE(false, "Set SENF_TIMING_CRITICAL_TESTS to not skip timing critical tests");
87     }
88     senf::DataPacket p (senf::DataPacket::create(13u));
89     senf::ppi::module::CloneSource source (p);
90     senf::ppi::module::RateFilter filter (senf::ClockService::milliseconds(58u));
91     senf::ppi::module::RateAnalyzer analyzer;
92     analyzer.startStatistics(senf::ClockService::milliseconds(150u));
93     analyzer.signals.packetsPerSecond.connect(&collectPPS);
94     analyzer.signals.bytesPerSecond.connect(&collectBPS);
95     analyzer.signals.bytesPerPacket.connect(&collectSize);
96
97     senf::ppi::connect(source, filter);
98     senf::ppi::connect(filter, analyzer);
99
100     senf::ppi::run();
101
102     BOOST_CHECK_EQUAL( calls, 6u );
103
104     pps /= calls;
105     bps /= calls;
106
107     if (enabled) {
108         BOOST_CHECK_CLOSE( pps, 16.67f, .1f );
109         BOOST_CHECK_CLOSE( bps, 216.67f, .1f );
110     }
111 }
112
113 //-/////////////////////////////////////////////////////////////////////////////////////////////////
114 #undef prefix_
115
116 \f
117 // Local Variables:
118 // mode: c++
119 // fill-column: 100
120 // comment-column: 40
121 // c-file-style: "senf"
122 // indent-tabs-mode: nil
123 // ispell-local-dictionary: "american"
124 // compile-command: "scons -u test"
125 // End: