new concept
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Torma Kristóf 2020-10-22 20:23:51 +02:00
parent f3d339a4a6
commit 9663660519
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
2 changed files with 51 additions and 41 deletions

View File

@ -5,6 +5,8 @@ import io.ktor.util.*
import kotlinx.coroutines.* import kotlinx.coroutines.*
import java.time.Duration import java.time.Duration
import java.time.Instant import java.time.Instant
import java.util.*
import kotlin.collections.LinkedHashMap
fun main(args: Array<String>): Unit = io.ktor.server.tomcat.EngineMain.main(args) fun main(args: Array<String>): Unit = io.ktor.server.tomcat.EngineMain.main(args)
@ -14,24 +16,25 @@ fun main(args: Array<String>): Unit = io.ktor.server.tomcat.EngineMain.main(args
fun Application.module(testing: Boolean = false) { fun Application.module(testing: Boolean = false) {
runBlocking { runBlocking {
var startTime: Instant var startTime: Instant
var elapsed = Duration.ofSeconds(0) var elapsed = Duration.ZERO
val targetTime = Duration.ofSeconds(System.getenv("WAITTIME").toLong()) val targetTime = Duration.ofSeconds(System.getenv("WAITTIME").toLong())
var currIter = 0 var currIter = 0
val timestamps = LinkedHashMap<Int, Instant>() val timestamps = LinkedHashMap<Int, Instant>()
var currTimeStamp: Instant val uploaders = LinkedList<Uploader>()
val uploader = Uploader()
val processResults = ProcessResults() val processResults = ProcessResults()
environment.monitor.subscribe(ApplicationStarted) { environment.monitor.subscribe(ApplicationStarted) {
GlobalScope.launch {
println("Starting benchmark") println("Starting benchmark")
startTime = Instant.now() startTime = Instant.now()
while (elapsed < targetTime) { while (elapsed < targetTime) {
currIter += 1 currIter += 1
GlobalScope.launch { uploaders.add(Uploader(currIter))
uploader.uploadData(currIter)
currTimeStamp = Instant.now()
timestamps[currIter] = currTimeStamp
elapsed = Duration.between(startTime, currTimeStamp)
} }
for (uploader in uploaders) {
uploader.sendRequest()
}
for (uploader in uploaders){
timestamps[uploader.currIteration] = uploader.respInstant
} }
println("Benchmark ended") println("Benchmark ended")
processResults.mapToProcess = timestamps processResults.mapToProcess = timestamps
@ -39,3 +42,4 @@ fun Application.module(testing: Boolean = false) {
} }
} }
} }
}

View File

@ -6,15 +6,21 @@ import io.ktor.client.*
import io.ktor.client.engine.apache.* import io.ktor.client.engine.apache.*
import io.ktor.client.request.* import io.ktor.client.request.*
import io.ktor.client.request.forms.* import io.ktor.client.request.forms.*
import io.ktor.client.statement.*
import io.ktor.http.* import io.ktor.http.*
import io.ktor.http.content.* import io.ktor.http.content.*
import io.ktor.util.* import io.ktor.util.*
import java.io.File import java.io.File
import java.time.Instant
class Uploader(){ class Uploader(currIteration: Int) {
private val gson = GsonBuilder().setPrettyPrinting().create() private val gson = GsonBuilder().setPrettyPrinting().create()
private val inputSound = File("/app/wave.wav").readBytes() private val inputSound = File("/app/wave.wav").readBytes()
val parts: List<PartData> = formData { private lateinit var parts: List<PartData>
var respInstant : Instant = Instant.now()
var currIteration : Int = 0
fun buildRequest() {
parts = formData {
val headersBuilder = HeadersBuilder() val headersBuilder = HeadersBuilder()
headersBuilder[HttpHeaders.ContentType] = "audio/wave" headersBuilder[HttpHeaders.ContentType] = "audio/wave"
headersBuilder[HttpHeaders.ContentDisposition] = "filename=csirip.wav" headersBuilder[HttpHeaders.ContentDisposition] = "filename=csirip.wav"
@ -23,7 +29,7 @@ class Uploader(){
inputSound, inputSound,
headersBuilder.build() headersBuilder.build()
) )
val inputSvcApi = InputSvcApi("2020-02-02T02:02:02", 1) val inputSvcApi = InputSvcApi("2020-02-02T02:02:02", currIteration)
val jsonHeadersBuilder = HeadersBuilder() val jsonHeadersBuilder = HeadersBuilder()
jsonHeadersBuilder[HttpHeaders.ContentType] = "application/json" jsonHeadersBuilder[HttpHeaders.ContentType] = "application/json"
this.append( this.append(
@ -32,13 +38,13 @@ class Uploader(){
jsonHeadersBuilder.build() jsonHeadersBuilder.build()
) )
} }
@KtorExperimentalAPI }
suspend fun uploadData(currIteration: Int) {
HttpClient(Apache).use { client ->
client.submitFormWithBinaryData<Unit>(formData = parts) { @KtorExperimentalAPI
url(System.getenv("URL") ?: "https://birb.k8s.kmlabz.com/benchmark") suspend fun sendRequest() {
} HttpClient(Apache).post<HttpResponse>(System.getenv("URL") ?: "https://birb.k8s.kmlabz.com/benchmark") {
} body = parts
}
respInstant = Instant.now()
} }
} }