historical_latencies_container.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_NETWORK_TIME_HISTORICAL_LATENCIES_CONTAINER_H_
  5. #define COMPONENTS_NETWORK_TIME_HISTORICAL_LATENCIES_CONTAINER_H_
  6. #include "base/containers/ring_buffer.h"
  7. #include "base/time/time.h"
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. namespace network_time {
  10. // Number of historical latencies to record.
  11. constexpr int kMaxNumHistoricalLatencies = 16;
  12. // A class to record latencies of the previous `kNumHistoricalLatencies` time
  13. // fetches.
  14. class HistoricalLatenciesContainer {
  15. public:
  16. // Records a new latency in the container.
  17. void Record(base::TimeDelta latency);
  18. // Computes the standard deviation of the latest latencies. Returns nullopt
  19. // if not enough latencies have been recorded yet.
  20. absl::optional<base::TimeDelta> StdDeviation() const;
  21. private:
  22. base::RingBuffer<base::TimeDelta, size_t{kMaxNumHistoricalLatencies}>
  23. latencies_;
  24. };
  25. } // namespace network_time
  26. #endif // COMPONENTS_NETWORK_TIME_HISTORICAL_LATENCIES_CONTAINER_H_