Created
April 1, 2021 00:28
-
-
Save jaimedantas/5ba620162c0cb22c01214d055318f204 to your computer and use it in GitHub Desktop.
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 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