When I run my function in junit test i have time about 1000 faster then with bootRun. When I set in build.gradle.kts
tasks.getByName<org.springframework.boot.gradle.tasks.run.BootRun>("bootRun") {
isOptimizedLaunch = false
}
time is equal then test run. Why it happens? How I can optimize bootJar? bootJar hasn't parameter isOptimizedLaunch
Function example
package com.example.demo
import java.util.concurrent.TimeUnit
class LongFun {
fun main() {
val startTime = System.nanoTime()
for (i1 in 0..10000) {
longFun(1000)
}
val endTime = System.nanoTime()
val durationInNano = endTime - startTime //Total execution time in nano seconds
var durationInMillis = TimeUnit.NANOSECONDS.toMillis(durationInNano)
println("time=$durationInMillis")
}
fun longFun(n: Int) {
for (i2 in 0..n) {
for (i3 in 0..n) {
var a = 1
}
}
}
}
The full code I did with https://start.spring.io/ (gradle, kotlin) and didn't change anything
UPDATE1. It is a simple example, in real the same problem in my app with optaplanner, it also has many calculations.
UPDATE2. With maven i have the same bad result
- In test - 2ms
- In gradle with isOptimizedLaunch = false - 2ms
- In gradle without isOptimizedLaunch = false - 5245ms
- In maven - 5146ms
From what I can tell, the
optimizedLaunch
property only adds the following two arguments to the JVM startup command and nothing else:-XX:TieredStopAtLevel=1
-Xverify:none
The latter is not used if you are on Java 13 or later.
These arguments are used to to make the JVM startup quicker for development and testing purposes. This comes at the cost of restrictions on the JVM ability to optimize itself at runtime. It is not unusual to see such slow performance when disabling runtime optimizations.
However, if you disable optimzedLaunch to prioritize runtime performance, you should also see the startup time on the JVM increase. On one of my Spring Boot projects, startup time went from ~7s to 13s. So on an actual project, in the development phase, you have to decide it you want to optimize for fast startup or fast runtime.
When you are at a point where you will run your application in a production setting, you will most likely use the jar file created by the
bootJar
(ordist
) task. Here you are responsible for providing the JVM arguments. If you don't provide any, you will get the default behaviour of the JVM which is to use tiered compilation (corresponding tooptimzedLaunch=false
).