fun runProfiler()

in ktor-server/ktor-server-benchmarks/src/jmh/kotlin/io/ktor/server/benchmarks/BenchmarkRunner.kt [71:111]


fun runProfiler(settings: BenchmarkSettings) {
    settings.benchmarks.forEach { (clazz, method) ->
        println("${clazz.name}.${method ?: "*"}")
        val instance = clazz.getConstructor().newInstance()
        val setups = clazz.methods.filter { it.annotations.any { it.annotationClass == Setup::class } }
        val teardowns = clazz.methods.filter { it.annotations.any { it.annotationClass == TearDown::class } }
        val allBenchmarks = clazz.methods.filter { it.annotations.any { it.annotationClass == Benchmark::class } }
        val benchmarks = if (method == null) allBenchmarks else allBenchmarks.filter { it.name == method }

        if (setups.isNotEmpty()) {
            println("Setting up…")
            setups.forEach { it.invoke(instance) }
        }

        println("Warming up…")
        benchmarks.forEach { it.invoke(instance) }

        if (settings.threads == 1) {
            println("Running $numberOfOperations iterations…")
            instance.executeBenchmarks(benchmarks, numberOfOperations)
        } else {
            val iterationsPerThread = numberOfOperations / settings.threads
            println("Running ${settings.threads} threads with $iterationsPerThread iterations per thread…")
            val threads = (1..settings.threads).map { index ->
                thread(name = "Test Thread $index") {
                    println("Started thread '${Thread.currentThread().name}'")
                    instance.executeBenchmarks(benchmarks, iterationsPerThread)
                    println("Finished thread '${Thread.currentThread().name}'")
                }
            }
            threads.forEach {
                it.join()
            }
        }

        if (teardowns.isNotEmpty()) {
            println("Tearing down…")
            teardowns.forEach { it.invoke(instance) }
        }
    }
}