Created
March 14, 2019 12:50
-
-
Save viniciusccarvalho/63b1a49b8e66df29b8a740f5248fcb67 to your computer and use it in GitHub Desktop.
Ktor client load test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InvocationService(val client: HttpClient) { | |
val tasks = mutableListOf<InvocationTask>() | |
fun start(invocations: Int){ | |
val invocation = InvocationTask(invocations, client) | |
tasks.add(invocation) | |
invocation.start() | |
} | |
} | |
class InvocationTask(val invocations: Int, @JsonIgnore val client: HttpClient) : CoroutineScope { | |
var status = TaskStatus.STOPPED | |
@JsonIgnore | |
private val host = "<redacted>" | |
@JsonIgnore | |
private val job = Job() | |
var startTime: LocalDateTime? = null | |
var endTime: LocalDateTime? = null | |
val id = UUID.randomUUID().toString() | |
override val coroutineContext: CoroutineContext | |
@JsonIgnore | |
get() = Dispatchers.Default + job | |
@JsonIgnore | |
private val channel = Channel<ExecutionEvent>() | |
val invocationCount = AtomicInteger(0) | |
val requestTime = AtomicLong(0) | |
val delayTime = AtomicInteger(0) | |
fun start() { | |
status = TaskStatus.RUNNING | |
launch(Dispatchers.Default) { | |
for(event in channel){ | |
requestTime.addAndGet(event.requestTime) | |
delayTime.addAndGet(event.pause) | |
if(invocationCount.get() == 0) { | |
startTime = LocalDateTime.now() | |
} | |
if(invocationCount.get() == invocations){ | |
endTime = LocalDateTime.now() | |
status = TaskStatus.FINISHED | |
} | |
invocationCount.incrementAndGet() | |
} | |
} | |
for(i in 0..invocations){ | |
launch { | |
val response = client.call(host).response | |
val result = mapper.readValue<FunctionResponse>(response.readBytes()) | |
val requestTime = response.responseTime.timestamp - response.requestTime.timestamp | |
channel.send(ExecutionEvent(requestTime = requestTime, pause = result.pause)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment