Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,51 @@ TEST_P(NetworkReporterTest, testTwoSessionsReceiveNetworkEvents) {
secondary.toPage().sendMessage(R"({"id": 4, "method": "Network.disable"})");
}

TEST_P(NetworkReporterTest, testReportsDecodedBodyLengthForImages) {
this->startTracing();

auto reportImageResponse = [](const std::string& requestId,
int contentLength) {
NetworkReporter::getInstance().reportRequestStart(
requestId,
{
.url = "https://trace.example.com/image.png",
.httpMethod = "GET",
},
0,
std::nullopt);
NetworkReporter::getInstance().reportConnectionTiming(
requestId, std::nullopt);
NetworkReporter::getInstance().reportResponseStart(
requestId,
{
.url = "https://trace.example.com/image.png",
.statusCode = 200,
.headers = Headers{{"Content-Type", "image/png"}},
},
contentLength);
NetworkReporter::getInstance().reportResponseEnd(requestId, 4096);
};

reportImageResponse("known-length", 2048);
reportImageResponse("unknown-length", -1);

auto allTraceEvents = endTracingAndCollectEvents();

EXPECT_THAT(
allTraceEvents,
Contains(AllOf(
AtJsonPtr("/name", "ResourceFinish"),
AtJsonPtr("/args/data/requestId", "known-length"),
AtJsonPtr("/args/data/decodedBodyLength", 2048))));
EXPECT_THAT(
allTraceEvents,
Contains(AllOf(
AtJsonPtr("/name", "ResourceFinish"),
AtJsonPtr("/args/data/requestId", "unknown-length"),
AtJsonPtr("/args/data/decodedBodyLength", 0))));
}

struct NetworkReporterTracingTestParams {
bool enableNetworkDomain;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <jsinspector-modern/network/HttpUtils.h>
#include <react/performance/timeline/PerformanceEntryReporter.h>

#include <algorithm>

namespace facebook::react {

NetworkReporter& NetworkReporter::getInstance() {
Expand Down Expand Up @@ -185,6 +187,7 @@ void NetworkReporter::reportResponseEnd(
const std::string& requestId,
int encodedDataLength) {
auto now = HighResTimeStamp::now();
[[maybe_unused]] int decodedBodyLength = 0;

// All builds: Report PerformanceResourceTiming event
{
Expand All @@ -204,6 +207,10 @@ void NetworkReporter::reportResponseEnd(
eventData.contentType,
eventData.encodedBodySize,
eventData.decodedBodySize);
// Read before the erase. -1 if the content length is unknown.
if (eventData.contentType.starts_with("image/")) {
decodedBodyLength = std::max(0, eventData.decodedBodySize);
}
perfTimingsBuffer_.erase(requestId);
}
}
Expand All @@ -214,20 +221,9 @@ void NetworkReporter::reportResponseEnd(
requestId, encodedDataLength);

// Debugger enabled: Add trace event to Performance timeline
{
int decodedBodyLength = 0;

std::lock_guard<std::mutex> lock(perfTimingsMutex_);
auto it = perfTimingsBuffer_.find(requestId);
if (it != perfTimingsBuffer_.end() &&
it->second.contentType.starts_with("image/")) {
decodedBodyLength = it->second.decodedBodySize;
}

jsinspector_modern::tracing::PerformanceTracer::getInstance()
.reportResourceFinish(
requestId, now, encodedDataLength, decodedBodyLength);
}
jsinspector_modern::tracing::PerformanceTracer::getInstance()
.reportResourceFinish(
requestId, now, encodedDataLength, decodedBodyLength);
#endif
}

Expand Down
Loading