summaryrefslogtreecommitdiff
path: root/plugins/kotlin/performance-tests/test/org/jetbrains/kotlin/idea/perf/util/ESUploader.kt
blob: 98cca19478bd0831dc0a666224896f5f1ffe99f9 (plain)
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
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package org.jetbrains.kotlin.idea.perf.util

import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody

object ESUploader {
    var host: String? = null
    var username: String? = null
    var password: String? = null

    var indexName = "kotlin_ide_benchmarks"

    private val JSON: MediaType = "application/json; charset=utf-8".toMediaType()
    private val client = OkHttpClient()

    init {
        host = System.getenv("es.hostname")
        username = System.getenv("es.username")
        password = System.getenv("es.password")
        logMessage { "initialized es details $username @ $host" }
    }

    fun upload(benchmark: Benchmark) {
        if (host == null) {
            logMessage { "ES host is not specified, ${benchmark.id()} would not be uploaded" }
            return
        }

        val url = "$host/$indexName/_doc/${benchmark.id()}"
        val auth = if (username != null && password != null) {
            Credentials.basic(username!!, password!!);
        } else {
            null
        }
        val json = kotlinJsonMapper.writeValueAsString(benchmark)

        val body: RequestBody = json.toRequestBody(JSON)
        val request: Request = Request.Builder()
            .url(url)
            .post(body)
            .header("Content-Type", "application/json")
            .also { builder ->
                auth?.let {
                    builder.header("Authorization", it)
                }
            }
            .build()
        client.newCall(request).execute().use { response ->
            val code = response.code
            val string = response.body?.string()
            logMessage { "$code -> $string" }
            if (code != 200 && code != 201) {
                throw IllegalStateException("Error code $code -> $string")
            }
        }
    }
}