diff --git a/src/Application.kt b/src/Application.kt index 8ef404e..6614543 100644 --- a/src/Application.kt +++ b/src/Application.kt @@ -15,8 +15,6 @@ fun main(args: Array): Unit = io.ktor.server.tomcat.EngineMain.main(args @kotlin.jvm.JvmOverloads fun Application.module(testing: Boolean = false) { runBlocking { - val cachedFile = File("/app/wave.wav").readBytes() - val gson = GsonBuilder().setPrettyPrinting().create() var startTime : Instant var elapsed = Duration.ofSeconds(0) val targetTime = Duration.ofSeconds(System.getenv("WAITTIME").toLong()) @@ -30,7 +28,7 @@ fun Application.module(testing: Boolean = false) { println("Starting benchmark") startTime = Instant.now() while(elapsed < targetTime){ - uploader.uploadData(currIter, gson, cachedFile) + uploader.uploadData(currIter) currIter+=1 currTimeStamp = Instant.now() timestamps[currIter] = currTimeStamp diff --git a/src/Uploader.kt b/src/Uploader.kt index 2b50956..8ef669e 100644 --- a/src/Uploader.kt +++ b/src/Uploader.kt @@ -1,6 +1,7 @@ package com.kmlabz.k8s import com.google.gson.Gson +import com.google.gson.GsonBuilder import io.ktor.client.* import io.ktor.client.engine.apache.* import io.ktor.client.request.* @@ -8,31 +9,33 @@ import io.ktor.client.request.forms.* import io.ktor.http.* import io.ktor.http.content.* import io.ktor.util.* +import java.io.File class Uploader(){ + private val gson = GsonBuilder().setPrettyPrinting().create() + private val inputSound = File("/app/wave.wav").readBytes() + val parts: List = 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", 1) + val jsonHeadersBuilder = HeadersBuilder() + jsonHeadersBuilder[HttpHeaders.ContentType] = "application/json" + this.append( + "description", + gson.toJson(inputSvcApi), + jsonHeadersBuilder.build() + ) + } @KtorExperimentalAPI - suspend fun uploadData(currIteration: Int, gson: Gson, inputSound: ByteArray) { + suspend fun uploadData(currIteration: Int) { HttpClient(Apache).use { client -> - val parts: List = 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() - ) - } - client.submitFormWithBinaryData(formData = parts) { url(System.getenv("URL") ?: "https://birb.k8s.kmlabz.com/benchmark") }