From 9653a424567377967d88e4ad2c4bffda2185e62c Mon Sep 17 00:00:00 2001 From: Xavier Gouchet Date: Wed, 1 Mar 2023 09:22:48 +0100 Subject: [PATCH] RUMM-3081 fix invalid cpu ticks per seconds --- .../RUMMobileVitalsScenario.storyboard | 13 +++++-- .../RUMMobileVitalsViewController.swift | 8 +++++ .../RUM/RUMMonitor/Scopes/RUMViewScope.swift | 2 +- .../RUM/RUMMobileVitalsScenarioTests.swift | 35 +++++++++++++++++++ 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/Datadog/Example/Scenarios/RUM/MobileVitals/RUMMobileVitalsScenario.storyboard b/Datadog/Example/Scenarios/RUM/MobileVitals/RUMMobileVitalsScenario.storyboard index 1f8287c31d..5af07a4a4e 100644 --- a/Datadog/Example/Scenarios/RUM/MobileVitals/RUMMobileVitalsScenario.storyboard +++ b/Datadog/Example/Scenarios/RUM/MobileVitals/RUMMobileVitalsScenario.storyboard @@ -1,9 +1,9 @@ - + - + @@ -18,7 +18,7 @@ - + + diff --git a/Datadog/Example/Scenarios/RUM/MobileVitals/RUMMobileVitalsViewController.swift b/Datadog/Example/Scenarios/RUM/MobileVitals/RUMMobileVitalsViewController.swift index b665a77f64..a69c41bf8e 100644 --- a/Datadog/Example/Scenarios/RUM/MobileVitals/RUMMobileVitalsViewController.swift +++ b/Datadog/Example/Scenarios/RUM/MobileVitals/RUMMobileVitalsViewController.swift @@ -5,6 +5,7 @@ */ import UIKit +import Datadog final class RUMMobileVitalsViewController: UIViewController { @@ -25,4 +26,11 @@ final class RUMMobileVitalsViewController: UIViewController { } } + @IBAction func startNewViewButtonTapped(_ sender: Any){ + print("Start New View button tapped... Starting new view...") + + Global.rum.startView(key: "sample view") + Global.rum.stopView(key: "sample view") + } + } diff --git a/Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift b/Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift index 3135276d13..00516452f5 100644 --- a/Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift +++ b/Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift @@ -456,7 +456,7 @@ internal class RUMViewScope: RUMScope, RUMContextProvider { view: .init( action: .init(count: actionsCount.toInt64), cpuTicksCount: cpuInfo?.greatestDiff, - cpuTicksPerSecond: cpuInfo?.greatestDiff?.divideIfNotZero(by: Double(timeSpent)), + cpuTicksPerSecond: timeSpent > 1.0 ? cpuInfo?.greatestDiff?.divideIfNotZero(by: Double(timeSpent)) : nil, crash: isCrash ? .init(count: 1) : .init(count: 0), cumulativeLayoutShift: nil, customTimings: customTimings.reduce(into: [:]) { acc, element in diff --git a/Tests/DatadogIntegrationTests/Scenarios/RUM/RUMMobileVitalsScenarioTests.swift b/Tests/DatadogIntegrationTests/Scenarios/RUM/RUMMobileVitalsScenarioTests.swift index d68093fa99..611941d68b 100644 --- a/Tests/DatadogIntegrationTests/Scenarios/RUM/RUMMobileVitalsScenarioTests.swift +++ b/Tests/DatadogIntegrationTests/Scenarios/RUM/RUMMobileVitalsScenarioTests.swift @@ -15,6 +15,10 @@ private extension ExampleApplication { func tapBlockMainThreadButton() { buttons["Block Main Thread"].tap() } + + func tapStartNewViewButton() { + buttons["Start New View"].tap() + } } class RUMMobileVitalsScenarioTests: IntegrationTests, RUMCommonAsserts { @@ -66,4 +70,35 @@ class RUMMobileVitalsScenarioTests: IntegrationTests, RUMCommonAsserts { let longTask2 = longTaskEvents[1] XCTAssertGreaterThan(longTask2.longTask.duration, 3_000_000_000) } + + func testRUMShortTimeSpentCPUScenario() throws { + // Server session recording RUM events send to `HTTPServerMock`. + let rumServerSession = server.obtainUniqueRecordingSession() + + let app = ExampleApplication() + app.launchWith( + testScenarioClassName: "RUMMobileVitalsScenario", + serverConfiguration: HTTPServerMockConfiguration( + rumEndpoint: rumServerSession.recordingURL + ) + ) + + // NOTE: RUMM-1086 even tapNoOpButton() can take up to 0.25sec in my local, + // therefore i used `threshold: 2.5` for long tasks in this scenario + app.tapStartNewViewButton() + try app.endRUMSession() + + // Get RUM Sessions with expected number of View visits + let recordedRUMRequests = try rumServerSession.pullRecordedRequests(timeout: dataDeliveryTimeout) { requests in + try RUMSessionMatcher.singleSession(from: requests)?.hasEnded() ?? false + } + + assertRUM(requests: recordedRUMRequests) + + let session = try XCTUnwrap(RUMSessionMatcher.singleSession(from: recordedRUMRequests)) + sendCIAppLog(session) + + let lastViewEvent = try XCTUnwrap(session.viewVisits[1].viewEvents.last) + XCTAssertNil(lastViewEvent.view.cpuTicksPerSecond) + } }