Ocean
RateCalculator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_MATH_RATE_CALCUALTOR_H
9 #define META_OCEAN_MATH_RATE_CALCUALTOR_H
10 
11 #include "ocean/math/Math.h"
12 
13 #include "ocean/base/Timestamp.h"
14 
15 namespace Ocean
16 {
17 
18 /**
19  * This class implements a calculate for rates like frame rates.
20  * The rates are determined with a sliding windows.<br>
21  * The class is thread-safe.
22  * @ingroup math
23  */
24 class OCEAN_MATH_EXPORT RateCalculator
25 {
26  protected:
27 
28  /**
29  * Definition of an ordered map mapping timestamps to quantities.
30  */
31  typedef std::map<Timestamp, double> TimestampMap;
32 
33  public:
34 
35  /**
36  * Creates a new rate calculator object.
37  * @param window The size of the sliding window, in seconds, with range (0, infinity)
38  */
39  explicit inline RateCalculator(const double window = 1.0);
40 
41  /**
42  * Adds another occurrence (e.g., a new frame has been processed).
43  * @param timestamp The timestamp at which the occurrence happened, must be valid
44  * @param quantity The quantity of the current occurrence
45  */
46  void addOccurance(const Timestamp& timestamp, const double quantity = 1.0);
47 
48  /**
49  * Returns the current rate.
50  * @param timestamp The timestamp at which the current rate will be determined, must be valid
51  * @return The current rate, with range [0, infinity)
52  */
53  double rate(const Timestamp& timestamp) const;
54 
55  /**
56  * Returns the current rate only every n-th second.
57  * @param rateTimestamp The timestamp at which the current rate will be determined, must be valid
58  * @param rate The resulting current rate, with range [0, infinity)
59  * @param interval The number of seconds necessary since the last successful call of this function, with range [0, infinity)
60  * @param requestTimestamp Optional an explicit timestamp when the request happens, e.g., in display time, invalid to use the timestamp for the rate
61  * @return True, if the rate was determined; False, if the interval was not yet reached
62  */
63  bool rateEveryNSeconds(const Timestamp& rateTimestamp, double& rate, const double interval = 1.0, const Timestamp& requestTimestamp = Timestamp(false));
64 
65  /**
66  * Updates the window of this rate calculator.
67  * @param window The window to be set, in seconds, with range [0, infinity)
68  */
69  inline void setWindow(const double window);
70 
71  /**
72  * Returns the window of this rate calculator.
73  * @return The rate calculator's window, in seconds, with range [0, infinity)
74  */
75  inline double window() const;
76 
77  /**
78  * Clears the rate calculator e.g., to start with a completely new measurement
79  */
80  inline void clear();
81 
82  protected:
83 
84  /// The size of the sliding window, in seconds, with range (0, infinity).
85  double window_ = 1.0;
86 
87  /// The map mapping timestamps to quantities.
89 
90  /// The timestamp at which the rate has been sucessfully requested the last time when calling rateEveryNSeconds().
92 
93  /// The lock of this object.
94  mutable Lock lock_;
95 };
96 
97 inline RateCalculator::RateCalculator(const double window) :
98  window_(window),
99  lastRequestTimestamp_(false)
100 {
101  ocean_assert(window_ > 0.0);
102 }
103 
104 inline void RateCalculator::setWindow(const double window)
105 {
106  ocean_assert(window > 0.0);
107 
108  const ScopedLock scopedLock(lock_);
109 
110  window_ = window;
111 }
112 
113 inline double RateCalculator::window() const
114 {
115  const ScopedLock scopedLock(lock_);
116 
117  return window_;
118 }
119 
121 {
122  const ScopedLock scopedLock(lock_);
123 
124  timestampMap_.clear();
126 }
127 
128 }
129 
130 #endif // META_OCEAN_MATH_RATE_CALCUALTOR_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a calculate for rates like frame rates.
Definition: RateCalculator.h:25
double window() const
Returns the window of this rate calculator.
Definition: RateCalculator.h:113
RateCalculator(const double window=1.0)
Creates a new rate calculator object.
Definition: RateCalculator.h:97
void setWindow(const double window)
Updates the window of this rate calculator.
Definition: RateCalculator.h:104
double window_
The size of the sliding window, in seconds, with range (0, infinity).
Definition: RateCalculator.h:85
std::map< Timestamp, double > TimestampMap
Definition of an ordered map mapping timestamps to quantities.
Definition: RateCalculator.h:31
void clear()
Clears the rate calculator e.g., to start with a completely new measurement.
Definition: RateCalculator.h:120
bool rateEveryNSeconds(const Timestamp &rateTimestamp, double &rate, const double interval=1.0, const Timestamp &requestTimestamp=Timestamp(false))
Returns the current rate only every n-th second.
Lock lock_
The lock of this object.
Definition: RateCalculator.h:94
void addOccurance(const Timestamp &timestamp, const double quantity=1.0)
Adds another occurrence (e.g., a new frame has been processed).
double rate(const Timestamp &timestamp) const
Returns the current rate.
TimestampMap timestampMap_
The map mapping timestamps to quantities.
Definition: RateCalculator.h:88
Timestamp lastRequestTimestamp_
The timestamp at which the rate has been sucessfully requested the last time when calling rateEveryNS...
Definition: RateCalculator.h:91
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
This class implements a timestamp.
Definition: Timestamp.h:36
Timestamp & toInvalid()
Sets the timestamp to invalid.
Definition: Timestamp.h:276
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15