Skip to content

Commit

Permalink
fix: Downloading performance Metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Adamczyk committed Nov 17, 2020
1 parent f02fb4e commit 272be86
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
18 changes: 16 additions & 2 deletions test_runner/src/main/kotlin/ftl/reports/api/PerformanceMetrics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,39 @@ package ftl.reports.api
import com.google.api.services.testing.model.TestExecution
import com.google.api.services.toolresults.model.PerfMetricsSummary
import ftl.android.AndroidCatalog
import ftl.args.IArgs
import ftl.gc.GcStorage
import ftl.gc.GcToolResults
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.runBlocking
import java.nio.file.Files
import java.nio.file.Paths

internal fun List<Pair<TestExecution, String>>.getAndUploadPerformanceMetrics(
resultBucket: String
args: IArgs
) = runBlocking {
filterAndroidPhysicalDevicesRuns()
.map { (testExecution, gcsStoragePath) ->
async(Dispatchers.IO) {
testExecution.getPerformanceMetric().upload(resultBucket = resultBucket, resultDir = gcsStoragePath)
val performanceMetrics = testExecution.getPerformanceMetric()
performanceMetrics.save(gcsStoragePath, args)
performanceMetrics.upload(resultBucket = args.resultsBucket, resultDir = gcsStoragePath)
}
}
.awaitAll()
}

private fun PerfMetricsSummary.save(resultsDir: String, args: IArgs) {
val configFilePath = if (args.useLocalResultDir())
Paths.get(args.localResultDir, "performanceMetrics.json") else
Paths.get(args.localResultDir, resultsDir, "performanceMetrics.json")

configFilePath.parent.toFile().mkdirs()
Files.write(configFilePath, toPrettyString().toByteArray())
}

private fun List<Pair<TestExecution, String>>.filterAndroidPhysicalDevicesRuns() = filterNot { (testExecution, _) ->
AndroidCatalog.isVirtualDevice(testExecution.environment.androidDevice, testExecution.projectId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ object ReportManager {
) {
testExecutions
.takeIf { args is AndroidArgs }
?.run {
withGcsStoragePath(matrices, args.resultsDir).getAndUploadPerformanceMetrics(args.resultsBucket)
}
?.run { withGcsStoragePath(matrices, args.resultsDir).getAndUploadPerformanceMetrics(args) }
}

private fun List<TestExecution>.withGcsStoragePath(
Expand Down
3 changes: 1 addition & 2 deletions test_runner/src/main/kotlin/ftl/run/NewTestRun.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ suspend fun newTestRun(args: IArgs) = withTimeoutOrNull(args.parsedTimeout) {

if (!args.async) {
cancelTestsOnTimeout(args.project, matrixMap.map) { pollMatrices(matrixMap.map.keys, args).updateMatrixMap(matrixMap) }
cancelTestsOnTimeout(args.project, matrixMap.map) { fetchArtifacts(matrixMap, args) }

ReportManager.generate(matrixMap, args, testShardChunks, ignoredTests)
cancelTestsOnTimeout(args.project, matrixMap.map) { fetchArtifacts(matrixMap, args) }

println()
matrixMap.printMatricesWebLinks(args.project)
Expand Down
4 changes: 2 additions & 2 deletions test_runner/src/main/kotlin/ftl/run/common/FetchArtifacts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ internal fun getDownloadPath(args: IArgs, blobPath: String): Path {
val objName = if (args.useLocalResultDir()) "" else parsed.getName(0).toString()
// for iOS it is shardName, remove this comment after FTL introduce server side sharding for iOS
val matrixName = parsed.getName(1).toString()
val deviceName = parsed.getName(2).toString()
val filePathName = if (args.keepFilePath) parsed.parent.drop(3).joinToString("/") else ""
val fileName = parsed.fileName.toString()
val deviceName = parsed.getName(2).toString().takeUnless { it == fileName }.orEmpty()
val filePathName = if (args.keepFilePath) parsed.parent.drop(3).joinToString("/") else ""

return Paths.get("$localDir/$objName/$matrixName/$deviceName/$filePathName/$fileName")
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.google.api.services.testing.model.TestExecution
import com.google.api.services.testing.model.ToolResultsStep
import com.google.common.truth.Truth.assertThat
import ftl.android.AndroidCatalog
import ftl.args.IArgs
import ftl.gc.GcStorage
import ftl.gc.GcToolResults
import ftl.test.util.FlankTestRunner
Expand All @@ -27,8 +28,11 @@ class PerformanceMetricsTest {
fun `should not get and upload performance metrics for virtual devices`() {
mockkObject(AndroidCatalog) {
every { AndroidCatalog.isVirtualDevice(any<AndroidDevice>(), any()) } returns true
val args = mockk<IArgs> {
every { resultsBucket } returns "b8ce"
}

assertThat(testExecutions.map { it to "path" }.getAndUploadPerformanceMetrics("b8ce")).isEmpty()
assertThat(testExecutions.map { it to "path" }.getAndUploadPerformanceMetrics(args)).isEmpty()
}
}

Expand All @@ -44,7 +48,12 @@ class PerformanceMetricsTest {
}

mockkObject(GcStorage) {
testExecutions.map { it to expectedPath }.getAndUploadPerformanceMetrics(expectedBucket)
val args = mockk<IArgs> {
every { resultsBucket } returns expectedBucket
every { useLocalResultDir() } returns false
every { localResultDir } returns "local"
}
testExecutions.map { it to expectedPath }.getAndUploadPerformanceMetrics(args)
performanceMetrics.forEach {
verify { GcStorage.uploadPerformanceMetrics(it, expectedBucket, expectedPath) }
}
Expand Down

0 comments on commit 272be86

Please sign in to comment.