-
Notifications
You must be signed in to change notification settings - Fork 70
[RUM-8654] Slow frames collection support #2518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
cb9a417
RUM-8654: supporting slow frames collection
satween 3c166aa
RUM-8654: EvictingQueue support
satween 5c3f4b6
RUM-8654: fix detekt
satween 8d1ca49
RUM-8654: post-review fixes
satween a08aea8
RUM-8654: refactoring of EvictingQueue
satween 35ce15b
RUM-8654: post-review fixes
satween 4dfe847
RUM-8654: roll back debug code
satween abbe82d
RUM-8654: post-review fixes
satween a6c8196
RUM-8630: avoiding spikes in short views, support anr ratio
satween 9b0ec31
RUM-8630: fix apiSurface
satween 3826df2
RUM-8630: test support
satween 786d24a
RUM-8630: avoiding spikes in short views, support anr ratio
satween b6749fc
RUM-8657: event schema support for ui slowness
satween d0b470c
RUM-8657: post-review fixes
satween acfca21
RUM-8947: SlowFrameListener configuration in rum config support
satween bac7727
RUM-8947: fix detekt, tests, adding some logs
satween 828d7c2
RUM-8947: enabling slow frames monitoring in sample app
satween 1f6c00b
RUM-8654: post-review fixes
satween 01cc0d7
RUM-9065: fix computation errors
satween ae48be9
RUM-9065: detekt & fix api surface
satween a12b3ca
RUM-9065: pos-review fixes
satween f3c24f3
RUM-9065: fix freeze rate and slow frames rate
satween f942dd6
RUM-9065: post-review fixes
satween File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...-android-internal/src/main/java/com/datadog/android/internal/collections/EvictingQueue.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
package com.datadog.android.internal.collections | ||
|
||
import java.util.LinkedList | ||
import java.util.Queue | ||
import kotlin.math.max | ||
|
||
/** | ||
* A bounded queue that automatically evicts the oldest elements when new elements are added beyond its maximum capacity. | ||
* | ||
* This implementation delegates all [Queue] operations to an underlying [LinkedList]. It provides a FIFO (first-in, first-out) | ||
* behavior with a fixed maximum size. When new elements are added and the queue is at capacity, the oldest element is evicted. | ||
* | ||
* @param T the type of elements held in this queue. | ||
* @param maxSize the maximum number of elements the queue can hold. Must be greater than or equal to 0. | ||
* The default value is [Int.MAX_VALUE], which effectively means there is no practical bound. | ||
* @param delegate the underlying [LinkedList] that stores the elements and to which all [Queue] operations are delegated. | ||
*/ | ||
class EvictingQueue<T> private constructor( | ||
maxSize: Int, | ||
private val delegate: LinkedList<T> | ||
) : Queue<T> by delegate { | ||
|
||
/** | ||
* Secondary constructor that initializes the [EvictingQueue] with the given [maxSize]. | ||
* | ||
* @param maxSize the maximum number of elements the queue can hold. | ||
*/ | ||
constructor(maxSize: Int = Int.MAX_VALUE) : this(maxSize, LinkedList()) | ||
|
||
override val size: Int | ||
get() = delegate.size | ||
private val maxSize: Int = max(0, maxSize) | ||
|
||
/** | ||
* Adds the specified [element] to the end of this queue. | ||
* | ||
* If the queue has reached its maximum capacity, the first (oldest) element is evicted (removed) | ||
* before the new element is added. | ||
* | ||
* This queue should never throw [IllegalStateException] due to capacity restriction of the [delegate] because it | ||
* uses [java.util.Queue.offer] to insert elements. | ||
* | ||
* @param element the element to be added. | ||
* | ||
* @return `true` if this collection changed as a result of the call (as specified by [java.util.Collection.add]) | ||
*/ | ||
override fun add(element: T): Boolean { | ||
return this.offer(element) | ||
} | ||
|
||
/** | ||
* Adds the specified [element] to the end of this queue. | ||
* | ||
* If the queue has reached its maximum capacity, the first (oldest) element is evicted (removed) | ||
* before the new element is added. | ||
* | ||
* @param element the element to be added. | ||
* | ||
* @return `true` if this collection changed as a result of the call | ||
*/ | ||
override fun offer(element: T): Boolean { | ||
if (maxSize == 0) return false | ||
if (size >= maxSize) { | ||
delegate.poll() | ||
} | ||
|
||
@Suppress("UnsafeThirdPartyFunctionCall") // can't have NPE here | ||
return delegate.offer(element) | ||
} | ||
|
||
/** | ||
* Adds all of the elements in the specified [elements] collection to the end of this queue. | ||
* | ||
* If the number of elements in [elements] is greater than or equal to [maxSize], the queue is cleared first, | ||
* and only the last [maxSize] elements from [elements] are added. | ||
* | ||
* Otherwise, if adding [elements] would exceed the maximum capacity, the required number of oldest elements | ||
* are evicted from the front of the queue to make room. | ||
* | ||
* @param elements the collection of elements to be added. | ||
* @return `true` if the queue changed as a result of the call. | ||
*/ | ||
override fun addAll(elements: Collection<T>): Boolean { | ||
return when { | ||
maxSize == 0 -> false | ||
|
||
elements.size >= maxSize -> { | ||
clear() | ||
for ((index, element) in elements.withIndex()) { | ||
if (index < elements.size - maxSize) continue | ||
delegate.add(element) | ||
} | ||
true | ||
} | ||
|
||
else -> { | ||
val spaceLeft = maxSize - size | ||
for (index in 0 until elements.size - spaceLeft) { | ||
delegate.poll() | ||
} | ||
|
||
delegate.addAll(elements) | ||
} | ||
} | ||
} | ||
} |
170 changes: 170 additions & 0 deletions
170
dd-sdk-android-internal/src/test/java/com/datadog/internal/collections/EvictingQueueTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
package com.datadog.internal.collections | ||
|
||
import com.datadog.android.internal.collections.EvictingQueue | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
|
||
internal class EvictingQueueTest { | ||
|
||
@Test | ||
fun `M shrink items W add {more than max}`() { | ||
// Given | ||
val queue = EvictingQueue<Int>(3) | ||
|
||
// When | ||
repeat(5) { queue.add(it) } | ||
|
||
// Then | ||
assertThat(queue.toList()).isEqualTo(listOf(2, 3, 4)) | ||
} | ||
|
||
@Test | ||
fun `M shrink items W offer {more than max}`() { | ||
// Given | ||
val queue = EvictingQueue<Int>(3) | ||
|
||
// When | ||
repeat(5) { queue.offer(it) } | ||
|
||
// Then | ||
assertThat(queue.toList()).isEqualTo(listOf(2, 3, 4)) | ||
} | ||
|
||
@Test | ||
fun `M not shrink items W add {less than max}`() { | ||
// Given | ||
val queue = EvictingQueue<Int>(3) | ||
|
||
// When | ||
repeat(2) { queue.add(it) } | ||
|
||
// Then | ||
assertThat(queue.toList()).isEqualTo(listOf(0, 1)) | ||
} | ||
|
||
@Test | ||
fun `M not shrink items W offer {less than max}`() { | ||
// Given | ||
val queue = EvictingQueue<Int>(3) | ||
|
||
// When | ||
repeat(2) { queue.offer(it) } | ||
|
||
// Then | ||
assertThat(queue.toList()).isEqualTo(listOf(0, 1)) | ||
} | ||
|
||
@Test | ||
fun `M not shrink items W addAll {less than max}`() { | ||
// Given | ||
val queue = EvictingQueue<Int>(3) | ||
|
||
// When | ||
queue.addAll(listOf(1, 2)) | ||
|
||
// Then | ||
assertThat(queue.toList()).isEqualTo(listOf(1, 2)) | ||
} | ||
|
||
@Test | ||
fun `M not shrink items W addAll {equal max}`() { | ||
// Given | ||
val queue = EvictingQueue<Int>(3) | ||
|
||
// When | ||
queue.addAll(listOf(1, 2)) | ||
|
||
// Then | ||
assertThat(queue.toList()).isEqualTo(listOf(1, 2)) | ||
} | ||
|
||
@Test | ||
fun `M shrink items W addAll {more than max}`() { | ||
// Given | ||
val queue = EvictingQueue<Int>(3) | ||
|
||
// When | ||
queue.addAll(listOf(1, 2, 3, 4)) | ||
|
||
// Then | ||
assertThat(queue.toList()).isEqualTo(listOf(2, 3, 4)) | ||
} | ||
|
||
@Test | ||
fun `M shrink items W addAll {more than max, less than max}`() { | ||
// Given | ||
val queue = EvictingQueue<Int>(3) | ||
|
||
// When | ||
queue.addAll(listOf(1, 2, 3, 4)) | ||
queue.addAll(listOf(5, 6)) | ||
|
||
// Then | ||
assertThat(queue.toList()).isEqualTo(listOf(4, 5, 6)) | ||
} | ||
|
||
@Test | ||
fun `M shrink items W addAll {more than max, more than max}`() { | ||
// Given | ||
val queue = EvictingQueue<Int>(3) | ||
|
||
// When | ||
queue.addAll(listOf(1, 2, 3, 4)) | ||
queue.addAll(listOf(5, 6, 7, 8)) | ||
|
||
// Then | ||
assertThat(queue.toList()).isEqualTo(listOf(6, 7, 8)) | ||
} | ||
|
||
@Test | ||
fun `M shrink items W addAll {equal max, equal max}`() { | ||
// Given | ||
val queue = EvictingQueue<Int>(3) | ||
|
||
// When | ||
queue.addAll(listOf(1, 2, 3)) | ||
queue.addAll(listOf(4, 5, 6)) | ||
|
||
// Then | ||
assertThat(queue.toList()).isEqualTo(listOf(4, 5, 6)) | ||
} | ||
|
||
@Test | ||
fun `M create empty queue W maxSize le 0`() { | ||
// When | ||
val queue = EvictingQueue<Int>(-1) | ||
|
||
// Then | ||
assertThat(queue.size).isEqualTo(0) | ||
} | ||
|
||
@Test | ||
fun `M not change 0-sized queue W add`() { | ||
// Given | ||
val queue = EvictingQueue<Int>(0) | ||
|
||
// When | ||
assertDoesNotThrow { queue.add(1) } | ||
|
||
// Then | ||
assertThat(queue.size).isEqualTo(0) | ||
} | ||
|
||
@Test | ||
fun `M not change 0-sized queue W offer`() { | ||
// Given | ||
val queue = EvictingQueue<Int>(0) | ||
|
||
// When | ||
assertDoesNotThrow { queue.offer(1) } | ||
|
||
// Then | ||
assertThat(queue.size).isEqualTo(0) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.