1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.bedstead.performanceanalyzer
18 
19 import com.android.bedstead.harrier.BedsteadJUnit4
20 import com.android.bedstead.performanceanalyzer.PerformanceAnalyzer.Companion.analyzeThat
21 import com.android.bedstead.performanceanalyzer.annotations.PerformanceTest
22 import com.android.bedstead.performanceanalyzer.exceptions.PerformanceTestFailedException
23 import com.google.common.truth.Truth.assertThat
24 import kotlin.random.Random
25 import org.junit.Assert.assertThrows
26 import org.junit.runner.RunWith
27 
28 @RunWith(BedsteadJUnit4::class)
29 class PerformanceAnalyzerTest {
30 
<lambda>null31     private val runnable = {
32         Thread.sleep(1000)
33     }
34 
<lambda>null35     private val runnableThatFails = {
36         Thread.sleep(500)
37         throw RuntimeException()
38     }
39 
40     @PerformanceTest
analyzeThat_runnable_finishesIn_true_successnull41     fun analyzeThat_runnable_finishesIn_true_success() {
42         assertThat(
43             analyzeThat(runnable)
44                 .finishesIn(2000))
45             .isTrue()
46     }
47 
48     @PerformanceTest
analyzeThat_runnable_finishesIn_false_throwsnull49     fun analyzeThat_runnable_finishesIn_false_throws() {
50         val thrown = assertThrows(PerformanceTestFailedException::class.java) {
51             analyzeThat(runnable)
52                 .finishesIn(500)
53             }
54 
55         assertThat(thrown).hasMessageThat().contains("The function did not comply with the SLOs")
56     }
57 
58     @PerformanceTest
analyzeThat_runnable_withCleanUpSpecified_finishesIn_true_successnull59     fun analyzeThat_runnable_withCleanUpSpecified_finishesIn_true_success() {
60         assertThat(
61             analyzeThat(runnable)
62                 .cleanUpUsing(runnable)
63                 .finishesIn(2000))
64             .isTrue()
65     }
66 
67     @PerformanceTest
analyzeThat_runnable_withCleanUpSpecified_finishesIn_false_throwsnull68     fun analyzeThat_runnable_withCleanUpSpecified_finishesIn_false_throws() {
69         val thrown = assertThrows(PerformanceTestFailedException::class.java) {
70             analyzeThat(runnable)
71                 .cleanUpUsing(runnable)
72                 .finishesIn(500)
73         }
74 
75         assertThat(thrown).hasMessageThat().contains("The function did not comply with the SLOs")
76     }
77 
78     @PerformanceTest
analyzeThat_runnable_withCleanUpAndIterationsSpecified_finishesIn_true_successnull79     fun analyzeThat_runnable_withCleanUpAndIterationsSpecified_finishesIn_true_success() {
80         assertThat(
81             analyzeThat(runnable)
82                 .cleanUpUsing(runnable)
83                 .runsNumberOfTimes(3)
84                 .finishesIn(2000))
85             .isTrue()
86     }
87 
88     @PerformanceTest
analyzeThat_runnable_withCleanUpAndIterationsSpecified_finishesIn_false_throwsnull89     fun analyzeThat_runnable_withCleanUpAndIterationsSpecified_finishesIn_false_throws() {
90         val thrown = assertThrows(PerformanceTestFailedException::class.java) {
91             analyzeThat(runnable)
92                 .cleanUpUsing(runnable)
93                 .runsNumberOfTimes(3)
94                 .finishesIn(500)
95         }
96 
97         assertThat(thrown).hasMessageThat().contains("The function did not comply with the SLOs")
98     }
99 
100     @PerformanceTest
analyzeThat_runnableThatFails_summaryContainsFailureCountsnull101     fun analyzeThat_runnableThatFails_summaryContainsFailureCounts() {
102        val thrown = assertThrows(PerformanceTestFailedException::class.java) {
103             analyzeThat(runnableThatFails)
104                 .runsNumberOfTimes(3)
105                 .finishesIn(500)
106         }
107 
108         assertThat(thrown).hasMessageThat().contains("Number of failures: 3")
109     }
110 }
111