birb-integrated-bench/src/Uploader.kt

50 lines
1.6 KiB
Kotlin
Raw Normal View History

2020-10-22 02:11:14 +02:00
package com.kmlabz.k8s
import com.google.gson.Gson
2020-10-22 19:17:58 +02:00
import com.google.gson.GsonBuilder
2020-10-22 02:11:14 +02:00
import io.ktor.client.*
import io.ktor.client.engine.apache.*
import io.ktor.client.request.*
import io.ktor.client.request.forms.*
2020-10-22 20:23:51 +02:00
import io.ktor.client.statement.*
2020-10-22 02:11:14 +02:00
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.util.*
2020-10-22 19:17:58 +02:00
import java.io.File
2020-10-22 20:23:51 +02:00
import java.time.Instant
2020-10-22 02:11:14 +02:00
2020-10-22 20:23:51 +02:00
class Uploader(currIteration: Int) {
2020-10-22 19:17:58 +02:00
private val gson = GsonBuilder().setPrettyPrinting().create()
private val inputSound = File("/app/wave.wav").readBytes()
2020-10-22 20:23:51 +02:00
private lateinit var parts: List<PartData>
var respInstant : Instant = Instant.now()
var currIteration : Int = 0
fun buildRequest() {
parts = formData {
val headersBuilder = HeadersBuilder()
headersBuilder[HttpHeaders.ContentType] = "audio/wave"
headersBuilder[HttpHeaders.ContentDisposition] = "filename=csirip.wav"
this.append(
"file",
inputSound,
headersBuilder.build()
)
val inputSvcApi = InputSvcApi("2020-02-02T02:02:02", currIteration)
val jsonHeadersBuilder = HeadersBuilder()
jsonHeadersBuilder[HttpHeaders.ContentType] = "application/json"
this.append(
"description",
gson.toJson(inputSvcApi),
jsonHeadersBuilder.build()
)
}
2020-10-22 19:17:58 +02:00
}
2020-10-22 02:11:14 +02:00
2020-10-22 20:23:51 +02:00
@KtorExperimentalAPI
suspend fun sendRequest() {
HttpClient(Apache).post<HttpResponse>(System.getenv("URL") ?: "https://birb.k8s.kmlabz.com/benchmark") {
body = parts
2020-10-22 02:11:14 +02:00
}
2020-10-22 20:23:51 +02:00
respInstant = Instant.now()
2020-10-22 02:11:14 +02:00
}
}