Skip to content

Instantly share code, notes, and snippets.

@jaimedantas
Created April 1, 2021 00:28
Show Gist options
  • Save jaimedantas/5ba620162c0cb22c01214d055318f204 to your computer and use it in GitHub Desktop.
Save jaimedantas/5ba620162c0cb22c01214d055318f204 to your computer and use it in GitHub Desktop.
class LoadSimulator{
@Inject
lateinit var propertiesConfiguration: PropertiesConfiguration
/**
* Process the request
*/
suspend fun processLoad(): HelloWorld {
val currentStartTime = LocalDateTime.now()
val threadDuration = propertiesConfiguration.processingTime/propertiesConfiguration.threads
val workload: List<Deferred<Unit>> = (1..propertiesConfiguration.threads).map {
GlobalScope.async {
BusyThread(threadDuration, currentStartTime)
}
}
workload.awaitAll()
return HelloWorld(propertiesConfiguration.returnMessage)
}
/**
* Puts this thread to work for some time
*/
private fun BusyThread(duration: Long, startTime: LocalDateTime) {
while (LocalDateTime.now() < startTime.plusNanos(duration * 1000000)){
var dummy = 213123
val dummy2 = dummy * dummy
dummy += dummy2
Thread.sleep(50)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment