event_rate_counter.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_NUMERICS_EVENT_RATE_COUNTER_H_
  11. #define RTC_BASE_NUMERICS_EVENT_RATE_COUNTER_H_
  12. #include "rtc_base/numerics/sample_stats.h"
  13. namespace webrtc {
  14. // Calculates statistics based on events. For example for computing frame rates.
  15. // Note that it doesn't provide any running statistics or reset funcitonality,
  16. // so it's mostly useful for end of call statistics.
  17. class EventRateCounter {
  18. public:
  19. // Adds an event based on it's |event_time| for correct updates of the
  20. // interval statistics, each event must be added past the previous events.
  21. void AddEvent(Timestamp event_time);
  22. // Adds the events from |other|. Note that the interval stats won't be
  23. // recalculated, only merged, so this is not equivalent to if the events would
  24. // have been added to the same counter from the start.
  25. void AddEvents(EventRateCounter other);
  26. bool IsEmpty() const;
  27. // Average number of events per second. Defaults to 0 for no events and NAN
  28. // for one event.
  29. double Rate() const;
  30. SampleStats<TimeDelta>& interval() { return interval_; }
  31. TimeDelta TotalDuration() const;
  32. int Count() const { return event_count_; }
  33. private:
  34. Timestamp first_time_ = Timestamp::PlusInfinity();
  35. Timestamp last_time_ = Timestamp::MinusInfinity();
  36. int64_t event_count_ = 0;
  37. SampleStats<TimeDelta> interval_;
  38. };
  39. } // namespace webrtc
  40. #endif // RTC_BASE_NUMERICS_EVENT_RATE_COUNTER_H_