-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
AndroidTransactionProfilerTest.kt
405 lines (358 loc) · 15.3 KB
/
AndroidTransactionProfilerTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package io.sentry.android.core
import android.content.Context
import android.os.Build
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.sentry.IHub
import io.sentry.ILogger
import io.sentry.ISentryExecutorService
import io.sentry.ProfilingTraceData
import io.sentry.SentryLevel
import io.sentry.SentryTracer
import io.sentry.TransactionContext
import io.sentry.android.core.internal.util.SentryFrameMetricsCollector
import io.sentry.assertEnvelopeItem
import io.sentry.test.getCtor
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.check
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.spy
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import java.io.File
import java.util.concurrent.Future
import java.util.concurrent.FutureTask
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
@RunWith(AndroidJUnit4::class)
class AndroidTransactionProfilerTest {
private lateinit var context: Context
private val className = "io.sentry.android.core.AndroidTransactionProfiler"
private val ctorTypes = arrayOf(Context::class.java, SentryAndroidOptions::class.java, BuildInfoProvider::class.java, SentryFrameMetricsCollector::class.java)
private val fixture = Fixture()
private class Fixture {
private val mockDsn = "http://key@localhost/proj"
val buildInfo = mock<BuildInfoProvider> {
whenever(it.sdkInfoVersion).thenReturn(Build.VERSION_CODES.LOLLIPOP)
}
val mockLogger = mock<ILogger>()
var lastScheduledRunnable: Runnable? = null
val mockExecutorService = object : ISentryExecutorService {
override fun submit(runnable: Runnable): Future<*> {
runnable.run()
return FutureTask {}
}
override fun schedule(runnable: Runnable, delayMillis: Long): Future<*> {
lastScheduledRunnable = runnable
return FutureTask {}
}
override fun close(timeoutMillis: Long) {}
}
val options = spy(SentryAndroidOptions()).apply {
dsn = mockDsn
profilesSampleRate = 1.0
isDebug = true
setLogger(mockLogger)
executorService = mockExecutorService
}
val hub: IHub = mock()
val frameMetricsCollector: SentryFrameMetricsCollector = mock()
lateinit var transaction1: SentryTracer
lateinit var transaction2: SentryTracer
lateinit var transaction3: SentryTracer
fun getSut(context: Context, buildInfoProvider: BuildInfoProvider = buildInfo): AndroidTransactionProfiler {
whenever(hub.options).thenReturn(options)
transaction1 = SentryTracer(TransactionContext("", ""), hub)
transaction2 = SentryTracer(TransactionContext("", ""), hub)
transaction3 = SentryTracer(TransactionContext("", ""), hub)
return AndroidTransactionProfiler(context, options, buildInfoProvider, frameMetricsCollector, hub)
}
}
@BeforeTest
fun `set up`() {
context = ApplicationProvider.getApplicationContext()
val buildInfoProvider = BuildInfoProvider(fixture.mockLogger)
AndroidOptionsInitializer.loadDefaultAndMetadataOptions(
fixture.options,
context,
fixture.mockLogger,
buildInfoProvider
)
AndroidOptionsInitializer.initializeIntegrationsAndProcessors(
fixture.options,
context,
buildInfoProvider,
LoadClass(),
false,
false
)
// Profiler doesn't start if the folder doesn't exists.
// Usually it's generated when calling Sentry.init, but for tests we can create it manually.
File(fixture.options.profilingTracesDirPath!!).mkdirs()
}
@AfterTest
fun clear() {
context.cacheDir.deleteRecursively()
}
@Test
fun `when null param is provided, invalid argument is thrown`() {
val ctor = className.getCtor(ctorTypes)
assertFailsWith<IllegalArgumentException> {
ctor.newInstance(arrayOf(null, mock<SentryAndroidOptions>(), mock()))
}
assertFailsWith<IllegalArgumentException> {
ctor.newInstance(arrayOf(mock<Context>(), null, mock(), mock()))
}
assertFailsWith<IllegalArgumentException> {
ctor.newInstance(arrayOf(mock<Context>(), mock<SentryAndroidOptions>(), null, mock()))
}
assertFailsWith<IllegalArgumentException> {
ctor.newInstance(arrayOf(mock<Context>(), mock<SentryAndroidOptions>(), mock(), null))
}
}
@Test
fun `profiler profiles current transaction`() {
val profiler = fixture.getSut(context)
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionFinish(fixture.transaction1)
verify(fixture.hub).captureEnvelope(
check {
assertEquals(1, it.items.count())
assertEnvelopeItem<ProfilingTraceData>(it.items.toList()) { _, item ->
assertEquals(item.transactionId, fixture.transaction1.eventId.toString())
}
}
)
}
@Test
fun `profiler works only on api 21+`() {
val buildInfo = mock<BuildInfoProvider> {
whenever(it.sdkInfoVersion).thenReturn(Build.VERSION_CODES.KITKAT)
}
val profiler = fixture.getSut(context, buildInfo)
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionFinish(fixture.transaction1)
verify(fixture.hub, never()).captureEnvelope(any())
}
@Test
fun `profiler on profilesSampleRate=0 false`() {
fixture.options.apply {
profilesSampleRate = 0.0
}
val profiler = fixture.getSut(context)
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionFinish(fixture.transaction1)
verify(fixture.hub, never()).captureEnvelope(any())
}
@Test
fun `profiler evaluates if profiling is enabled in options only on first transaction profiling`() {
fixture.options.apply {
profilesSampleRate = 0.0
}
// We create the profiler, and nothing goes wrong
val profiler = fixture.getSut(context)
verify(fixture.mockLogger, never()).log(SentryLevel.INFO, "Profiling is disabled in options.")
// Regardless of how many times the profiler is started, the option is evaluated and logged only once
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionStart(fixture.transaction1)
verify(fixture.mockLogger, times(1)).log(SentryLevel.INFO, "Profiling is disabled in options.")
}
@Test
fun `profiler evaluates profilingTracesDirPath options only on first transaction profiling`() {
fixture.options.apply {
cacheDirPath = null
}
// We create the profiler, and nothing goes wrong
val profiler = fixture.getSut(context)
verify(fixture.mockLogger, never()).log(
SentryLevel.WARNING,
"Disabling profiling because no profiling traces dir path is defined in options."
)
// Regardless of how many times the profiler is started, the option is evaluated and logged only once
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionStart(fixture.transaction1)
verify(fixture.mockLogger, times(1)).log(
SentryLevel.WARNING,
"Disabling profiling because no profiling traces dir path is defined in options."
)
}
@Test
fun `profiler evaluates profilingTracesHz options only on first transaction profiling`() {
fixture.options.apply {
profilingTracesHz = 0
}
// We create the profiler, and nothing goes wrong
val profiler = fixture.getSut(context)
verify(fixture.mockLogger, never()).log(
SentryLevel.WARNING,
"Disabling profiling because trace rate is set to %d",
0
)
// Regardless of how many times the profiler is started, the option is evaluated and logged only once
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionStart(fixture.transaction1)
verify(fixture.mockLogger, times(1)).log(
SentryLevel.WARNING,
"Disabling profiling because trace rate is set to %d",
0
)
}
@Test
fun `profiler on tracesDirPath null`() {
fixture.options.apply {
cacheDirPath = null
}
val profiler = fixture.getSut(context)
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionFinish(fixture.transaction1)
verify(fixture.hub, never()).captureEnvelope(any())
}
@Test
fun `profiler on tracesDirPath empty`() {
fixture.options.apply {
cacheDirPath = null
}
val profiler = fixture.getSut(context)
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionFinish(fixture.transaction1)
verify(fixture.hub, never()).captureEnvelope(any())
}
@Test
fun `profiler on profilingTracesHz 0`() {
fixture.options.apply {
profilingTracesHz = 0
}
val profiler = fixture.getSut(context)
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionFinish(fixture.transaction1)
verify(fixture.hub, never()).captureEnvelope(any())
}
@Test
fun `profiler ignores profilingTracesIntervalMillis`() {
fixture.options.apply {
profilingTracesIntervalMillis = 0
}
val profiler = fixture.getSut(context)
profiler.onTransactionStart(fixture.transaction1)
val traceData = profiler.onTransactionFinish(fixture.transaction1)
assertNotNull(traceData)
}
@Test
fun `profiler uses background threads`() {
val profiler = fixture.getSut(context)
fixture.options.executorService = mock()
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionFinish(fixture.transaction1)
verify(fixture.hub, never()).captureEnvelope(any())
}
@Test
fun `onTransactionFinish works only if previously started`() {
val profiler = fixture.getSut(context)
profiler.onTransactionFinish(fixture.transaction1)
verify(fixture.hub, never()).captureEnvelope(any())
}
@Test
fun `timedOutData has timeout truncation reason`() {
val profiler = fixture.getSut(context)
// Start and finish first transaction profiling
profiler.onTransactionStart(fixture.transaction1)
// Set timed out data by calling the timeout scheduled job
fixture.lastScheduledRunnable?.run()
// First transaction finishes: timed out data is returned
profiler.onTransactionFinish(fixture.transaction1)
verify(fixture.hub).captureEnvelope(
check {
assertEquals(1, it.items.count())
assertEnvelopeItem<ProfilingTraceData>(it.items.toList()) { _, item ->
assertEquals(item.transactionId, fixture.transaction1.eventId.toString())
assertEquals(ProfilingTraceData.TRUNCATION_REASON_TIMEOUT, item.truncationReason)
}
}
)
}
@Test
fun `profiling stops and returns data only when the last transaction finishes`() {
val profiler = fixture.getSut(context)
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionStart(fixture.transaction2)
profiler.onTransactionFinish(fixture.transaction2)
verify(fixture.hub, never()).captureEnvelope(any())
profiler.onTransactionFinish(fixture.transaction1)
verify(fixture.hub).captureEnvelope(
check {
assertEquals(1, it.items.count())
assertEnvelopeItem<ProfilingTraceData>(it.items.toList()) { _, item ->
assertEquals(item.transactionId, fixture.transaction1.eventId.toString())
}
}
)
}
@Test
fun `profiling records multiple concurrent transactions`() {
val profiler = fixture.getSut(context)
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionStart(fixture.transaction2)
profiler.onTransactionFinish(fixture.transaction1)
verify(fixture.hub, never()).captureEnvelope(any())
profiler.onTransactionStart(fixture.transaction3)
profiler.onTransactionFinish(fixture.transaction3)
verify(fixture.hub, never()).captureEnvelope(any())
profiler.onTransactionFinish(fixture.transaction2)
verify(fixture.hub).captureEnvelope(
check {
val expectedTransactions = listOf(
fixture.transaction1.eventId.toString(),
fixture.transaction3.eventId.toString(),
fixture.transaction2.eventId.toString()
)
assertEquals(1, it.items.count())
assertEnvelopeItem<ProfilingTraceData>(it.items.toList()) { _, item ->
assertEquals(item.transactionId, fixture.transaction2.eventId.toString())
assertTrue(item.transactions.map { it.id }.containsAll(expectedTransactions))
assertTrue(expectedTransactions.containsAll(item.transactions.map { it.id }))
}
}
)
}
@Test
fun `profiling trace data contains release field`() {
val profiler = fixture.getSut(context)
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionFinish(fixture.transaction1)
verify(fixture.hub).captureEnvelope(
check {
assertEnvelopeItem<ProfilingTraceData>(it.items.toList()) { _, item ->
assertEquals(fixture.options.release, item.release)
assertNotNull(item.release)
}
}
)
}
fun `profiler starts collecting frame metrics when the first transaction starts`() {
val profiler = fixture.getSut(context)
profiler.onTransactionStart(fixture.transaction1)
verify(fixture.frameMetricsCollector, times(1)).startCollection(any())
profiler.onTransactionStart(fixture.transaction2)
verify(fixture.frameMetricsCollector, times(1)).startCollection(any())
}
@Test
fun `profiler stops collecting frame metrics when the last transaction finishes`() {
val profiler = fixture.getSut(context)
val frameMetricsCollectorId = "id"
whenever(fixture.frameMetricsCollector.startCollection(any())).thenReturn(frameMetricsCollectorId)
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionStart(fixture.transaction2)
profiler.onTransactionFinish(fixture.transaction1)
verify(fixture.frameMetricsCollector, never()).stopCollection(frameMetricsCollectorId)
profiler.onTransactionFinish(fixture.transaction2)
verify(fixture.frameMetricsCollector).stopCollection(frameMetricsCollectorId)
}
}