Telemetry.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) Facebook, Inc. and its 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. #pragma once
  8. #include <chrono>
  9. namespace facebook {
  10. namespace react {
  11. /*
  12. * Represents a monotonic clock suitable for measuring intervals.
  13. */
  14. using TelemetryClock = std::chrono::steady_clock;
  15. /*
  16. * Represents a point in time satisfied the requirements of TelemetryClock.
  17. */
  18. using TelemetryTimePoint = TelemetryClock::time_point;
  19. /*
  20. * Represents a time interval satisfied the requirements of TelemetryClock.
  21. */
  22. using TelemetryDuration = std::chrono::nanoseconds;
  23. /*
  24. * Represents a time point which never happens.
  25. */
  26. static TelemetryTimePoint const kTelemetryUndefinedTimePoint =
  27. TelemetryTimePoint::max();
  28. /*
  29. * Returns a time point representing the current point in time.
  30. */
  31. static inline TelemetryTimePoint telemetryTimePointNow() {
  32. return TelemetryClock::now();
  33. }
  34. /*
  35. * Returns a number of milliseconds that passed from some epoch starting time
  36. * point to a given time point. The epoch starting time point is not specified
  37. * but stays the same for an application run.
  38. */
  39. static inline int64_t telemetryTimePointToMilliseconds(
  40. TelemetryTimePoint timePoint) {
  41. return std::chrono::duration_cast<std::chrono::milliseconds>(
  42. timePoint - TelemetryTimePoint{})
  43. .count();
  44. }
  45. /*
  46. * Returns a number of milliseconds that represents the given duration object.
  47. */
  48. static inline int64_t telemetryDurationToMilliseconds(
  49. TelemetryDuration duration) {
  50. return std::chrono::duration_cast<std::chrono::milliseconds>(duration)
  51. .count();
  52. }
  53. } // namespace react
  54. } // namespace facebook