Regreso del tiempo de servicio al controlador en Grails

We are working on performance in our grails app, and it seems like grails needs much time (7-13ms) to get out of Service back to Controller. The data that is returned is reference to domain objects (Map with 2 references), not very complex. Is there any way to shorten this time?

We have log.debug() before return statement in service, and another one after leaving service, in controller.

2012-02-01 15:16:07,048 [http-8080-1] DEBUG api.TestService test before service return
2012-02-01 15:16:07,063 [http-8080-1] DEBUG api.TestController test after service return

Edit: Grails version 1.3.7

Edit: After turning on hibernate SQL logging:

2012-02-02 09:20:04,504 [http-localhost%2F127.0.0.1-8080-1] DEBUG api.TestService before return
2012-02-02 09:20:04,505 [http-localhost%2F127.0.0.1-8080-1] DEBUG hibernate.SQL select nextval ('hibernate_sequence')
2012-02-02 09:20:04,516 [http-localhost%2F127.0.0.1-8080-1] DEBUG hibernate.SQL insert into test ...
2012-02-02 09:20:04,520 [http-localhost%2F127.0.0.1-8080-1] DEBUG hibernate.SQL update test1 ...
2012-02-02 09:20:04,522 [http-localhost%2F127.0.0.1-8080-1] DEBUG hibernate.SQL insert into test_test1 ...
2012-02-02 09:20:04,524 [http-localhost%2F127.0.0.1-8080-1] DEBUG api.TestController after service

preguntado el 01 de febrero de 12 a las 14:02

Have you tried stepping through the code with a debugger, going down inside the grails guts, to see where it's going? -

Can you provide some for info what are you doing in the service? How many elements has the map? Code snippets? -

Seriously? You're concerned about 7 to 13 milisegundos ? -

doh! 7 to 13 milliseconds in an api which takes 60ms is 1/5 of time! 100 requests with 10ms wasted equal to a wasted second :) -

something on the topic: would the contents of map matter? It should be reference passed and not the object itself. -

2 Respuestas

The overhead might be caused by Spring building a transaction context around the service call (Grails default, see If your service doesn't require (database) transactions, make sure you add

static transactional = false 

en el servicio.

If you do need transactions and you are doing a lot of service calls from a controller, it's worth moving them to a service so that you have the minimum amount of transactions to deal with. (If you absolutely want to keep them in the controller, withTransaction block can be used to do several service calls in a single transaction.)

Respondido 02 Feb 12, 01:02

Ustedre right. After adding some more logging weve noticed, the transaction ends when leaving Service, and the inserts and updates last so long. I`ve edited the question with logs after turning on hibernate SQL debug. - Maciej

7 to 13 milliseconds is pretty much instantaneous. You're wasting your time trying to reduce this further. Surely you must have other more important things to do?

Even if this is your most pressing concern, there seems little point in spending time on it, because there's really nothing you can do about it, because it's Grails/Spring code (rather than yours) than executes between the service and controller.

Respondido 02 Feb 12, 00:02

So you're saying that 7 to 13 milliseconds to return from one method to another is ok? It's not the API call that takes 7-13ms but just going from service to controller. So in order to meet the SLAs this is one of the things to look into. - Krystian

Yep. If you're concerned about millisecond performance, you should be coding your application in finely tuned assembly language. - Burt Beckwith

Burt, I don't agree.I think you are going from one extreme [don't care about performance] to another [performance is the king].What we want to do is to write in something flexible as Grails and squeeze from it as much as we can.When doing so we are looking for bottlenecks which are easy to fine tune [like for example moving long loops to java]. We have found that in simple return from service to controller we are loosing more than 1/4 time of some apis.Since we couldn't find why, we decided to ask. Do tell, how is this wrong?It would be if it was a webapp for 10 users, but that's not the case. - Krystian

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.