MountingTelemetryTest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include <chrono>
  8. #include <thread>
  9. #include <gtest/gtest.h>
  10. #include <react/mounting/MountingTelemetry.h>
  11. #include <react/utils/Telemetry.h>
  12. using namespace facebook::react;
  13. #define EXPECT_EQ_WITH_THRESHOLD(a, b, threshold) \
  14. EXPECT_TRUE((a >= b - threshold) && (a <= b + threshold))
  15. TEST(MountingTelemetryTest, timepoints) {
  16. auto threshold = int64_t{100};
  17. auto timepointA = telemetryTimePointNow();
  18. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  19. auto timepointB = telemetryTimePointNow();
  20. auto duration = telemetryDurationToMilliseconds(timepointB - timepointA);
  21. EXPECT_EQ_WITH_THRESHOLD(duration, 100, threshold);
  22. }
  23. TEST(MountingTelemetryTest, normalUseCase) {
  24. auto threshold = int64_t{100};
  25. auto telemetry = MountingTelemetry{};
  26. telemetry.willCommit();
  27. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  28. telemetry.willLayout();
  29. std::this_thread::sleep_for(std::chrono::milliseconds(200));
  30. telemetry.didLayout();
  31. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  32. telemetry.didCommit();
  33. std::this_thread::sleep_for(std::chrono::milliseconds(300));
  34. telemetry.willMount();
  35. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  36. telemetry.didMount();
  37. auto commitDuration = telemetryDurationToMilliseconds(
  38. telemetry.getCommitEndTime() - telemetry.getCommitStartTime());
  39. auto layoutDuration = telemetryDurationToMilliseconds(
  40. telemetry.getLayoutEndTime() - telemetry.getLayoutStartTime());
  41. auto mountDuration = telemetryDurationToMilliseconds(
  42. telemetry.getMountEndTime() - telemetry.getMountStartTime());
  43. EXPECT_EQ_WITH_THRESHOLD(commitDuration, 400, threshold * 2);
  44. EXPECT_EQ_WITH_THRESHOLD(layoutDuration, 200, threshold);
  45. EXPECT_EQ_WITH_THRESHOLD(mountDuration, 100, threshold);
  46. }
  47. TEST(MountingTelemetryTest, abnormalUseCases) {
  48. // Calling `did` before `will` should crash.
  49. EXPECT_DEATH_IF_SUPPORTED(
  50. {
  51. auto telemetry = MountingTelemetry{};
  52. telemetry.didDiff();
  53. },
  54. "diffStartTime_");
  55. EXPECT_DEATH_IF_SUPPORTED(
  56. {
  57. auto telemetry = MountingTelemetry{};
  58. telemetry.didCommit();
  59. },
  60. "commitStartTime_");
  61. EXPECT_DEATH_IF_SUPPORTED(
  62. {
  63. auto telemetry = MountingTelemetry{};
  64. telemetry.didMount();
  65. },
  66. "mountStartTime_");
  67. // Getting `start` *or* `end` timepoints before a pair of `will` and `did`
  68. // should crash.
  69. EXPECT_DEATH_IF_SUPPORTED(
  70. {
  71. auto telemetry = MountingTelemetry{};
  72. telemetry.willCommit();
  73. telemetry.getCommitStartTime();
  74. },
  75. "commitEndTime_");
  76. EXPECT_DEATH_IF_SUPPORTED(
  77. {
  78. auto telemetry = MountingTelemetry{};
  79. telemetry.willCommit();
  80. telemetry.getCommitEndTime();
  81. },
  82. "commitEndTime_");
  83. }