Skip to content

Commit

Permalink
RUMM-2077 Disable all vitals on .never frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
maxep committed Jun 2, 2022
1 parent b6936c8 commit a6102a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 48 deletions.
37 changes: 19 additions & 18 deletions Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {
/// It can be toggled from inside `RUMResourceScope`/`RUMUserActionScope` callbacks, as they are called from processing `RUMCommand`s inside `process()`.
private var needsViewUpdate = false

private let vitalInfoSampler: VitalInfoSampler
private let vitalInfoSampler: VitalInfoSampler?

/// Samples view update events, so we can minimize the number of events in payload.
private let viewUpdatesThrottler: RUMViewUpdatesThrottlerType
Expand All @@ -98,14 +98,15 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {
self.viewName = name
self.viewStartTime = startTime
self.dateCorrection = dependencies.dateCorrector.currentCorrection

self.vitalInfoSampler = VitalInfoSampler(
cpuReader: dependencies.vitalCPUReader,
memoryReader: dependencies.vitalMemoryReader,
refreshRateReader: dependencies.vitalRefreshRateReader,
frequency: dependencies.vitalFrequency
)
self.viewUpdatesThrottler = dependencies.viewUpdatesThrottlerFactory()
self.vitalInfoSampler = dependencies.vitalFrequency.map { frequency in
.init(
cpuReader: dependencies.vitalCPUReader,
memoryReader: dependencies.vitalMemoryReader,
refreshRateReader: dependencies.vitalRefreshRateReader,
frequency: frequency
)
}
}

// MARK: - RUMContextProvider
Expand Down Expand Up @@ -376,10 +377,10 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {
let isActive = isActiveView || !resourceScopes.isEmpty
// RUMM-2079 `time_spent` can't be lower than 1ns
let timeSpent = max(1e-9, command.time.timeIntervalSince(viewStartTime))
let cpuInfo = vitalInfoSampler.cpu
let memoryInfo = vitalInfoSampler.memory
let refreshRateInfo = vitalInfoSampler.refreshRate
let isSlowRendered = refreshRateInfo.meanValue.flatMap { $0 < Constants.slowRenderingThresholdFPS }
let cpuInfo = vitalInfoSampler?.cpu
let memoryInfo = vitalInfoSampler?.memory
let refreshRateInfo = vitalInfoSampler?.refreshRate
let isSlowRendered = refreshRateInfo?.meanValue.flatMap { $0 < Constants.slowRenderingThresholdFPS }

let eventData = RUMViewEvent(
dd: .init(
Expand All @@ -404,8 +405,8 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {
version: dependencies.applicationVersion,
view: .init(
action: .init(count: actionsCount.toInt64),
cpuTicksCount: cpuInfo.greatestDiff,
cpuTicksPerSecond: cpuInfo.greatestDiff?.divideIfNotZero(by: Double(timeSpent)),
cpuTicksCount: cpuInfo?.greatestDiff,
cpuTicksPerSecond: cpuInfo?.greatestDiff?.divideIfNotZero(by: Double(timeSpent)),
crash: nil,
cumulativeLayoutShift: nil,
customTimings: customTimings.reduce(into: [:]) { acc, element in
Expand All @@ -428,12 +429,12 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {
loadingTime: nil,
loadingType: nil,
longTask: .init(count: longTasksCount),
memoryAverage: memoryInfo.meanValue,
memoryMax: memoryInfo.maxValue,
memoryAverage: memoryInfo?.meanValue,
memoryMax: memoryInfo?.maxValue,
name: viewName,
referrer: nil,
refreshRateAverage: refreshRateInfo.meanValue,
refreshRateMin: refreshRateInfo.minValue,
refreshRateAverage: refreshRateInfo?.meanValue,
refreshRateMin: refreshRateInfo?.minValue,
resource: .init(count: resourcesCount.toInt64),
timeSpent: timeSpent.toInt64Nanoseconds,
url: viewPath
Expand Down
6 changes: 1 addition & 5 deletions Sources/Datadog/RUM/RUMVitals/VitalInfoSampler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal final class VitalInfoSampler {
cpuReader: SamplingBasedVitalReader,
memoryReader: SamplingBasedVitalReader,
refreshRateReader: ContinuousVitalReader,
frequency: TimeInterval?,
frequency: TimeInterval,
maximumRefreshRate: Double = Double(UIScreen.main.maximumFramesPerSecond)
) {
self.cpuReader = cpuReader
Expand All @@ -63,10 +63,6 @@ internal final class VitalInfoSampler {
self.refreshRateReader.register(self.refreshRatePublisher)
self.maximumRefreshRate = maximumRefreshRate

guard let frequency = frequency else {
return
}

takeSample()
let timer = Timer(
timeInterval: frequency,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,6 @@ class VitalInfoSamplerTests: XCTestCase {
}
}

func testItDoesNotSamplePeriodically() {
let mockCPUReader = SamplingBasedVitalReaderMock()
let mockMemoryReader = SamplingBasedVitalReaderMock()

let sampler = VitalInfoSampler(
cpuReader: mockCPUReader,
memoryReader: mockMemoryReader,
refreshRateReader: ContinuousVitalReaderMock(),
frequency: nil
)

mockCPUReader.vitalData = 123.0
mockMemoryReader.vitalData = 321.0

let samplingExpectation = expectation(description: "sampling expectation")
DispatchQueue.global().asyncAfter(deadline: .now() + 0.5) {
samplingExpectation.fulfill()
}

waitForExpectations(timeout: 1.0) { _ in
XCTAssertEqual(sampler.cpu.sampleCount, 0)
XCTAssertEqual(sampler.memory.sampleCount, 0)
}
}

func testItSamplesDataFromBackgroundThreads() {
// swiftlint:disable implicitly_unwrapped_optional
var sampler: VitalInfoSampler!
Expand Down

0 comments on commit a6102a5

Please sign in to comment.