Size: 1572 bytes.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// cs/apps/trycopilot.ai/fps_counter.hh
// cs/apps/trycopilot.ai/fps_counter.hh
// cs/apps/trycopilot.ai/fps_counter.hh
#ifndef CS_APPS_TRYCOPILOT_AI_FPS_COUNTER_HH
#define CS_APPS_TRYCOPILOT_AI_FPS_COUNTER_HH
#include <ctime>
#include <deque>

namespace cs::apps::trycopilotai {
// Utility class that registers new frame events and
// computes the average frame rate
class AppFpsCounter {
 public:
  explicit AppFpsCounter(size_t fps)
      : frame_rate_fps_(fps) {}

  // Should be called to register a new frame rendering
  // event. Saves the timestamp this function was called to
  // a size-limited queue.
  void newFrame() {
    while (timestamps_queue_.size() > maxQueueSize()) {
      timestamps_queue_.pop_front();
    }

    timestamps_queue_.push_back(time(nullptr));
  }

  // Computes the FPS over the duration of the sliding
  // window
  float fps() const {
    if (timestamps_queue_.size() < 2) {
      return -1;
    }
    time_t oldest_timestamp_sec = timestamps_queue_.front();
    time_t newest_frame_timestamp_sec =
        timestamps_queue_.back();

    time_t deltaInSec =
        newest_frame_timestamp_sec - oldest_timestamp_sec;
    return static_cast<float>(timestamps_queue_.size()) /
           static_cast<float>(deltaInSec);
  }

 private:
  size_t maxQueueSize() const {
    return frame_rate_fps_ * MAX_SAMPLING_SECONDS;
  }

  static constexpr size_t MAX_SAMPLING_SECONDS = 5;
  size_t frame_rate_fps_ = 0;
  std::deque<time_t> timestamps_queue_;
};
}  // namespace cs::apps::trycopilotai

#endif  // CS_APPS_TRYCOPILOT_AI_FPS_COUNTER_HH
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 4ms.