Background: I am working on a spring web application that provides an optimization algorithm to the users. The performance of my algorithm is acceptable when executed in a pure java project. However, calling it via a rest controller in a spring project leads to a massive performance gap which I would like to understand.
Test/Observation: Of course, all tests are executed on the same hardware. The optimization algorithm is independent of spring, i.e. it receives some basic java objects as input and returns a result. As you can see in the table below, executing my algorithm takes at least twice as long inside spring which holds for 5 iterations and for 100 iterations (i.e. no constant overhead in the beginning).
| Iterations [No.] | Pure Java [ms] | Spring [ms] |
|---|---|---|
| 5 | ~200 | ~ 400 |
| 100 | ~1400 | 3500 - 5000 |
I totally understand that a web application leads to some overhead on my machine (spring, angular, etc.). However, would you really expect such a massive performance impact? Essentially, this is only a small test data set and real world data sets will be more challenging.
To explain how I measured this in more detail:
- I have an
Optimization.classwhich contains apublic static Result optimize(Data data){}.Data.classis a POJO without any reference to spring entities. optimize()is an evolutionary algorithm with 5 or 100 iterations. The time given above is only measured insideoptimize()but around the loop of iterations usingSystem.currentTimeInMillis(). Hence, my method is only triggered once but executed for different number of iterations and I am only measuring how long these iterations in my algorithm take.Pure Javameans that I created apublic static void main(String[] args)that preparesData.classand callsoptimize().Springmeans that I created a rest controller that preparesData.classand callsoptimize().
Main Questions: I would highly appreciate any help to understand this issue in more detail. Currently I am thinking about the following questions:
- Which factors influence the performance of a method executed inside a spring rest controller?
- Can I somehow configure how the given hardware is used by spring?
- Is there any alternative to access an algorithm from the client, i.e. some alternative to the rest controller?
- Would it help to execute my algorithm in some asynchronous way and call the server again later to receive the result?