can stress test the analyse

16
Timo Bryant 2024-01-10 15:45:10 +01:00
parent 0e1ae654a7
commit 86f3f66d50
2 changed files with 76 additions and 16 deletions

View File

@ -1,17 +1,19 @@
package de.itkl.xsCli
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.convert
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.types.int
import de.itkl.httpClient.clients.XsClient
import de.itkl.httpClient.httpClientModule
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO
import java.util.Random
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.koin.core.context.startKoin
import org.koin.core.context.GlobalContext.startKoin
import java.io.File
import java.nio.file.Path
import java.nio.file.Paths
class Cli : CliktCommand() {
@ -21,8 +23,7 @@ class Cli : CliktCommand() {
val inputDirectory: File by option(help="Input directory path").convert { File(it) }.required()
val tasks: Int by option(help="Number of tasks").int().required()
override fun run() {
// TODO: Use inputDirectory and tasks here
override fun run() {
}
}
@ -34,11 +35,19 @@ class Cli : CliktCommand() {
class XsCli : KoinComponent {
private val xsClient: XsClient by inject()
suspend fun run() {
xsClient.analyse(Paths.get("assets/xs-reg/00001.jpg"))
xsClient.analyse(Paths.get("assets/xs-reg/00001.jpg"))
xsClient.analyse(Paths.get("assets/xs-reg/00001.jpg"))
xsClient.analyse(Paths.get("assets/xs-reg/00001.jpg"))
suspend fun run(tasks: Int, inputDirectory: Path) = coroutineScope {
val files = inputDirectory.toFile().listFiles()!!.toList().filter { it.isFile }.filter { it.extension in listOf("pdf", "ttf", "jpg", "jpeg") }
val random = Random()
(0..tasks)
.map {
val file = files[random.nextInt(files.size)]
async {
val taskRef = xsClient.analyse(file.toPath())
xsClient.waitFor(taskRef)
}
}
.awaitAll()
}
}
@ -46,6 +55,6 @@ suspend fun main(args: Array<String>) {
startKoin {
modules(httpClientModule)
}
XsCli().run()
XsCli().run(tasks = 1, inputDirectory = Paths.get("assets/xs-reg"))
// Cli().main(args)
}

View File

@ -2,6 +2,7 @@ package de.itkl.httpClient.clients
import io.github.oshai.kotlinlogging.KotlinLogging
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.request.forms.*
import io.ktor.client.statement.*
@ -12,10 +13,13 @@ import io.ktor.util.*
import io.ktor.util.cio.*
import io.ktor.utils.io.*
import io.ktor.utils.io.streams.*
import kotlinx.serialization.Serializable
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.awt.SystemColor.info
import java.nio.file.Files
import java.nio.file.Path
import java.util.UUID
import kotlin.io.path.exists
import kotlin.io.path.isRegularFile
import kotlin.io.path.name
@ -25,7 +29,18 @@ private val Log = KotlinLogging.logger { }
class XsClient : KoinComponent {
private val httpClient by inject<HttpClient>()
suspend fun analyse(image: Path) {
suspend fun waitFor(task: XsTask) {
Log.info { "Wait for competition: $task" }
val response = httpClient.get {
url("http://localhost:8080/api/v1/analyse/tasks/wait/${task.xsTaskId.taskId}")
user = "xs.dev.klara"
}
val responseText = response.bodyAsText()
Log.info { "Waiting done for task $task: ${response.status}: $responseText" }
}
suspend fun analyse(image: Path): TaskReference {
Log.info { "Starting analysis for image at path: $image" }
check(image.isRegularFile()) {
@ -46,11 +61,47 @@ class XsClient : KoinComponent {
)
}
) {
this.attributes.put(AttributeKey("username"), "xs.dev.klara")
user = "xs.dev.klara"
}
val responseText = response.bodyAsText()
Log.info { "Form submitted: ${response.status}: $responseText" }
Log.info { "Received the response from the server." }
if(response.status.isSuccess()) {
Log.info { "Successful response with status: ${response.status}" }
return response.body()
} else {
val responseText = response.bodyAsText()
Log.warn { "Error creating analyse task: ${response.status}: $responseText" }
error("Could not create analyse task: $responseText")
}
}
}
var HttpRequestBuilder.user: String?
get() = this.attributes[AttributeKey(("username"))]
set(value) = value?.let { this.attributes.put(AttributeKey("username"), it) } ?: this.attributes.remove(
AttributeKey("username")
)
interface XsTask {
val xsTaskId: XsTaskId
}
@Serializable
data class TaskReference(
override val xsTaskId: XsTaskId
) : XsTask {
override fun toString(): String {
return "Task($xsTaskId)"
}
}
@Serializable
data class XsTaskId(val tenantId: String, val taskId: String) {
override fun toString(): String {
return "$tenantId/$taskId"
}
}