Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime parsing slow in Spring Boot #40078

Closed
larsaane opened this issue Mar 22, 2024 · 4 comments
Closed

DateTime parsing slow in Spring Boot #40078

larsaane opened this issue Mar 22, 2024 · 4 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@larsaane
Copy link

larsaane commented Mar 22, 2024

Parsing datetime strings when running Spring Boot is slow compared to plain Java.
Please look at my example code below.
Is this performance degradation using Spring a bug, or is it normal that Spring have this much overhead?
The Spring perfomance is better with Temurin 21 compared to Temurin 17, but still slow compared to plain java.

Prerequisite:
Using a Spring Boot 3.2.3 demo created with Spring Initializr.
Running examples with IntelliJ IDEA 2023.3.6

Code example used with Spring Boot and with plain Java project:

        long start = System.currentTimeMillis();
        for (int i = 0; i < 3000000; i++) {
            String dateTime = "2024-03-22T12:34:56.123+00:00";
            ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTime, ISO_OFFSET_DATE_TIME);
        }
        long end = System.currentTimeMillis();
        System.out.println("Total parse time ms: " + (end - start));

Measurements:
Spring Boot 3.2.3 - Java Temurin 17
Total parse time ms: 9330
Spring Boot 3.2.3 - Java Temurin 21:
Total parse time ms: 4225

Plain Java project - Java Temurin 17
Total parse time ms: 1547
Plain Java project - Java Temurin 21:
Total parse time ms: 1648

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 22, 2024
@mhalbritter
Copy link
Contributor

There's no Spring Boot code involved in this, so I'm not sure what we can do about that?

Also, your approach to benchmarking is flawed. You should use something like JMH.

@mhalbritter mhalbritter added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 22, 2024
@mhalbritter mhalbritter closed this as not planned Won't fix, can't repro, duplicate, stale Mar 22, 2024
@larsaane
Copy link
Author

This is the Spring code involved:

@Component
public class DatePerformanceTest {
    public DatePerformanceTest() {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 3000000; i++) {
            String dateTime = "2024-03-22T12:34:56.123+00:00";
            ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTime, ISO_OFFSET_DATE_TIME);
        }
        long end = System.currentTimeMillis();
        System.out.println("Total parse time ms: " + (end - start));

    }
}

@larsaane
Copy link
Author

Measuring the total time it takes to do the parsing should be just fine, We don't need JMH to see that it takes longer time running the parsing code in Spring compared to plain java.

@wilkinsona
Copy link
Member

This isn't an apples-to-apples comparison and, like @mhalbritter, I don't think it has anything to do with Spring.

When you run the code directly in plain Java, the JVM is in a different state to when the code's run as part of a Spring application. For example, the amount of heap that's available will be different and this will have a knock-on effect on garbage collection. The code that you're measuring creates a significant amount of gargage that has to be collected. When there's less heap available, this collection will have to be performed more often which will slow things down.

You could see this for yourself by running with -verbose:gc and comparing how hard the garbage collector has to work in each case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

4 participants