Code is slow when running in Azure functions HTTP Trigger

Joaquin van Loon 21 Reputation points
2021-03-12T12:40:03.067+00:00

I stumbled upon the issue that the Java (and Kotlin) code I execute takes really long when called from a HttpTrigger. If I just execute code in the main method of a class it is significantly faster. I am not sure what causes this time difference.

To isolate the issue I followed the tutorial here to create a functions app in Java.
After creating the project with "mvn archetype:generate -DarchetypeGroupId=com.microsoft.azure -DarchetypeArtifactId=azure-functions-archetype -DjavaVersion=8"
I altered the Function class as follows:

77188-image.png

Now if I run the function with "mvn azure-functions:run" and send a post request to "http://localhost:7071/api/HttpExample" the code in the run method will execute and the print statement on line 35 wil say something around 4000, which means the whole execution from line 31 to line 35 takes approximately 4000ms.

If I now run the code from the main method it takes around 6ms (this is what the print on line 44 is telling). This is expected behaviour, since the code I execute is not really complex.

Why is there such a large difference between the different ways of executing the code? Am I missing something?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,323 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pramod Valavala 20,591 Reputation points Microsoft Employee
    2021-03-15T14:40:10.81+00:00

    @Joaquin van Loon The difference you observe is because of the Java Worker JVM options, specifically the -XX:TieredStopAtLevel=1 which uses the C1 JIT Compiler which has lesser optimizations in place.

    If you were to run the main code using this flag, you should see similar results.

    You could just add a JAVA_OPTS app setting with value -XX:TieredStopAtLevel=4 and see it improve. I believe the setting is the way it is for quicker startup times but you could initiate a more insightful discussion on it its decision at the Java Worker Repo.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful