-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-benchmarks.main.kts
executable file
·504 lines (446 loc) · 13.7 KB
/
run-benchmarks.main.kts
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#!/usr/bin/env kotlin
@file:DependsOn("com.squareup.okio:okio:3.2.0")
@file:DependsOn("com.google.cloud:google-cloud-storage:2.8.1")
@file:DependsOn("net.mbonnin.bare-graphql:bare-graphql:0.0.2")
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3")
@file:DependsOn("com.squareup.okhttp3:okhttp:4.10.0")
import com.google.auth.oauth2.GoogleCredentials
import com.google.cloud.storage.Storage
import com.google.cloud.storage.StorageOptions
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import net.mbonnin.bare.graphql.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okio.Buffer
import okio.buffer
import okio.source
import java.io.File
import java.util.Date
import kotlin.math.roundToLong
/**
* This script expects:
*
* - `gcloud` in the path
* - A Google Cloud Project with "Google Cloud Testing API" and "Cloud Tool Results API" enabled
* - GITHUB_REPOSITORY env variable: the repository to create the issue into in the form `owner/name`
*
* This script must be run from the repo root
*/
fun getInput(name: String): String {
return getOptionalInput(name) ?: error("Cannot find an input for $name")
}
fun getOptionalInput(name: String): String? {
return System.getenv("INPUT_${name.uppercase()}")?.ifBlank {
null
}
}
val now = System.currentTimeMillis() / 1000
/**
* Executes the given command and returns stdout as a String
* Throws if the exit code is not 0
*/
fun executeCommand(vararg command: String): CommandResult {
println("execute: ${command.joinToString(" ")}")
val process = ProcessBuilder()
.command(*command)
.redirectInput(ProcessBuilder.Redirect.INHERIT)
.start()
/**
* Read output and error in a thread to not block the process if the output/error
* doesn't fit in the buffer
*/
var output: String? = null
var error: String? = null
val outputThread = Thread {
val buffer = process.inputStream.source().buffer()
output = buildString {
while (true) {
val line = buffer.readUtf8Line()
if (line == null) {
break
}
println("STDOUT: $line")
appendLine(line)
}
}
}
outputThread.start()
val errorThread = Thread {
val buffer = process.errorStream.source().buffer()
error = buildString {
while (true) {
val line = buffer.readUtf8Line()
if (line == null) {
break
}
println("STDERR: $line")
appendLine(line)
}
}
}
errorThread.start()
val exitCode = process.waitFor()
outputThread.join()
errorThread.join()
return CommandResult(exitCode, output ?: "", error ?: "")
}
class CommandResult(val code: Int, val stdout: String, val stderr: String)
/**
* Authenticates the local 'gcloud' and a new [Storage] instance
* Throws on error
*/
fun authenticate(): GCloud {
val googleServicesJson = getInput("GOOGLE_SERVICES_JSON")
val tmpFile: File = File.createTempFile("google", "json")
val credentials: GoogleCredentials
val storage: Storage
try {
tmpFile.writeText(googleServicesJson)
val result = executeCommand("gcloud", "auth", "activate-service-account", "--key-file=${tmpFile.absoluteFile}")
if (result.code != 0) {
error("Cannot authenticate")
}
credentials = GoogleCredentials.fromStream(tmpFile.inputStream())
.createScoped(listOf("https://www.googleapis.com/auth/cloud-platform"))
storage = StorageOptions.newBuilder().setCredentials(credentials).build().service
} finally {
tmpFile.delete()
}
val jsonElement = Json.parseToJsonElement(googleServicesJson)
return GCloud(
storage,
jsonElement.jsonObject.get("project_id")?.jsonPrimitive?.content ?: error("Cannot find project_id")
)
}
data class GCloud(val storage: Storage, val projectId: String)
fun runTest(projectId: String): String {
val args = mutableListOf(
"gcloud",
"-q", // Disable all interactive prompts
"--project",
projectId,
"firebase",
"test",
"android",
"run",
"--type",
"instrumentation",
"--device",
"model=${getInput("device_model")}",
"--test",
getInput("test_apk"),
"--app",
getInput("app_apk")
)
getOptionalInput("directories_to_pull")?.let {
args.add("--directories-to-pull")
args.add(it)
}
getOptionalInput("environment_variables")?.let {
args.add("--environment-variables")
args.add(it)
}
val result = executeCommand(*args.toTypedArray())
check(result.code == 0) {
"Test failed"
}
// Most of the interesting output is in stderr
return result.stderr
}
/**
* Parses the 'gcloud firebase test android run' output and download the instrumentation
* results from Google Cloud Storage
*
* @return the [TestResult]
*/
fun getTestResult(output: String, storage: Storage): TestResult {
val gsUrl = output.lines().mapNotNull {
val matchResult =
Regex(".*\\[https://console.developers.google.com/storage/browser/([^\\]]*).*").matchEntire(it)
matchResult?.groupValues?.get(1)
}.single()
.split("/")
.filter { it.isNotBlank() }
val bucket = gsUrl[0]
val blobBase = "${gsUrl[1]}/redfin-30-en-portrait"
val directory = getOptionalInput("directories_to_pull")?.split(",")?.filter { !it.isNullOrBlank() }?.singleOrNull()
var cases: List<Case>? = null
var extraMetrics: List<Map<String, Any>>? = null
if (directory != null) {
// A directory was provided, look inside it to check if we can find the test results
cases = locateBenchmarkData(storage, bucket, "$blobBase/artifacts$directory")
extraMetrics = try {
locateExtraMetrics(storage, bucket, "$blobBase/artifacts$directory")
} catch (_: Exception) {
null
}
}
if (cases == null) {
// Get the cases from the logs
cases = downloadBlob(storage, bucket, "$blobBase/instrumentation.results").parseCases()
}
val firebaseUrl = output.lines().mapNotNull {
val matchResult = Regex("Test results will be streamed to \\[(.*)\\].").matchEntire(it)
matchResult?.groupValues?.get(1)
}.single()
return TestResult(firebaseUrl, cases, extraMetrics.orEmpty())
}
fun locateBenchmarkData(storage: Storage, bucket: String, prefix: String): List<Case>? {
val candidates = storage.list(bucket, Storage.BlobListOption.prefix(prefix)).values
return candidates.singleOrNull {
it.name.endsWith("benchmarkData.json")
}?.let {
downloadBlob(storage, bucket, it.name)
}?.let {
Json.parseToJsonElement(it).toAny()
}?.let {
it.parseCasesFromBenchmarkData()
}
}
fun locateExtraMetrics(storage: Storage, bucket: String, prefix: String): List<Map<String, Any>>? {
val candidates = storage.list(bucket, Storage.BlobListOption.prefix(prefix)).values
return candidates.singleOrNull {
it.name.endsWith("extraMetrics.json")
}?.let {
downloadBlob(storage, bucket, it.name)
}?.let {
Json.parseToJsonElement(it).toAny()
}?.let {
it.parseCasesFromExtraMetrics()
}
}
fun Any.parseCasesFromBenchmarkData(): List<Case> {
return this.asMap["benchmarks"].asList.map { it.asMap }.map {
Case(
test = it["name"].asString,
clazz = it["className"].asString,
nanos = it["metrics"].asMap["timeNs"].asMap["median"].asNumber.toLong(),
allocs = it["metrics"].asMap["allocationCount"].asMap["median"].asNumber.toLong(),
)
}
}
fun Any.parseCasesFromExtraMetrics(): List<Map<String, Any>> {
return this.asList.map { it.asMap }.map {
Serie(
name = it["name"].asString,
value = it["value"].asNumber.toLong(),
tags = it["tags"] as List<String>? ?: emptyList(),
now = now,
)
}
}
fun downloadBlob(storage: Storage, bucket: String, blobName: String): String {
val buffer = Buffer()
storage.get(bucket, blobName).downloadTo(buffer.outputStream())
return buffer.readUtf8()
}
/**
* Heuristics based parser until Firebase Test Labs supports downloading the Json
*/
fun String.parseCases(): List<Case> {
val cases = mutableListOf<Case>()
var clazz: String? = null
var test: String? = null
var nanos: Long? = null
var allocs: Long? = null
val clazzRegex = Regex("INSTRUMENTATION_STATUS: class=(.*)")
val testRegex = Regex("INSTRUMENTATION_STATUS: test=(.*)")
val nanosRegex = Regex("INSTRUMENTATION_STATUS: time_nanos_median=(.*)")
val allocsRegex = Regex("INSTRUMENTATION_STATUS: allocation_count_median=(.*)")
fun maybeOutput() {
if (clazz != null && test != null && nanos != null && allocs != null) {
cases.add(Case(clazz!!, test!!, nanos!!, allocs!!))
clazz = null
test = null
nanos = null
allocs = null
}
}
lines().forEach {
var result = clazzRegex.matchEntire(it)
if (result != null) {
clazz = result.groupValues[1]
maybeOutput()
return@forEach
}
result = testRegex.matchEntire(it)
if (result != null) {
test = result.groupValues[1]
maybeOutput()
return@forEach
}
result = nanosRegex.matchEntire(it)
if (result != null) {
nanos = result.groupValues[1].toDouble().roundToLong()
maybeOutput()
return@forEach
}
result = allocsRegex.matchEntire(it)
if (result != null) {
allocs = result.groupValues[1].toDouble().roundToLong()
maybeOutput()
return@forEach
}
}
return cases
}
data class TestResult(
val firebaseUrl: String,
val cases: List<Case>,
val extraMetrics: List<Map<String, Any>>,
)
data class Case(
val clazz: String,
val test: String,
val nanos: Long,
val allocs: Long,
) {
val fqName = "${clazz}.$test"
}
fun issueBody(testResult: TestResult): String {
return buildString {
appendLine("### Last Run: ${Date()}")
appendLine("* Firebase console: [link](${testResult.firebaseUrl})")
getOptionalInput("dd_dashboard_url")?.let {
appendLine("* Datadog dashboard: [link](${it})")
}
appendLine()
appendLine("### Test Cases:")
appendLine("| Test Case | Nanos | Allocs |")
appendLine("|-----------|-------|--------|")
testResult.cases.forEach {
appendLine("|${it.fqName}|${it.nanos}|${it.allocs}|")
}
}
}
val issueTitle = "Benchmarks dashboard"
fun updateOrCreateGithubIssue(testResult: TestResult) {
val ghRepo = System.getenv("GITHUB_REPOSITORY") ?: error("GITHUB_REPOSITORY env variable is missing")
val ghRepositoryOwner = ghRepo.split("/")[0]
val ghRepositoryName = ghRepo.split("/")[1]
val query = """
{
search(query: "$issueTitle repo:$ghRepo", type: ISSUE, first: 100) {
edges {
node {
... on Issue {
title
id
}
}
}
}
repository(owner: "$ghRepositoryOwner", name: "$ghRepositoryName") {
id
}
}
""".trimIndent()
val response = ghGraphQL(query)
val existingIssues = response.get("search").asMap.get("edges").asList
val mutation: String
val variables: Map<String, String>
if (existingIssues.isEmpty()) {
mutation = """
mutation createIssue(${'$'}repositoryId: ID!, ${'$'}title: String!, ${'$'}body: String!) {
createIssue(input: {repositoryId: ${'$'}repositoryId, title: ${'$'}title, body: ${'$'}body} ){
clientMutationId
}
}
""".trimIndent()
variables = mapOf(
"title" to issueTitle,
"body" to issueBody(testResult),
"repositoryId" to response.get("repository").asMap["id"].cast<String>()
)
println("creating issue")
} else {
mutation = """
mutation updateIssue(${'$'}id: ID!, ${'$'}body: String!) {
updateIssue(input: {id: ${'$'}id, body: ${'$'}body} ){
clientMutationId
}
}
""".trimIndent()
variables = mapOf(
"id" to existingIssues.first().asMap["node"].asMap["id"].cast<String>(),
"body" to issueBody(testResult)
)
println("updating issue")
}
ghGraphQL(mutation, variables)
}
fun ghGraphQL(operation: String, variables: Map<String, String> = emptyMap()): Map<String, Any?> {
val ghToken = getInput("github_token")
val headers = mapOf("Authorization" to "bearer $ghToken")
return graphQL(
url = "https://api.github.com/graphql",
operation = operation,
headers = headers,
variables = variables
).get("data").asMap
}
fun Serie(name: String, value: Long, tags: List<String>, now: Long): Map<String, Any> {
return mapOf(
"metric" to "${getInput("dd_metric_prefix")}.$name",
"type" to 0,
"points" to listOf(
mapOf(
"timestamp" to now,
"value" to value
)
),
"tags" to tags
)
}
fun Serie(clazz: String, test: String, name: String, value: Long, now: Long): Map<String, Any> {
return Serie(
name,
value,
listOf(
"class:$clazz",
"test:$test"
),
now
)
}
fun uploadToDatadog(cases: List<Case>, extraMetrics: List<Map<String, Any>>) {
val body = mapOf(
"series" to cases.flatMap {
listOf(
Serie(it.clazz, it.test, "nanos", it.nanos, now),
Serie(it.clazz, it.test, "allocs", it.allocs, now)
)
} + extraMetrics
)
val response = body.toJsonElement().toString().let {
Request.Builder().url("https://api.datadoghq.com/api/v2/series")
.post(it.toRequestBody("application/json".toMediaType()))
.addHeader("DD-API-KEY", getInput("DD_API_KEY"))
.build()
}.let {
OkHttpClient.Builder()
.build()
.newCall(it)
.execute()
}
check(response.isSuccessful) {
"Cannot post to Datadog: '${response.code}'\n${response.body?.string()}"
}
println("posted to Datadog")
}
fun main() {
val gcloud = authenticate()
val testOutput = runTest(gcloud.projectId)
val testResult = getTestResult(testOutput, gcloud.storage)
if (getOptionalInput("github_token") != null) {
updateOrCreateGithubIssue(testResult)
}
if (getOptionalInput("dd_api_key") != null) {
uploadToDatadog(testResult.cases, testResult.extraMetrics)
}
}
main()