Files
react-native/ReactCommon/react/renderer/mounting/tests/MountingTelemetryTest.cpp
T
Valentin Shergin dfa25df2cf Fabric: Counting number of text measuments as part of MountingTelemetry
Summary:
With this change, we now collect the number of text measurements that we perform during the layout phase of the commit. Text measurements are the most expensive layout operations which pretty much responsible for the vast majority of time spent in the layout phase.

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: mdvacca

Differential Revision: D23364664

fbshipit-source-id: 19514b93166b4053c2f3be37e79507f2c5248000
2020-08-27 12:33:58 -07:00

123 lines
3.2 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/MountingTelemetry.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(MountingTelemetryTest, 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(MountingTelemetryTest, normalUseCase) {
auto threshold = int64_t{70};
auto telemetry = MountingTelemetry{};
telemetry.setAsThreadLocal();
telemetry.willCommit();
sleep<TelemetryClock>(0.1);
telemetry.willLayout();
sleep<TelemetryClock>(0.2);
telemetry.didMeasureText();
MountingTelemetry::threadLocalTelemetry()->didMeasureText();
MountingTelemetry::threadLocalTelemetry()->didMeasureText();
telemetry.didLayout();
sleep<TelemetryClock>(0.1);
telemetry.didCommit();
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, 400, threshold);
EXPECT_EQ_WITH_THRESHOLD(layoutDuration, 200, threshold);
EXPECT_EQ_WITH_THRESHOLD(mountDuration, 100, threshold);
EXPECT_EQ(telemetry.getNumberOfTextMeasurements(), 3);
}
TEST(MountingTelemetryTest, abnormalUseCases) {
// Calling `did` before `will` should crash.
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = MountingTelemetry{};
telemetry.didDiff();
},
"diffStartTime_");
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = MountingTelemetry{};
telemetry.didCommit();
},
"commitStartTime_");
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = MountingTelemetry{};
telemetry.didMount();
},
"mountStartTime_");
// Getting `start` *or* `end` timepoints before a pair of `will` and `did`
// should crash.
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = MountingTelemetry{};
telemetry.willCommit();
telemetry.getCommitStartTime();
},
"commitEndTime_");
EXPECT_DEATH_IF_SUPPORTED(
{
auto telemetry = MountingTelemetry{};
telemetry.willCommit();
telemetry.getCommitEndTime();
},
"commitEndTime_");
}