Skip to content

Commit 7613d5a

Browse files
authored
Reduce examplesTest execution time (#481)
Halves execution time of examplesTest in CI by ensuring the tests use faster API queries and by removing one test case of example-gradle-task (particularly slow as it tested the example default of a `-14d` query).
1 parent bffbdd5 commit 7613d5a

File tree

11 files changed

+69
-44
lines changed

11 files changed

+69
-44
lines changed

examples/example-project/src/main/kotlin/com/gabrielfeo/develocity/api/example/Main.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ import okhttp3.OkHttpClient
1313

1414
val clientBuilder = OkHttpClient.Builder()
1515

16-
suspend fun main() {
16+
suspend fun main(args: Array<String>) {
1717
val newConfig = Config(
1818
clientBuilder = clientBuilder,
1919
)
2020
val develocityApi = DevelocityApi.newInstance(newConfig)
21-
runAllAnalysis(develocityApi)
21+
val query = args.getOrElse(0) { "buildStartTime>-1d buildTool:gradle" }
22+
runAllAnalysis(develocityApi, query)
2223
develocityApi.shutdown()
2324
}
2425

25-
private suspend fun runAllAnalysis(develocityApi: DevelocityApi) {
26-
mostFrequentBuilds(api = develocityApi.buildsApi)
26+
private suspend fun runAllAnalysis(develocityApi: DevelocityApi, query: String) {
27+
mostFrequentBuilds(api = develocityApi.buildsApi, query)
2728
}

examples/example-project/src/main/kotlin/com/gabrielfeo/develocity/api/example/analysis/MostFrequentBuilds.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import java.util.LinkedList
2222
*/
2323
suspend fun mostFrequentBuilds(
2424
api: BuildsApi,
25-
startTime: String = "-7d",
25+
query: String,
2626
) {
2727
// Fetch builds from the API
2828
val builds: List<GradleAttributes> = api.getBuildsFlow(
2929
fromInstant = 0,
30-
query = """buildStartTime>$startTime buildTool:gradle""",
30+
query = query,
3131
models = listOf(BuildModelName.gradleAttributes),
3232
).map {
3333
it.models!!.gradleAttributes!!.model!!

examples/example-scripts/example-script.main.kts

100644100755
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* - "Some are doing check builds locally, which we set up to trigger our notably slow
1515
* legacy tests. We should suggest they run test instead, leaving check for CI to run."
1616
*
17-
* Run this with at least 1GB of heap to accomodate the fetched data: JAVA_OPTS=-Xmx1g
17+
* Run this with at least 1GB of heap to accommodate the fetched data: JAVA_OPTS=-Xmx1g
1818
*/
1919

2020
@file:DependsOn("com.gabrielfeo:develocity-api-kotlin:2024.3.0")
@@ -24,21 +24,17 @@ import com.gabrielfeo.develocity.api.model.*
2424
import com.gabrielfeo.develocity.api.extension.*
2525
import kotlinx.coroutines.*
2626
import kotlinx.coroutines.flow.*
27-
import java.time.*
2827
import java.util.LinkedList
2928

3029
// Parameters
31-
val startDate = LocalDate.now().minusWeeks(1)
32-
val buildFilter: (GradleAttributes) -> Boolean = { build ->
33-
"LOCAL" in build.tags
34-
}
30+
val query = args.getOrElse(0) { "buildStartTime>-7d buildTool:gradle" }
3531

3632
// Fetch builds from the API
3733
val api = DevelocityApi.newInstance()
3834
val builds: List<GradleAttributes> = runBlocking {
3935
api.buildsApi.getBuildsFlow(
4036
fromInstant = 0,
41-
query = """buildStartTime>-7d buildTool:gradle""",
37+
query = query,
4238
models = listOf(BuildModelName.gradleAttributes),
4339
).map {
4440
it.models!!.gradleAttributes!!.model!!
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.gabrielfeo.develocity.api.example
2+
3+
object BuildStartTime {
4+
const val RECENT = "-10h"
5+
}
6+
7+
object Queries {
8+
const val FAST = "buildStartTime>${BuildStartTime.RECENT} buildTool:gradle"
9+
}

library/src/examplesTest/kotlin/com/gabrielfeo/develocity/api/example/gradle/ExampleGradleTaskTest.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.gabrielfeo.develocity.api.example.gradle
22

3+
import com.gabrielfeo.develocity.api.example.BuildStartTime
34
import org.junit.jupiter.api.Assertions.assertTrue
45
import org.junit.jupiter.api.BeforeEach
56
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation
@@ -43,10 +44,10 @@ class ExampleGradleTaskTest {
4344
}
4445

4546
@Test
46-
fun testBuildPerformanceMetricsTaskWithDefaults() {
47-
val user = System.getProperty("user.name")
48-
val output = runBuild("userBuildPerformanceMetrics").stdout
49-
assertPerformanceMetricsOutput(output, user = user, period = "-14d")
47+
fun testBuildPerformanceMetricsTask() {
48+
val args = "--user runner --period=${BuildStartTime.RECENT}"
49+
val output = runBuild("userBuildPerformanceMetrics $args").stdout
50+
assertPerformanceMetricsOutput(output, user = "runner", period = BuildStartTime.RECENT)
5051
}
5152

5253
private fun runBuild(gradleArgs: String) =
@@ -67,10 +68,4 @@ class ExampleGradleTaskTest {
6768
assertTrue(output.contains("▶︎ Serialization factor:"))
6869
assertTrue(output.contains("⏩︎ Avoidance savings:"))
6970
}
70-
71-
@Test
72-
fun testBuildPerformanceMetricsTaskWithOptions() {
73-
val output = runBuild("userBuildPerformanceMetrics --user runner --period=-1d").stdout
74-
assertPerformanceMetricsOutput(output, user = "runner", period = "-1d")
75-
}
7671
}

library/src/examplesTest/kotlin/com/gabrielfeo/develocity/api/example/gradle/ExampleProjectTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.gabrielfeo.develocity.api.example.gradle
22

3+
import com.gabrielfeo.develocity.api.example.Queries
34
import org.junit.jupiter.api.Assertions.assertTrue
45
import org.junit.jupiter.api.BeforeEach
56
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation
@@ -43,7 +44,7 @@ class ExampleProjectTest {
4344

4445
@Test
4546
fun testExampleProject() {
46-
val output = runBuild("run").stdout
47+
val output = runBuild("""run --args '"${Queries.FAST}"'""").stdout
4748
val tableRegex = Regex("""(?ms)^[-]+\nMost frequent builds:\n\s*\n(.+\|\s*\d+\s*\n?)+""")
4849
assertTrue(tableRegex.containsMatchIn(output)) {
4950
"Expected match for pattern '$tableRegex' in output '$output'"

library/src/examplesTest/kotlin/com/gabrielfeo/develocity/api/example/notebook/Jupyter.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class Jupyter(
1212
val workDir: Path,
1313
val venv: Path,
1414
) {
15-
1615
class Execution(
1716
val outputStreams: OutputStreams,
1817
val outputNotebook: Path,
@@ -31,24 +30,24 @@ class Jupyter(
3130
return Execution(outputStreams, outputPath)
3231
}
3332

34-
fun replaceMagics(
33+
fun replacePattern(
3534
path: Path,
36-
replacePattern: Regex,
37-
replacement: String
35+
pattern: Regex,
36+
replacement: String,
3837
): Path {
3938
if ((workDir / "preprocessors.py").notExists()) {
4039
copyFromResources("/preprocessors.py", workDir)
4140
}
42-
val outputPath = path.parent / "${path.nameWithoutExtension}-processed.ipynb"
41+
val outputPath = path.parent / "${path.nameWithoutExtension}-replaced.ipynb"
4342
runInShell(
4443
workDir,
4544
"source '${venv / "bin/activate"}' &&",
4645
"jupyter nbconvert '$path'",
4746
"--to ipynb",
4847
"--output='$outputPath'",
49-
"--NotebookExporter.preprocessors=preprocessors.ReplaceMagicsPreprocessor",
50-
"--ReplaceMagicsPreprocessor.pattern='$replacePattern'",
51-
"--ReplaceMagicsPreprocessor.replacement='$replacement'",
48+
"--NotebookExporter.preprocessors=preprocessors.ReplacePatternPreprocessor",
49+
"--ReplacePatternPreprocessor.pattern='$pattern'",
50+
"--ReplacePatternPreprocessor.replacement='$replacement'",
5251
)
5352
return outputPath
5453
}

library/src/examplesTest/kotlin/com/gabrielfeo/develocity/api/example/notebook/NotebooksTest.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.gabrielfeo.develocity.api.example.notebook
22

33
import com.gabrielfeo.develocity.api.example.JsonAdapter
4+
import com.gabrielfeo.develocity.api.example.Queries
45
import com.gabrielfeo.develocity.api.example.copyFromResources
56
import org.junit.jupiter.api.Assertions.assertTrue
67
import org.junit.jupiter.api.BeforeEach
@@ -38,7 +39,8 @@ class NotebooksTest {
3839
@Test
3940
fun testMostFrequentBuildsNotebook() {
4041
val sourceNotebook = tempDir / "examples/example-notebooks/MostFrequentBuilds.ipynb"
41-
val snapshotNotebook = forceUseOfMavenLocalSnapshotArtifact(sourceNotebook)
42+
val fasterNotebook = forceUseOfFasterQuery(sourceNotebook)
43+
val snapshotNotebook = forceUseOfMavenLocalSnapshotArtifact(fasterNotebook)
4244
val executedNotebook = assertDoesNotThrow { jupyter.executeNotebook(snapshotNotebook) }
4345
with(JsonAdapter.fromJson(executedNotebook.outputNotebook).asNotebookJson()) {
4446
assertTrue(textOutputLines.any { Regex("""Collected \d+ builds from the API""").containsMatchIn(it) }) {
@@ -53,6 +55,12 @@ class NotebooksTest {
5355
}
5456
}
5557

58+
private fun forceUseOfFasterQuery(sourceNotebook: Path): Path = jupyter.replacePattern(
59+
path = sourceNotebook,
60+
pattern = Regex("""query\s*=.+,"""),
61+
replacement = """query = "${Queries.FAST}",""",
62+
)
63+
5664
@Test
5765
fun testLoggingNotebook() {
5866
val sourceNotebook = tempDir / "examples/example-notebooks/Logging.ipynb"
@@ -67,12 +75,13 @@ class NotebooksTest {
6775
val libraryDescriptor = (tempDir / "develocity-api-kotlin.json").apply {
6876
writeText(buildLibraryDescriptor(version = "SNAPSHOT", repository = mavenLocal))
6977
}
70-
return jupyter.replaceMagics(
78+
return jupyter.replacePattern(
7179
path = sourceNotebook,
72-
replacePattern = Regex("""(?:DependsOn|%use).*develocity-api-kotlin.*"""),
80+
pattern = Regex("(?:DependsOn|%use).*develocity-api-kotlin.*"),
7381
replacement = """
7482
%use develocity-api-kotlin@file[$libraryDescriptor]
7583
%trackClasspath on
84+
%logLevel debug
7685
""".trimIndent()
7786
)
7887
}

library/src/examplesTest/kotlin/com/gabrielfeo/develocity/api/example/notebook/ScriptsTest.kt

Whitespace-only changes.

library/src/examplesTest/kotlin/com/gabrielfeo/develocity/api/example/script/ScriptsTest.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.gabrielfeo.develocity.api.example.script
22

3+
import com.gabrielfeo.develocity.api.example.Queries
34
import com.gabrielfeo.develocity.api.example.copyFromResources
45
import com.gabrielfeo.develocity.api.example.runInShell
56
import org.junit.jupiter.api.Assertions.assertTrue
@@ -12,18 +13,18 @@ import kotlin.io.path.div
1213
class ScriptsTest {
1314

1415
@TempDir
15-
lateinit var tempDir: Path
16+
lateinit var dir: Path
1617

1718
@BeforeEach
1819
fun setup() {
19-
copyFromResources("/examples", tempDir)
20+
copyFromResources("/examples", dir)
2021
}
2122

2223
@Test
2324
fun testMostFrequentBuildsScript() {
24-
val script = tempDir / "examples/example-scripts/example-script.main.kts"
25+
val script = dir / "examples/example-scripts/example-script.main.kts"
2526
val replacedScript = forceUseOfMavenLocalSnapshotArtifact(script)
26-
val output = runInShell(tempDir, "kotlin '$replacedScript'").stdout.trim()
27+
val output = runInShell(dir, "kotlin '$replacedScript' '${Queries.FAST}'").stdout.trim()
2728
val tableRegex = Regex("""(?ms)^[-]+\nMost frequent builds:\n\s*\n(.+\|\s*\d+\s*\n?)+""")
2829
assertTrue(tableRegex.containsMatchIn(output)) {
2930
"Expected match for pattern '$tableRegex' in output '$output'"
@@ -43,7 +44,7 @@ class ScriptsTest {
4344
@file:Repository("file://$mavenLocal")
4445
""".trimIndent(),
4546
)
46-
val replacedPath = tempDir.resolve("examples/example-scripts/example-script-SNAPSHOT.main.kts")
47+
val replacedPath = dir.resolve("examples/example-scripts/example-script-SNAPSHOT.main.kts")
4748
replacedPath.toFile().writeText(replaced)
4849
return replacedPath
4950
}

0 commit comments

Comments
 (0)