Fawkes API Fawkes Development Version
syncpoint_call_stats.cpp
1/***************************************************************************
2 * syncpoint_call_stats.cpp - Utility class to keep track of SP call stats
3 *
4 * Created: Fri Aug 15 16:17:42 2014
5 * Copyright 2014 Till Hofmann
6 *
7 ****************************************************************************/
8
9/* This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Library General Public License for more details.
18 *
19 * Read the full text in the LICENSE.GPL file in the doc directory.
20 */
21
22#include <syncpoint/syncpoint_call_stats.h>
23
24namespace fawkes {
25
26/** @class SyncPointCallStats <syncpoint/syncpoint_call_stats.h>
27 * This class represents call stats of a single component to a single SyncPoint.
28 * It keeps track of the first and last call and computes the call frequency.
29 *
30 * @see SyncPoint
31 * @see SyncPointCall
32 *
33 */
34
35/** Constructor. */
37: first_call_(TIME_MAX), last_call_(TIME_MIN), total_wait_time_(Time(0.f)), num_calls_(0)
38{
39}
40
41/** Add a call to the stats.
42 * Update the first and last call and increment the call counter
43 * @param new_call the time of the call
44 * @param wait_time the time the caller had to wait, 0 for emit()
45 */
46void
47SyncPointCallStats::update_calls(const Time &new_call, const Time &wait_time)
48{
49 num_calls_++;
50 total_wait_time_ += wait_time;
51 if (new_call < first_call_) {
52 first_call_ = new_call;
53 }
54 if (new_call > last_call_) {
55 last_call_ = new_call;
56 }
57}
58
59/** Add a call to the stats.
60 * @param call the new call
61 */
62void
64{
66}
67
68/** Get the first call to the SyncPoint by the component
69 * @return The time of the first call
70 */
71Time
73{
74 return first_call_;
75}
76
77/** Get the last call to the SyncPoint by the component
78 * @return The time of the last call
79 */
80Time
82{
83 return last_call_;
84}
85
86/** Get the call frequency. This is calculated using the first and last call
87 * and the number of calls
88 * @return the call frequency
89 */
90float
92{
93 if (num_calls_ <= 1) {
94 return 0.f;
95 }
96 return num_calls_ / (last_call_.in_sec() - first_call_.in_sec());
97}
98
99/** Get the average wait time. For emit calls, this is 0.
100 * @return average wait time
101 */
102float
104{
105 return total_wait_time_.in_sec() / num_calls_;
106}
107
108/** Get total number of calls.
109 * @return the total number of calls
110 */
111unsigned int
113{
114 return num_calls_;
115}
116
117} // namespace fawkes
float get_call_frequency() const
Get the call frequency.
void update_calls(const Time &new_call, const Time &wait_time=Time(0.f))
Add a call to the stats.
Time get_first_call() const
Get the first call to the SyncPoint by the component.
float get_waittime_average() const
Get the average wait time.
unsigned int get_num_calls() const
Get total number of calls.
Time get_last_call() const
Get the last call to the SyncPoint by the component.
A call (wait() or emit()) to a SyncPoint.
Time get_call_time() const
Get the time when the call was made.
Time get_wait_time() const
Get the wait time.
A class for handling time.
Definition: time.h:93
double in_sec() const
Convet time to seconds.
Definition: time.cpp:219
Fawkes library namespace.
const Time TIME_MAX
Instance of Time denoting the maximum value possible.
Definition: time.cpp:43
const Time TIME_MIN
Instance of Time denoting the minimum value possible.
Definition: time.cpp:48