Files
react-native/ReactCommon/react/renderer/mounting/tests/TransactionTelemetryTest.cpp
T
Valentin Shergin 60f15d6b5d Tracking time spent on measuring text in TransactionTelemetry
Summary:
Now we not only measure how many times we measured text but also measure how much time it takes. This way we can see which portion of the layout process is spent by layout itself (and measuring embedded components).

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: mdvacca

Differential Revision: D26827447

fbshipit-source-id: e0b09fcacc86aed50dd94b48458215adbb0a60ef
2021-03-11 14:34:22 -08:00

138 lines
3.8 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <chrono>
#include <thread>
#include <gtest/gtest.h>
#include <react/renderer/mounting/TransactionTelemetry.h>
#include <react/utils/Telemetry.h>
using namespace facebook::react;
#define EXPECT_EQ_WITH_THRESHOLD(a, b, threshold) \
EXPECT_TRUE((a >= b - threshold) && (a <= b + threshold))
template <typename ClockT>
void sleep(double durationInSeconds) {
auto timepoint = ClockT::now() +
std::chrono::milliseconds((long long)(durationInSeconds * 1000));
while (ClockT::now() < timepoint) {
}
}
TEST(TransactionTelemetryTest, timepoints) {
auto threshold = int64_t{70};
auto timepointA = telemetryTimePointNow();
sleep<TelemetryClock>(0.1);
auto timepointB = telemetryTimePointNow();
auto duration = telemetryDurationToMilliseconds(timepointB - timepointA);
EXPECT_EQ_WITH_THRESHOLD(duration, 100, threshold);
}
TEST(TransactionTelemetryTest, normalUseCase) {
auto threshold = int64_t{70};
auto telemetry = TransactionTelemetry{};
telemetry.setAsThreadLocal();
telemetry.willCommit();
sleep<TelemetryClock>(0.1);
telemetry.willLayout();
sleep<TelemetryClock>(0.2);
TransactionTelemetry::threadLocalTelemetry()->willMeasureText();
sleep<TelemetryClock>(0.1);
TransactionTelemetry::threadLocalTelemetry()->didMeasureText();
TransactionTelemetry::threadLocalTelemetry()->willMeasureText();
sleep<TelemetryClock>(0.2);
TransactionTelemetry::threadLocalTelemetry()->didMeasureText();
TransactionTelemetry::threadLocalTelemetry()->willMeasureText();
sleep<TelemetryClock>(0.3);
TransactionTelemetry::threadLocalTelemetry()->didMeasureText();
telemetry.didLayout();
sleep<TelemetryClock>(0.1);
telemetry.didCommit();
telemetry.setRevisionNumber(42);
telemetry.unsetAsThreadLocal();
sleep<TelemetryClock>(0.3);
telemetry.willMount();
sleep<TelemetryClock>(0.1);
telemetry.didMount();
auto commitDuration = telemetryDurationToMilliseconds(
telemetry.getCommitEndTime() - telemetry.getCommitStartTime());
auto layoutDuration = telemetryDurationToMilliseconds(
telemetry.getLayoutEndTime() - telemetry.getLayoutStartTime());
auto mountDuration = telemetryDurationToMilliseconds(
telemetry.getMountEndTime() - telemetry.getMountStartTime());
EXPECT_EQ_WITH_THRESHOLD(commitDuration, 1000, threshold);
EXPECT_EQ_WITH_THRESHOLD(layoutDuration, 800, threshold);
EXPECT_EQ_WITH_THRESHOLD(mountDuration, 100, threshold);
EXPECT_EQ(telemetry.getNumberOfTextMeasurements(), 3);
EXPECT_EQ_WITH_THRESHOLD(
telemetryDurationToMilliseconds(telemetry.getTextMeasureTime()),
600,
threshold);
EXPECT_EQ(telemetry.getRevisionNumber(), 42);
}
TEST(TransactionTelemetryTest, abnormalUseCases) {
// Calling `did` before `will` should crash.
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = TransactionTelemetry{};
telemetry.didDiff();
},
"diffStartTime_");
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = TransactionTelemetry{};
telemetry.didCommit();
},
"commitStartTime_");
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = TransactionTelemetry{};
telemetry.didMount();
},
"mountStartTime_");
// Getting `start` *or* `end` timepoints before a pair of `will` and `did`
// should crash.
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = TransactionTelemetry{};
telemetry.willCommit();
telemetry.getCommitStartTime();
},
"commitEndTime_");
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = TransactionTelemetry{};
telemetry.willCommit();
telemetry.getCommitEndTime();
},
"commitEndTime_");
}