1 /*
<lambda>null2  * Copyright 2022 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 androidx.benchmark.darwin.gradle
18 
19 import androidx.benchmark.darwin.gradle.skia.Metrics
20 import androidx.benchmark.darwin.gradle.xcode.GsonHelpers
21 import androidx.benchmark.darwin.gradle.xcode.XcResultParser
22 import java.io.ByteArrayInputStream
23 import java.io.ByteArrayOutputStream
24 import javax.inject.Inject
25 import org.gradle.api.DefaultTask
26 import org.gradle.api.file.DirectoryProperty
27 import org.gradle.api.file.RegularFileProperty
28 import org.gradle.api.provider.Property
29 import org.gradle.api.tasks.CacheableTask
30 import org.gradle.api.tasks.Input
31 import org.gradle.api.tasks.InputDirectory
32 import org.gradle.api.tasks.Optional
33 import org.gradle.api.tasks.OutputFile
34 import org.gradle.api.tasks.PathSensitive
35 import org.gradle.api.tasks.PathSensitivity
36 import org.gradle.api.tasks.TaskAction
37 import org.gradle.process.ExecOperations
38 
39 @CacheableTask
40 abstract class DarwinBenchmarkResultsTask
41 @Inject
42 constructor(private val execOperations: ExecOperations) : DefaultTask() {
43     @get:InputDirectory
44     @get:PathSensitive(PathSensitivity.RELATIVE)
45     abstract val xcResultPath: DirectoryProperty
46 
47     @get:Input @get:Optional abstract val referenceSha: Property<String>
48 
49     @get:OutputFile abstract val outputFile: RegularFileProperty
50 
51     @get:OutputFile @get:Optional abstract val distFile: RegularFileProperty
52 
53     @TaskAction
54     fun benchmarkResults() {
55         val xcResultFile = xcResultPath.get().asFile
56         val parser =
57             XcResultParser(xcResultFile) { args ->
58                 val output = ByteArrayOutputStream()
59                 execOperations.exec { spec ->
60                     spec.commandLine = args
61                     spec.standardOutput = output
62                 }
63                 output.use {
64                     val input = ByteArrayInputStream(output.toByteArray())
65                     input.use { input.reader().readText() }
66                 }
67             }
68         val (record, summaries) = parser.parseResults()
69         val metrics = Metrics.buildMetrics(record, summaries, referenceSha.orNull)
70         val output = GsonHelpers.gsonBuilder().setPrettyPrinting().create().toJson(metrics)
71 
72         outputFile.get().asFile.writeText(output)
73 
74         // Add output to the DIST_DIR when specified
75         if (distFile.isPresent) {
76             distFile.get().asFile.writeText(output)
77         }
78     }
79 }
80