diff --git a/src/Application.kt b/src/Application.kt index 0d24600..87134d5 100644 --- a/src/Application.kt +++ b/src/Application.kt @@ -1,14 +1,7 @@ package com.kmlabz.k8s -import com.google.gson.Gson import com.google.gson.GsonBuilder import io.ktor.application.* -import io.ktor.client.* -import io.ktor.client.engine.apache.* -import io.ktor.client.request.* -import io.ktor.client.request.forms.* -import io.ktor.http.* -import io.ktor.http.content.* import io.ktor.util.* import kotlinx.coroutines.* import java.io.File @@ -29,44 +22,19 @@ fun Application.module(testing: Boolean = false) { val targetTime = Duration.ofSeconds(10) var currIter = 1 val timestamps = LinkedHashMap() - var currTimeStamp = Instant.now() + var currTimeStamp : Instant + val uploader = Uploader() + val processResults = ProcessResults() + println("Starting benchmark") while(elapsed > targetTime){ - uploadData(currIter, gson, cachedFile) + println(currIter) + uploader.uploadData(currIter, gson, cachedFile) currIter+=1 currTimeStamp = Instant.now() timestamps[currIter] = currTimeStamp elapsed = Duration.between(startTime, currTimeStamp) } + println("Benchmark ended") + processResults.process() } -} - -@KtorExperimentalAPI -suspend fun uploadData(currIteration: Int, gson: Gson, inputSound: ByteArray) { - 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("https://birb.k8s.kmlabz.com/benchmark") - } - } -} - -data class InputSvcApi(val date: String, val device_id: Int) \ No newline at end of file +} \ No newline at end of file diff --git a/src/InputSvcApi.kt b/src/InputSvcApi.kt new file mode 100644 index 0000000..3934cc0 --- /dev/null +++ b/src/InputSvcApi.kt @@ -0,0 +1,3 @@ +package com.kmlabz.k8s + +data class InputSvcApi(val date: String, val device_id: Int) \ No newline at end of file diff --git a/src/ProcessResults.kt b/src/ProcessResults.kt new file mode 100644 index 0000000..5da783d --- /dev/null +++ b/src/ProcessResults.kt @@ -0,0 +1,20 @@ +package com.kmlabz.k8s + +import java.time.Instant +import java.time.ZoneId +import java.time.format.DateTimeFormatter + +class ProcessResults { + var mapToProcess = LinkedHashMap() + private var formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss_SSS") + .withZone(ZoneId.systemDefault()) + + + fun process() { + println("Start processing results") + for ((id, instant) in this.mapToProcess) { + val currInstantString = formatter.format(instant) + println("$id, $currInstantString") + } + } +} \ No newline at end of file diff --git a/src/Uploader.kt b/src/Uploader.kt new file mode 100644 index 0000000..f2d0574 --- /dev/null +++ b/src/Uploader.kt @@ -0,0 +1,41 @@ +package com.kmlabz.k8s + +import com.google.gson.Gson +import io.ktor.client.* +import io.ktor.client.engine.apache.* +import io.ktor.client.request.* +import io.ktor.client.request.forms.* +import io.ktor.http.* +import io.ktor.http.content.* +import io.ktor.util.* + +class Uploader(){ + @KtorExperimentalAPI + suspend fun uploadData(currIteration: Int, gson: Gson, inputSound: ByteArray) { + 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("https://birb.k8s.kmlabz.com/benchmark") + } + } + } +} \ No newline at end of file