mirror of
https://github.com/tursom/TursomServer.git
synced 2025-03-14 03:40:06 +08:00
finish performance-test module
This commit is contained in:
parent
3a20ab01d4
commit
4fbe5236ee
@ -0,0 +1,55 @@
|
||||
package cn.tursom.test
|
||||
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
class PerformanceTest(
|
||||
val countPerThread: Int,
|
||||
val threadCount: Int = 1,
|
||||
val handler: PerformanceTestHandler
|
||||
) {
|
||||
val totalCount = countPerThread * threadCount
|
||||
|
||||
constructor(
|
||||
countPerThread: Int,
|
||||
threadCount: Int = 1,
|
||||
handler: () -> Unit
|
||||
) : this(countPerThread, threadCount, object : PerformanceTestHandler, () -> Unit by handler {})
|
||||
|
||||
fun run(): TestResult {
|
||||
val schedule = AtomicInteger(0)
|
||||
val wait = CountDownLatch(threadCount)
|
||||
val runLock = CountDownLatch(1)
|
||||
var line95 = 0L
|
||||
var line98 = 0L
|
||||
var line99 = 0L
|
||||
repeat(threadCount) {
|
||||
thread {
|
||||
runLock.await()
|
||||
try {
|
||||
repeat(countPerThread) {
|
||||
val loop = schedule.getAndIncrement()
|
||||
if (loop * 100 % totalCount == 0) {
|
||||
val line = loop * 100 / totalCount
|
||||
when (line) {
|
||||
95 -> line95 = System.currentTimeMillis()
|
||||
98 -> line98 = System.currentTimeMillis()
|
||||
99 -> line99 = System.currentTimeMillis()
|
||||
}
|
||||
handler.logSchedule(line)
|
||||
}
|
||||
handler()
|
||||
}
|
||||
} finally {
|
||||
wait.countDown()
|
||||
}
|
||||
}
|
||||
}
|
||||
val start = System.currentTimeMillis()
|
||||
runLock.countDown()
|
||||
wait.await()
|
||||
val end = System.currentTimeMillis()
|
||||
return TestResult(start, end, line95, line98, line99)
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package cn.tursom.test
|
||||
|
||||
interface PerformanceTestHandler : () -> Unit {
|
||||
fun logSchedule(percentage: Int) {
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package cn.tursom.test
|
||||
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
data class TestResult(
|
||||
val startTime: Long,
|
||||
val endTime: Long,
|
||||
val line95: Long,
|
||||
val line98: Long,
|
||||
val line99: Long,
|
||||
val usingTime: Long = endTime - startTime,
|
||||
val usingTimeS: Long = TimeUnit.SECONDS.convert(usingTime, TimeUnit.MILLISECONDS),
|
||||
val t95: Long = line95 - startTime,
|
||||
val t98: Long = line98 - startTime,
|
||||
val t99: Long = line99 - startTime,
|
||||
val t95s: Long = TimeUnit.SECONDS.convert(t95, TimeUnit.MILLISECONDS),
|
||||
val t98s: Long = TimeUnit.SECONDS.convert(t98, TimeUnit.MILLISECONDS),
|
||||
val t99s: Long = TimeUnit.SECONDS.convert(t99, TimeUnit.MILLISECONDS)
|
||||
) {
|
||||
}
|
Loading…
Reference in New Issue
Block a user