Wednesday, January 12, 2011

FreeMarker vs. GSP for Dynamic Template Rendering

I find out that there are two ways in Grails to render dynamic template (String template), using FreeMarker and GSP. FreeMarker support enabled by freemarker plugin and freemarker-tags plugin. Special thanks to Jeff and Daniel creating these plugins, it will be less options for Grails view technology without their contribution. Given two options, I need to perform some micro-benchmarking to find out which one is the appropriate technology for given requirements.


The Objective
To evaluate and decide which template engine (FreeMarker or GSP) to use as Form Template of the Grails Form Builder Plugin.


The Result
FreeMarker is the winner. By perform rendering 100 times for the same template, GSP need 119,946ms and FreeMarker need approximately 1/4 of GSP, 32,749ms (Surprise? Yes, I am surprised about the performance of FreeMarker over GSP in dynamic template rendering).


How I Did It 
The Benchmark Case
The dynamic template was created based on the following domain class:
package test_freemarker2

class Test1 {
    int prop1
    String prop2
    Date prop3
   
    static constraints = { prop2 blank:false }
}


The FreeMarker's template rendering code:
new Template("freemarkerTemplate",
             new StringReader(freemarkerTemplateCode),
             freemarkerConfig.configuration)
            .process([test1Instance: test1Instance, flash:flash], out)


The GSP's template rendering code:
groovyPagesTemplateEngine.createTemplate(gspTemplateCode, 'gspTemplate').make([test1Instance: test1Instance]).writeTo(out)

As the template code (freemarkerTemplateCode and gspTemplateCode) was too lengthy, I will not describe it here (You can download the complete source code of the application here to run the benchmark test in your own environment).

The Benchmark Application
I run the benchmarking tests with the following steps:
  1. Start the application using grails run-war.
  2. After the application was started, open or refresh the URL http://localhost:8080/freemarker-vs-gsp/benchmark/index in your browser. You will see the following screen:
  3. Click on "FreeMarker" link and wait for test completion and shutdown the application. Do the same for "GSP", start from step 1.
The Environment
Operating System: Puppy Linux 4.3.1
JDK:  1.6.0_20
Grails: 1.3.6
Processor: Intel Pentium M 1.5GHz
Memory: 1285MB

The Source Code
You can download the complete source code of the application and run it yourself.

Conclusion
Even the winner of this benchmark test is FreeMarker, the benchmark code was written based on my limited knowledge of FreeMarker and GSP. You may have better way to write optimized code than I do, please let's me know if you found a better way.

Edited on 28 Jun 2012: Please see the part 2 of this post.

    Like this blog?

    Thanks for visiting! If you like what you see, I'd really appreciate you linking to it or otherwise sharing it with people you think would find it useful.

    4 comments:

    Søren Berg said...

    I was wondering, if you have tried it with pre-compiled GSP code?
    Was the performace still the same?

    potter_ru said...

    When I moved template creation out of loop, the GSP engine was .

    Template creation located inside loop:

    FreeMarker 100 times completed in 6589ms.
    GSP 100 times completed in 33391ms.


    Template creation located outside loop:

    FreeMarker 100 times completed in 6386ms.
    GSP 100 times completed in 950ms.

    Lari Hotari said...

    Please read my comments on the mailing list:
    http://grails.1312388.n4.nabble.com/FreeMarker-vs-GSP-for-Dynamic-Template-Rendering-tp3213939p3214347.html

    Lim Chee Kin said...

    Thanks for writing.

    @Søren Berg, GSP pre-compilation is not an option as the template text is dynamically generated.

    @potter_ru, thanks to break the benchmark test to template creation and template rendering. Your test result prove that FreeMarker is faster in template creation and GSP is faster in template rendering. My requirement is more on template creation.

    @Lari Hotari, I will see your comments there, thanks.