Wednesday, August 12, 2009

Grails : performance tuning using caching

Hi friends,

In one of our projects, we had lot of performance issues with the portal. That project had all the data loaded in the memory, so we were not concerned about database hits but the huge data being transferred. So after careful thought process and observations, we got to know following facts :

1. To many requests were made to the server (approx 110)
2. Every time connection opening & closing consumed lot of time.
3. File size were quite heavy. Files like js, images, css

We used Firebug, a firefox addon to identify the first problems. So in a process to improve performance we implemented three things

1. Not open & close connection on every request : For this to happen, we created filters and in before filter we added the following code :
response.setHeader('Connection','keep-alive')
2. Caching the response : To cache the response, we added the following code to the before filter :
response.setHeader('CacheControl', "public") response.setDateHeader('Expires', System.currentTimeMillis() +60*1 000)
3. Reduce the size of files to be loaded : To do this, we zipped all the files and set the response header again in before filters as given in the following code :
response.addHeader('Accept-Encoding','gzip, deflate')

Page download time reduced to 1/5th of the original time after using these techniques.

There is another option that can be explored, which is ETAG, which saves us from downloading the file again, if there are no changes from the last response.

Hope this helped. Please share your thoughts to improve the performance.

Cheers!

Regards,
~~Amit Jain~~
amitjain1982@gmail.com







Grails - multiple layouts

Hi Friends,

The small project that I worked had many different layouts, like different for home and rest of the pages. So we generally make use of main.gsp to define the basic layout of the application which is common to all gsps. So to cater to different layouts issue, I created multiple(as required) gsps just like main.gsp in the layouts folder. And wherever a particular layout was needed corresponding gsp was included in the meta tag. For example

For home page layout - mainHome.gsp and referred it as < meta name="layout" content="mainHome"/ >

For admin page layout - mainAdmin.gsp and referred it as < meta name="layout" content="mainAdmin"/ > and like ways.

So this saved me from writing the same code again and again.

Cheers!
~~Amit Jain~~
amitjain1982@gmail.com

Friday, August 7, 2009

Grails : custom error page on any exception

Hi Friends,

To display the custom error page on production instead of any exception that might occur, We can use modify error.gsp file. Error.gsp file is the one which is executed anytime any exception occurs.
As grails enables us to configure the application in development, production and testing environments. We can use the same error.gsp file and render certain gsp as per the current environment.

< html >
< g:if test="${GrailsUtil.isDevelopmentEnv()}" >
< head >
< title >Grails Runtime Exception< /title >...
< /head >< body > ...
...< !-- Default error.gsp body part -- >
< /body >
< /g:if >
< g:else >
< g:render template="/errorPage"/ >
< /g:else >
< /html >

In the above code, in development environment the error.gsp would execute the default code but in any other environment it would render the template "errorPage", which is our custom user friendly errorPage template.

Cheers!
~~Amit Jain~~
amitjain1982@gmail.com

Grails : How to set value in FckEditor

Hi Friends,

I was using fckeditor plugin, where I found that when I wanted to display the data from the database in the fckeditor, it appeared but it displayed the html code as well and with no formatting as it didn't render the html code. So the workaround I found is shared below :

< fckeditor:editor
id="content" name="content"
width="600"
height="400"
toolbar="Standard"
fileBrowser="default"
value="${myBean.content}"
>
${myBean.content}
< /fckeditor:editor >

Hope this helped.

Cheers!
~~Amit Jain~~
amitjain1982@gmail.com