Quantcast

FreeMarker vs. GSP for Dynamic Template Rendering

classic Classic list List threaded Threaded
34 messages Options
12
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

FreeMarker vs. GSP for Dynamic Template Rendering

Lim Chee Kin
Hi there,

I perform some micro-benchmarking for the subject above.

The result was surprising. 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. Please see blog post at http://limcheekin.blogspot.com/2011/01/freemarker-vs-gsp-for-dynamic-template.html to find out more.

Please let's me know what do you think, I welcome your input and feedback.

Regards,
Chee Kin
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

tomas lin
I wonder if you are using the optimal call for GSPs. If I recall
correctly, you should be able to precompile your gsp files using the
ant.gspc command and passing these classes to your GSP files in
groovyPagesTemplateEngine. This would be closer to how GSPs are
actually processed and used in production.



On Wed, Jan 12, 2011 at 1:38 PM, Lim Chee Kin <[hidden email]> wrote:

>
> Hi there,
>
> I perform some micro-benchmarking for the subject above.
>
> The result was surprising. 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. Please see blog post at
> http://limcheekin.blogspot.com/2011/01/freemarker-vs-gsp-for-dynamic-template.html
> to find out more.
>
> Let's me know what you think! I welcome your input and feedback.
>
> Regards,
> Chee Kin
> --
> View this message in context: http://grails.1312388.n4.nabble.com/FreeMarker-vs-GSP-for-Dynamic-Template-Rendering-tp3213939p3213939.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Daniel Henrique Alves Lima
In reply to this post by Lim Chee Kin
A looooooong time ago i've did a similar comparison using precompiled jsp and config optimized velocity and freemarker. Guess what?

Em Quarta 12 de Jan de 2011 11:54 BRST Tomas Lin escreveu:

>I wonder if you are using the optimal call for GSPs. If I recall
>correctly, you should be able to precompile your gsp files using the
>ant.gspc command and passing these classes to your GSP files in
>groovyPagesTemplateEngine. This would be closer to how GSPs are
>actually processed and used in production.
>
>
>
>On Wed, Jan 12, 2011 at 1:38 PM, Lim Chee Kin <[hidden email]> wrote:
>>
>> Hi there,
>>
>> I perform some micro-benchmarking for the subject above.
>>
>> The result was surprising. 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. Please see blog post at
>> http://limcheekin.blogspot.com/2011/01/freemarker-vs-gsp-for-dynamic-template.html
>> to find out more.
>>
>> Let's me know what you think! I welcome your input and feedback.
>>
>> Regards,
>> Chee Kin
>> --
>> View this message in context: http://grails.1312388.n4.nabble.com/FreeMarker-vs-GSP-for-Dynamic-Template-Rendering-tp3213939p3213939.html
>> Sent from the Grails - user mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>---------------------------------------------------------------------
>To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>





---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Lim Chee Kin
Hi tomas lin,

Thanks for quick response.

Pre-compilation is not an option as the template is dynamically generated as stated in objective of the blog post. Anyway, I would like to explore if you aware any other better option.

Regards,
Chee Kin
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Lari Hotari -
In reply to this post by Lim Chee Kin
12.01.2011 15:38, Lim Chee Kin wrote:
Hi there,

I perform some micro-benchmarking for the subject above. 

The result was surprising. 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. Please see blog post at
http://limcheekin.blogspot.com/2011/01/freemarker-vs-gsp-for-dynamic-template.html
to find out more.

Let's me know what you think! I welcome your input and feedback.

Regards,
Chee Kin

Freemarker is definitely faster than GSP for dynamic templates that aren't cached (what you tested). Compiling GSP each time is very slow.

After modifying the test to cache templates, I get these results:

  • FreeMarker 100 times completed in 7500ms.
  • GSP 100 times completed in 1614ms.

Here are the changes I made:

    def freemarker = {
        def out = new org.codehaus.groovy.grails.web.pages.FastStringWriter()
        def test1Instance = new Test1(prop2: "This is freemarker test")
        long startTime = System.currentTimeMillis()
        def t=new Template("freemarkerTemplate",
                    new StringReader(freemarkerTemplateCode),
                    freemarkerConfig.configuration)
        for (int i = 0; i < TIMES; i++) {
            t.process([test1Instance: test1Instance, flash:flash], out)
            out.flush()
            println "Executing... $i"
        }
        response.setContentType("text/json;charset=UTF-8")
        long timeTaken = System.currentTimeMillis() - startTime
        render '{"message":"' + TIMES + ' times completed in ' + timeTaken + 'ms."}'
    }
   
    def gsp = {
        def out = new org.codehaus.groovy.grails.web.pages.FastStringWriter()
        def test1Instance = new Test1(prop2: "This is gsp test")
        long startTime = System.currentTimeMillis()
        def t=groovyPagesTemplateEngine.createTemplate(gspTemplateCode, 'gspTemplate')
        for (int i = 0; i < TIMES; i++) {
            t.make([test1Instance: test1Instance]).writeTo(out)
            out.flush()
            println "Executing... $i"
        }
        response.setContentType("text/json;charset=UTF-8")
        long timeTaken = System.currentTimeMillis() - startTime
        render '{"message":"' + TIMES + ' times completed in ' + timeTaken + 'ms."}'
    }
}

Lari

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Lari Hotari -

I forgot to mention that after I made the changes I started the benchmark app with "grails prod run-war" command.
The performance is slightly lower for GSP in "grails run-war" mode (1913ms vs 1614ms for my test runs).

Lari


12.01.2011 18:34, Lari Hotari wrote:
12.01.2011 15:38, Lim Chee Kin wrote:
Hi there,

I perform some micro-benchmarking for the subject above. 

The result was surprising. 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. Please see blog post at
http://limcheekin.blogspot.com/2011/01/freemarker-vs-gsp-for-dynamic-template.html
to find out more.

Let's me know what you think! I welcome your input and feedback.

Regards,
Chee Kin

Freemarker is definitely faster than GSP for dynamic templates that aren't cached (what you tested). Compiling GSP each time is very slow.

After modifying the test to cache templates, I get these results:

  • FreeMarker 100 times completed in 7500ms.
  • GSP 100 times completed in 1614ms.

Here are the changes I made:

    def freemarker = {
        def out = new org.codehaus.groovy.grails.web.pages.FastStringWriter()
        def test1Instance = new Test1(prop2: "This is freemarker test")
        long startTime = System.currentTimeMillis()
        def t=new Template("freemarkerTemplate",
                    new StringReader(freemarkerTemplateCode),
                    freemarkerConfig.configuration)
        for (int i = 0; i < TIMES; i++) {
            t.process([test1Instance: test1Instance, flash:flash], out)
            out.flush()
            println "Executing... $i"
        }
        response.setContentType("text/json;charset=UTF-8")
        long timeTaken = System.currentTimeMillis() - startTime
        render '{"message":"' + TIMES + ' times completed in ' + timeTaken + 'ms."}'
    }
   
    def gsp = {
        def out = new org.codehaus.groovy.grails.web.pages.FastStringWriter()
        def test1Instance = new Test1(prop2: "This is gsp test")
        long startTime = System.currentTimeMillis()
        def t=groovyPagesTemplateEngine.createTemplate(gspTemplateCode, 'gspTemplate')
        for (int i = 0; i < TIMES; i++) {
            t.make([test1Instance: test1Instance]).writeTo(out)
            out.flush()
            println "Executing... $i"
        }
        response.setContentType("text/json;charset=UTF-8")
        long timeTaken = System.currentTimeMillis() - startTime
        render '{"message":"' + TIMES + ' times completed in ' + timeTaken + 'ms."}'
    }
}

Lari


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Arash Sharif
In reply to this post by Lari Hotari -

Have a link that describes how to cache the gsp...also does this mean pre compile like jsp or is it actual cache where if data changes the gsp does not know...

Many thanks

On Jan 12, 2011 8:34 AM, "Lari Hotari" <[hidden email]> wrote:
> 12.01.2011 15:38, Lim Chee Kin wrote:
>> Hi there,
>>
>> I perform some micro-benchmarking for the subject above.
>>
>> The result was surprising. 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. Please see blog post at
>> http://limcheekin.blogspot.com/2011/01/freemarker-vs-gsp-for-dynamic-template.html
>> to find out more.
>>
>> Let's me know what you think! I welcome your input and feedback.
>>
>> Regards,
>> Chee Kin
>
> Freemarker is definitely faster than GSP for dynamic templates that
> aren't cached (what you tested). Compiling GSP each time is very slow.
>
> After modifying the test to cache templates, I get these results:
>
> * FreeMarker
> <http://localhost:8080/freemarker-vs-gsp/benchmark/index#>100
> times completed in 7500ms.
> * GSP <http://localhost:8080/freemarker-vs-gsp/benchmark/index#>100
> times completed in 1614ms.
>
>
> Here are the changes I made:
>
> def freemarker = {
> def out = new
> org.codehaus.groovy.grails.web.pages.FastStringWriter()
> def test1Instance = new Test1(prop2: "This is freemarker test")
> long startTime = System.currentTimeMillis()
> def t=new Template("freemarkerTemplate",
> new StringReader(freemarkerTemplateCode),
> freemarkerConfig.configuration)
> for (int i = 0; i < TIMES; i++) {
> t.process([test1Instance: test1Instance, flash:flash], out)
> out.flush()
> println "Executing... $i"
> }
> response.setContentType("text/json;charset=UTF-8")
> long timeTaken = System.currentTimeMillis() - startTime
> render '{"message":"' + TIMES + ' times completed in ' +
> timeTaken + 'ms."}'
> }
>
> def gsp = {
> def out = new
> org.codehaus.groovy.grails.web.pages.FastStringWriter()
> def test1Instance = new Test1(prop2: "This is gsp test")
> long startTime = System.currentTimeMillis()
> def t=groovyPagesTemplateEngine.createTemplate(gspTemplateCode,
> 'gspTemplate')
> for (int i = 0; i < TIMES; i++) {
> t.make([test1Instance: test1Instance]).writeTo(out)
> out.flush()
> println "Executing... $i"
> }
> response.setContentType("text/json;charset=UTF-8")
> long timeTaken = System.currentTimeMillis() - startTime
> render '{"message":"' + TIMES + ' times completed in ' +
> timeTaken + 'ms."}'
> }
> }
>
> Lari
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Lari Hotari -

When you are using GSP in the view layer in an ordinary way, you don't have to separately cache or precompile GSPs. They are always precompiled by default in Grails 1.2.x and 1.3.x when a war file is generated.

--

For special uses cases where the GroovyPagesTemplateEngine is used as a "dynamic template" engine, the situation is different.

I personally haven't used GroovyPagesTemplateEngine directly in the Grails applications I have built.
I believe that when GroovyPagesTemplateEngine is used directly to compile&render "dynamic templates", you should keep a reference to the returned groovy.text.Template instance in some cache.

Could someone else explain (or contribute/point documentation about) how it should be used?

I remember that weceem & bean-fields are good examples of "dynamic templates" and the mail plugin is a example of GSP view rendering (outside of the web request). (Marc or Luke, could you share some experiences?)

There are 2 Jira issues open related to using GroovyPagesTemplateEngine directly:
http://jira.codehaus.org/browse/GRAILS-3818
http://jira.codehaus.org/browse/GRAILS-6809


Lari


12.01.2011 18:46, Arash Sharif wrote:

Have a link that describes how to cache the gsp...also does this mean pre compile like jsp or is it actual cache where if data changes the gsp does not know...

Many thanks

On Jan 12, 2011 8:34 AM, "Lari Hotari" <[hidden email]> wrote:
> 12.01.2011 15:38, Lim Chee Kin wrote:
>> Hi there,
>>
>> I perform some micro-benchmarking for the subject above.
>>
>> The result was surprising. 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. Please see blog post at
>> http://limcheekin.blogspot.com/2011/01/freemarker-vs-gsp-for-dynamic-template.html
>> to find out more.
>>
>> Let's me know what you think! I welcome your input and feedback.
>>
>> Regards,
>> Chee Kin
>
> Freemarker is definitely faster than GSP for dynamic templates that
> aren't cached (what you tested). Compiling GSP each time is very slow.
>
> After modifying the test to cache templates, I get these results:
>
> * FreeMarker
> <http://localhost:8080/freemarker-vs-gsp/benchmark/index#>100
> times completed in 7500ms.
> * GSP <http://localhost:8080/freemarker-vs-gsp/benchmark/index#>100
> times completed in 1614ms.
>
>
> Here are the changes I made:
>
> def freemarker = {
> def out = new
> org.codehaus.groovy.grails.web.pages.FastStringWriter()
> def test1Instance = new Test1(prop2: "This is freemarker test")
> long startTime = System.currentTimeMillis()
> def t=new Template("freemarkerTemplate",
> new StringReader(freemarkerTemplateCode),
> freemarkerConfig.configuration)
> for (int i = 0; i < TIMES; i++) {
> t.process([test1Instance: test1Instance, flash:flash], out)
> out.flush()
> println "Executing... $i"
> }
> response.setContentType("text/json;charset=UTF-8")
> long timeTaken = System.currentTimeMillis() - startTime
> render '{"message":"' + TIMES + ' times completed in ' +
> timeTaken + 'ms."}'
> }
>
> def gsp = {
> def out = new
> org.codehaus.groovy.grails.web.pages.FastStringWriter()
> def test1Instance = new Test1(prop2: "This is gsp test")
> long startTime = System.currentTimeMillis()
> def t=groovyPagesTemplateEngine.createTemplate(gspTemplateCode,
> 'gspTemplate')
> for (int i = 0; i < TIMES; i++) {
> t.make([test1Instance: test1Instance]).writeTo(out)
> out.flush()
> println "Executing... $i"
> }
> response.setContentType("text/json;charset=UTF-8")
> long timeTaken = System.currentTimeMillis() - startTime
> render '{"message":"' + TIMES + ' times completed in ' +
> timeTaken + 'ms."}'
> }
> }
>
> Lari
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Daniel Henrique Alves Lima
        Hi, everybody. I think i've missed something...

        Chee Kin, you're trying to simulate the parsing and execution of
*different* templates N times, but you just don't want to bother
yourself creating a lot of different templates. Is this right?
        If this is true, your original source code makes more sense to me:

for (int i = 0; i < TIMES; i++) {
  new Template("freemarkerTemplate",
     new StringReader(freemarkerTemplateCode),
     freemarkerConfig.configuration)
                .process([test1Instance: test1Instance, flash:flash], out)
  out.flush()
  println "Executing... $i"
}

        Than the modified version of it:

def t =  new Template("freemarkerTemplate",
     new StringReader(freemarkerTemplateCode),
     freemarkerConfig.configuration)

for (int i = 0; i < TIMES; i++) {
  t.process([test1Instance: test1Instance, flash:flash], out)
  out.flush()
  println "Executing... $i"
}



        Have you considered other aspects like memory usage, concurrent
hits/second, concurrent users and etc? Or are theses aspects lesser
important to you in this context?
        BTW, i'm not sure if this will be significant, but freemarker-tags
plugin can be harming your tests. You can try to disable "auto
import" (you will be forced to rewrite your ftl):

http://grails-freemarker-tags-plugin.googlecode.com/svn/doc/manual/guide/3.%20Miscellaneous.html#3.1.%20Configuration


        Best regards,

                Daniel.





---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Daniel Henrique Alves Lima
        Maybe there are some useful information here (the plugin uses multiple
template loaders):

http://freemarker.org/docs/api/freemarker/template/Configuration.html#setTemplateUpdateDelay(int)
http://freemarker.org/docs-24pre1/pgui_config_templateloading.html#autoid_40
http://freemarker.org/docs-24pre1/pgui_config_templateloading.html#pgui_config_templateloading_caching


        But, like i've said before, i'm not a FreeMarker expert.

        Best regards,

                Daniel.


On Wed, 2011-01-12 at 20:22 -0200, Daniel Henrique Alves Lima wrote:
> Hi, everybody. I think i've missed something...
>
(...)


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Daniel Henrique Alves Lima
        GSP parsing seems to create a lot of objects because memory usage
increases fast (i've got an OutOfMemoryError sometimes). If memory usage
is high, Garbage Collector can be affecting the performance of your GSP
test.


On Wed, 2011-01-12 at 20:54 -0200, Daniel Henrique Alves Lima wrote:
> Maybe there are some useful information here (the plugin uses multiple
> template loaders):
(...)


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Lim Chee Kin
Hi Lari,

Thanks for writing, running the test and providing code snippet. The benchmark test could be break down to template creation and template rendering. Your test result prove that GSP is faster in template rendering, but FreeMarker is faster in template creation as per comments of potter_ru in my blog post. I re-post his comment here:
--------------------------------------------------------------------------------------------
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.
---------------------------------------------------------------------------------------------

Hi Daniel,

Thanks for clarify my intention of the test, my requirement need to tackle both template creation and template rendering, that's the reason I put them inside loop. Thanks for sharing your experience, possible optimization options and helpful links.

Regards,
Chee Kin

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Daniel Henrique Alves Lima
http://freemarker.624813.n4.nabble.com/Possible-room-for-performance-improvement-when-using-TemplateDirectiveModel-and-TemplateMethodModelEx-td3217295.html



On Wed, 2011-01-12 at 18:25 -0800, Lim Chee Kin wrote:
(...)

> Hi Daniel,
>
> Thanks for clarify my intention of the test, my requirement need to
> tackle both template creation and template rendering, that's the
> reason I put them inside loop. Thanks for sharing your experience,
> possible optimization options and helpful links.
>
> Regards,
> Chee Kin
>
>



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Marc Palmer Local
In reply to this post by Lim Chee Kin

On 13 Jan 2011, at 02:25, Lim Chee Kin wrote:

> Hi Lari,
>
> Thanks for writing, running the test and providing code snippet. The benchmark test could be break down to template creation and template rendering. Your test result prove that GSP is faster in template rendering, but FreeMarker is faster in template creation as per comments of potter_ru in my blog post. I re-post his comment here:
> --------------------------------------------------------------------------------------------
> 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.
> ---------------------------------------------------------------------------------------------
>

This is almost certainly because GSP generates source code and then runs the groovy compiler on it, then loads the class with classloader to use it.

I'm guessing that Freemarker does not compile to java bytecodes and just maintains a form of AST in memory, at least Velocity (and its predecessor WebMacro) did this. It means template parse is very fast, but the trade off is execution is in effect "interpreted" and hence slower. Which seems to be the results you are seeing.

I used to contribute a little to WebMacro way back, before Apache forked it to Velocity (boo hiss).

As a result I used to always think "Wouldn't it be nice if Grails used something like Velocity / Freemarker instead of GSP".

It is folly. GSP is excellent. There are things that would be very difficult or impossible to do in non-GSP view tech due to the power of Groovy, without a bunch of specific additions to that view tech to support Groovy expressions etc.

Give it up - GSP is great, and thanks to Lari's hard work is getting faster all the time.

Also note that 100 executions is not significant at all for performance testing. You need 10000s of requests to get the JVM to optimize code correctly over time. You will see the time keeps going done the more you run, until it hits a reasonably stable level.

Marc
~ ~ ~
Marc Palmer
Freelancer (Grails/Groovy/Java)

Blog         > http://www.anyware.co.uk
Twitter      > http://twitter.com/wangjammer5
Grails Rocks > http://www.grailsrocks.com








---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Daniel Henrique Alves Lima
        Hi, Marc.

On Fri, 2011-01-14 at 09:40 +0000, Marc Palmer wrote:
> On 13 Jan 2011, at 02:25, Lim Chee Kin wrote:
>
> >
>
> I'm guessing that Freemarker does not compile to java bytecodes and just maintains a form of AST in memory,
> at least Velocity (and its predecessor WebMacro) did this.
> It means template parse is very fast, but the
> trade off is execution is in effect "interpreted" and hence slower. Which seems to be the results you are seeing.

        I have no idea how FreeMarker works. I've made some simple tests (a
long time ago) and FreeMarker was better or equivalent to a compiled
JSP. But JSP generation is too dependent on your Web
Container/Application Server and the tests were too simple to be
considered.


> As a result I used to always think "Wouldn't it be nice if Grails used something like Velocity /
> Freemarker instead of GSP".
>
> It is folly. GSP is excellent. There are things that would be very difficult or impossible
> to do in non-GSP view tech due to the power of Groovy, without a bunch of specific additions
>  to that view tech to support Groovy expressions etc.
>
> Give it up - GSP is great, and thanks to Lari's hard work is getting faster all the time.

        So do you think it is useless support FreeMarker as an option?

        I agree GSP is probably more powerful than FreeMarker and i think i
don't mind to use one or another, but i've 2 observations:

        1. I'm not sure why but some non-Java developers prefer
Velocity/FreeMarker syntax over JSP (i don't know if this holds true for
GSP). For them it is easier to write a macro than write a TagLib;
        2. If parsing is faster in FreeMarker then FreeMarker will be better at
development stage, when there is a lot of change template/refresh
browser activities. GSP will be better at production.

       
        I'm just guessing here. There is no concrete data about it.


        Best regards,

                Daniel.



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Daniel Henrique Alves Lima
In reply to this post by Lari Hotari -
        I don't believe micro-benchmarking are meaningful (because they are too
simple), but i think the poor performance in Lari's version could be my
fault.

        Chee Kin, how do you check if ftl and gsp are producing the same html?
        Try this (after your first tests, change auto import to true and try
again):


== grails-app/conf/Config.groovy ==

grails.plugins.freemarkertags = [
  autoImport: false,      
  defineFunctions: true    
]


==
grails-app/controllers/org/grails/benchmark/BenchmarkController.groovy
==

        static int TIMES = 10000

        static String freemarkerTemplateCode = '''\
[#ftl/]
<html>
abc=${test1Instance.prop2}
</html>
 '''  
        static String gspTemplateCode = '''\
<html>
abc=${test1Instance.prop2}
</html>


// Lari's version
    def freemarker = {
        def out = new
org.codehaus.groovy.grails.web.pages.FastStringWriter()
        def test1Instance = new Test1(prop2: "This is freemarker test")
        long startTime = System.currentTimeMillis()
        def t=new Template("freemarkerTemplate",
                    new StringReader(freemarkerTemplateCode),
                    freemarkerConfig.configuration)
        for (int i = 0; i < TIMES; i++) {
            t.process([test1Instance: test1Instance, flash:flash], out)
            out.flush()
            println "Executing... $i"
        }
        response.setContentType("text/json;charset=UTF-8")
        long timeTaken = System.currentTimeMillis() - startTime
        render '{"message":"' + TIMES + ' times completed in ' +
timeTaken + 'ms."}'
    }
   
    def gsp = {
        def out = new
org.codehaus.groovy.grails.web.pages.FastStringWriter()
        def test1Instance = new Test1(prop2: "This is gsp test")
        long startTime = System.currentTimeMillis()
        def t=groovyPagesTemplateEngine.createTemplate(gspTemplateCode,
'gspTemplate')
        for (int i = 0; i < TIMES; i++) {
            t.make([test1Instance: test1Instance]).writeTo(out)
            out.flush()
            println "Executing... $i"
        }
        response.setContentType("text/json;charset=UTF-8")
        long timeTaken = System.currentTimeMillis() - startTime
        render '{"message":"' + TIMES + ' times completed in ' +
timeTaken + 'ms."}'
    }


        You can also try to write a bigger template without tags. These tests
don't prove that ftl is faster than gsp. They just show how object
creation could affect the results.


        Best regards,

                Daniel.


On Wed, 2011-01-12 at 19:10 +0200, Lari Hotari wrote:
>




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Lim Chee Kin
Hi Daniel,

Thanks for further advice. Yeah, I do agreed that micro-benchmarking is just meaningful to specific context.

Lari's test is toward template rendering and my requirement is more on template creation (decided to use FreeMarker as template engine of the Form Builder), I will postpone the test of template rendering to optimization phase.

Regards,
Chee Kin



On Sat, Jan 15, 2011 at 7:53 AM, Daniel Henrique Alves Lima [via Grails] <[hidden email]> wrote:
        I don't believe micro-benchmarking are meaningful (because they are too
simple), but i think the poor performance in Lari's version could be my
fault.

        Chee Kin, how do you check if ftl and gsp are producing the same html?
        Try this (after your first tests, change auto import to true and try
again):


== grails-app/conf/Config.groovy ==

grails.plugins.freemarkertags = [
  autoImport: false,      
  defineFunctions: true    
]


==
grails-app/controllers/org/grails/benchmark/BenchmarkController.groovy
==

        static int TIMES = 10000

        static String freemarkerTemplateCode = '''\
[#ftl/]
<html>
abc=${test1Instance.prop2}
</html>
 '''  
        static String gspTemplateCode = '''\
<html>
abc=${test1Instance.prop2}
</html>


// Lari's version
    def freemarker = {
        def out = new
org.codehaus.groovy.grails.web.pages.FastStringWriter()
        def test1Instance = new Test1(prop2: "This is freemarker test")
        long startTime = System.currentTimeMillis()
        def t=new Template("freemarkerTemplate",
                    new StringReader(freemarkerTemplateCode),
                    freemarkerConfig.configuration)
        for (int i = 0; i < TIMES; i++) {
            t.process([test1Instance: test1Instance, flash:flash], out)
            out.flush()
            println "Executing... $i"
        }
        response.setContentType("text/json;charset=UTF-8")
        long timeTaken = System.currentTimeMillis() - startTime
        render '{"message":"' + TIMES + ' times completed in ' +
timeTaken + 'ms."}'
    }
   
    def gsp = {
        def out = new
org.codehaus.groovy.grails.web.pages.FastStringWriter()
        def test1Instance = new Test1(prop2: "This is gsp test")
        long startTime = System.currentTimeMillis()
        def t=groovyPagesTemplateEngine.createTemplate(gspTemplateCode,
'gspTemplate')
        for (int i = 0; i < TIMES; i++) {
            t.make([test1Instance: test1Instance]).writeTo(out)
            out.flush()
            println "Executing... $i"
        }
        response.setContentType("text/json;charset=UTF-8")
        long timeTaken = System.currentTimeMillis() - startTime
        render '{"message":"' + TIMES + ' times completed in ' +
timeTaken + 'ms."}'
    }


        You can also try to write a bigger template without tags. These tests
don't prove that ftl is faster than gsp. They just show how object
creation could affect the results.


        Best regards,

                Daniel.


On Wed, 2011-01-12 at 19:10 +0200, Lari Hotari wrote:
>




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





View message @ http://grails.1312388.n4.nabble.com/FreeMarker-vs-GSP-for-Dynamic-Template-Rendering-tp3213939p3218555.html

To unsubscribe from FreeMarker vs. GSP for Dynamic Template Rendering, click here.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

Daniel Henrique Alves Lima
        Ok, Chee Kin.

        But i'm not sure if FreeMarker is the culprit in Lari's test.
FreeMarker Tags plugin could be responsible for that (or the way it
works). I've noticed that if you write a template and repeat the same
tag N times, in both .ftl and .gsp, .ftl performance will be the worst.
If you add the same tag again and again, .ftl rendering will get worse
and worse.


        Best regards,

                Daniel.


On Fri, 2011-01-14 at 18:39 -0800, Lim Chee Kin wrote:
(...)
>
> Lari's test is toward template rendering and my requirement is more on
> template creation (decided to use FreeMarker as template engine of the
> Form Builder), I will postpone the test of template rendering to
> optimization phase.
(...)


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

basejump (Josh)
In reply to this post by Marc Palmer Local
The main reason we use Freemarker in some areas (reporting, email templates, etc..) is so we can give our customers the option of editing them.
I would love it if we could stick with gsp but we saw that as opening up a huge security hole. It does suck not to stay with gsp but unless there is a way to "lock down" a gsp file so its difficult to be malicious then we need to stick with ftl's
Anyone doing anything like that?

On Jan 14, 2011, at 3:40 AM, Marc Palmer wrote:

>
> On 13 Jan 2011, at 02:25, Lim Chee Kin wrote:
>
>> Hi Lari,
>>
>> Thanks for writing, running the test and providing code snippet. The benchmark test could be break down to template creation and template rendering. Your test result prove that GSP is faster in template rendering, but FreeMarker is faster in template creation as per comments of potter_ru in my blog post. I re-post his comment here:
>> --------------------------------------------------------------------------------------------
>> 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.
>> ---------------------------------------------------------------------------------------------
>>
>
> This is almost certainly because GSP generates source code and then runs the groovy compiler on it, then loads the class with classloader to use it.
>
> I'm guessing that Freemarker does not compile to java bytecodes and just maintains a form of AST in memory, at least Velocity (and its predecessor WebMacro) did this. It means template parse is very fast, but the trade off is execution is in effect "interpreted" and hence slower. Which seems to be the results you are seeing.
>
> I used to contribute a little to WebMacro way back, before Apache forked it to Velocity (boo hiss).
>
> As a result I used to always think "Wouldn't it be nice if Grails used something like Velocity / Freemarker instead of GSP".
>
> It is folly. GSP is excellent. There are things that would be very difficult or impossible to do in non-GSP view tech due to the power of Groovy, without a bunch of specific additions to that view tech to support Groovy expressions etc.
>
> Give it up - GSP is great, and thanks to Lari's hard work is getting faster all the time.
>
> Also note that 100 executions is not significant at all for performance testing. You need 10000s of requests to get the JVM to optimize code correctly over time. You will see the time keeps going done the more you run, until it hits a reasonably stable level.
>
> Marc
> ~ ~ ~
> Marc Palmer
> Freelancer (Grails/Groovy/Java)
>
> Blog         > http://www.anyware.co.uk
> Twitter      > http://twitter.com/wangjammer5
> Grails Rocks > http://www.grailsrocks.com
>
>
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: FreeMarker vs. GSP for Dynamic Template Rendering

marcopas
The last couple of days i was thinking in the same direction, how can i give my users  way of using a template language and i prefer to stick to gsp, So if someone has any thoughts how gsp can be provided to users in a save way that would be great!
For now Freemarker is one of the solutions i consider.

- Marco
On Jan 15, 2011, at 7:02 PM, basejump wrote:

> The main reason we use Freemarker in some areas (reporting, email templates, etc..) is so we can give our customers the option of editing them.
> I would love it if we could stick with gsp but we saw that as opening up a huge security hole. It does suck not to stay with gsp but unless there is a way to "lock down" a gsp file so its difficult to be malicious then we need to stick with ftl's
> Anyone doing anything like that?
>
> On Jan 14, 2011, at 3:40 AM, Marc Palmer wrote:
>
>>
>> On 13 Jan 2011, at 02:25, Lim Chee Kin wrote:
>>
>>> Hi Lari,
>>>
>>> Thanks for writing, running the test and providing code snippet. The benchmark test could be break down to template creation and template rendering. Your test result prove that GSP is faster in template rendering, but FreeMarker is faster in template creation as per comments of potter_ru in my blog post. I re-post his comment here:
>>> --------------------------------------------------------------------------------------------
>>> 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.
>>> ---------------------------------------------------------------------------------------------
>>>
>>
>> This is almost certainly because GSP generates source code and then runs the groovy compiler on it, then loads the class with classloader to use it.
>>
>> I'm guessing that Freemarker does not compile to java bytecodes and just maintains a form of AST in memory, at least Velocity (and its predecessor WebMacro) did this. It means template parse is very fast, but the trade off is execution is in effect "interpreted" and hence slower. Which seems to be the results you are seeing.
>>
>> I used to contribute a little to WebMacro way back, before Apache forked it to Velocity (boo hiss).
>>
>> As a result I used to always think "Wouldn't it be nice if Grails used something like Velocity / Freemarker instead of GSP".
>>
>> It is folly. GSP is excellent. There are things that would be very difficult or impossible to do in non-GSP view tech due to the power of Groovy, without a bunch of specific additions to that view tech to support Groovy expressions etc.
>>
>> Give it up - GSP is great, and thanks to Lari's hard work is getting faster all the time.
>>
>> Also note that 100 executions is not significant at all for performance testing. You need 10000s of requests to get the JVM to optimize code correctly over time. You will see the time keeps going done the more you run, until it hits a reasonably stable level.
>>
>> Marc
>> ~ ~ ~
>> Marc Palmer
>> Freelancer (Grails/Groovy/Java)
>>
>> Blog         > http://www.anyware.co.uk
>> Twitter      > http://twitter.com/wangjammer5
>> Grails Rocks > http://www.grailsrocks.com
>>
>>
>>
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>   http://xircles.codehaus.org/manage_email
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


12
Loading...