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

Add cache for DateTimeFormatter for @Format annotations #10194

Closed
wants to merge 3 commits into from

Conversation

altro3
Copy link
Contributor

@altro3 altro3 commented Dec 4, 2023

Minor performance improvement and memory reducer for TimeConverterRegistrar. Don't need to recreate custom DateTimeFormatter for every request / response, we can cache it.

This simple test show timings with cache and without cache:

@MicronautTest
@Property(name = "spec.name", value = "DateFormatterTest")
@Property(name = "micronaut.http.client.read-timeout", value = "30s")
class DateFormatterTest {

    private static final Logger log = LoggerFactory.getLogger(DateFormatterTest.class);

    @Inject
    MyGetClient myGetClient;

    @Test
    void test() {

        var date = LocalDate.now();
        var size = 1_000_000;
        var results = new ArrayList<Integer>(size);

        for (var i = 0; i < size; i++) {
            var startTime = System.nanoTime();
            myGetClient.formatDateTime(date);
            results.add((int) (System.nanoTime() - startTime));
        }

        results.sort(Comparator.naturalOrder());

        var sum = 0;

        // remove 5% of the best timings and 5% of the worst timings
        for (var i = size / 20; i < size - (size / 20); i++) {
            sum += (results.get(i) / 1000);
        }

        log.info("!!!!!!!!!!!!!! time = {}", Duration.ofNanos(sum * 1000L));
    }

    @Requires(property = "spec.name", value = "DateFormatterTest")
    @Client("/get")
    interface MyGetClient {

        @Get("/dateTime/{myDate}")
        String formatDateTime(@Format("yyyy-MM-dd") LocalDate myDate);
    }

    @Requires(property = "spec.name", value = "DateFormatterTest")
    @Controller("/get")
    static class GetController {

        @Get("/dateTimeQuery")
        String formatDateTimeQuery(@QueryValue @Format("yyyy-MM-dd") LocalDate myDate) {
            return myDate.toString();
        }
    }
}

Test results:

Requests count: 100_000
With cache:  PT21.427236S
Without cache: PT21.560763S

Requests count: 1_000_000
With cache: PT3M21.53591S
Without cache: PT3M24.615995S

@altro3 altro3 force-pushed the date-formatter-cache branch 3 times, most recently from 6c2bc67 to a33da56 Compare December 4, 2023 09:44
@dstepanov
Copy link
Contributor

Please target 4.3

@altro3 altro3 force-pushed the date-formatter-cache branch from a33da56 to 237001b Compare December 4, 2023 10:09
@altro3 altro3 changed the base branch from 4.2.x to 4.3.x December 4, 2023 10:09
@@ -30,14 +30,14 @@ jakarta-el-impl = "5.0.0-M1"
jcache = "1.1.1"
junit5 = "5.9.3"
junit-platform="1.9.3"
logback = "1.4.13"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please cleanup updates

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@altro3 altro3 force-pushed the date-formatter-cache branch from 237001b to 038f34c Compare December 4, 2023 13:52
@altro3 altro3 closed this Dec 4, 2023
@altro3 altro3 deleted the date-formatter-cache branch December 4, 2023 13:57
@altro3
Copy link
Contributor Author

altro3 commented Dec 4, 2023

@dstepanov continue here: #10198

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants