GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

Post on 17-May-2015

3.857 views 0 download

Tags:

Transcript of GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

Peter Ledbrook

Tuning Grails apps

1Monday, 30 May 2011

Everyone wants to be Facebook

• Few apps start with millions of hits a day

• Scaling throws up many more issues than just the framework

2Monday, 30 May 2011

Premature optimisation

Profile... then optimise

3Monday, 30 May 2011

Where can things be slow?

DB BusinessLogic Network UI

4Monday, 30 May 2011

Server-side tools

• Spring Insight• Profiler Plugin• Hibernate logging• P6Spy• Hibernate Statistics

– App Info & Hibernate Stats Plugins

5

5Monday, 30 May 2011

UI tools

• Google Speed Tracer (Chrome only)– Spring Insight integration

• ySlow (Firefox only - requires Firebug)

6

6Monday, 30 May 2011

7Monday, 30 May 2011

Database performance

• Reduce the number of queries– Use appropriate fetch mode– Don’t fetch data you don’t need

• Tune your queries– Add appropriate indexes– Don’t be afraid to change the model

8

8Monday, 30 May 2011

Fetch mode and indexing

class User { String username String passwordHash Profile profile

static hasMany = [ roles: Role ]

static mapping = { username index: 'username_idx' profile fetch: 'join' roles lazy: false }}

9Monday, 30 May 2011

Database performance

• Caching– Hibernate 2nd-level cache– Distributed cache/data grid

• Terracotta• GemFire

• Alternative data store!

10

10Monday, 30 May 2011

• DomainClass.get()– Works reliably and well

• Query cache– Each query must be declared as cached– Changes to data clear the cache– Best for data that changes infrequently

Hibernate 2nd-level cache

hibernate { cache.use_second_level_cache = true cache.use_query_cache = true cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'}

11Monday, 30 May 2011

Data grid

DB GrailsApp

GemFireGemFireGemFire

12

12Monday, 30 May 2011

Business logic

• Caching– Spring Cache Plugin for service methods

• Go asynchronous– Ideal for long running tasks– Spring Events, Executor– Messaging: JMS, AMQP

13

13Monday, 30 May 2011

Business logic

• Fall back to Java– Limited applicability– Numerically intensive tasks?

• Groovy++?– One JAR– @Typed on class or method

14

14Monday, 30 May 2011

Business logic

15

class MyService { @Typed(TypePolicy.MIXED) void doSomething() { expensiveMethod() }

@Typed void expensiveMethod() { // Do something }}

15Monday, 30 May 2011

Network and UI

• Reduce view processing– Spring Cache plugin for views

• Reduce number of requests (latency)– Expires HTTP header– Bundle CSS & Javascript files– Image spriting

• Reduce amount of data transferred to browser (bandwidth)– Minify Javascript– Compress data

• Plugins– Performance UI, JAWR– Resources et al.

16Monday, 30 May 2011

Spring Cache with views

Sitemesh

Spring Cache

Render GSP Cached View

17Monday, 30 May 2011

Spring Cache in code

import grails.plugin.springcache.annotations.*

class PostController { @Cacheable("myCache") def list = { ... } @CacheFlush("myCache") def addPost = { ... }}

18Monday, 30 May 2011

Compressing page content

• Container-dependent– Tomcat has connector setting– For Jetty, add GzipFilter to your web.xml

• Enable gzip compression in Tomcat plugin:

eventConfigureTomcat = { tomcat -> tomcat.connector.setAttribute( "compression", "on") tomcat.connector.port = serverPort}

19Monday, 30 May 2011

Resources plugin

• Modularise static content• Bundle files of the same type• Caching Resources plugin

– Sets HTTP Expires header 1 year ahead– Gives static files a unique name

• Zipped Resources plugin– Gzips static content– Can exclude files by extension

20Monday, 30 May 2011

21Monday, 30 May 2011

Rich UIs

• Browser does the work• Minimal traffic• GWT, Flex, etc.• Data transfer

– JSON– XML– GWT-RPC

22Monday, 30 May 2011

Summary

• Profile before you optimise!• Several tools available for profiling• Several plugins available for improving performance• Many techniques apply to any web application• Law of diminishing returns

– At some point, further performance improvements aren’t worth the required effort

• Resources will be in Grails 1.4

23Monday, 30 May 2011

Resources

• Spring Insight– http://www.springsource.com/developer/tcserver

• Grails Plugins– http://grails.org/plugin/profiler– http://grails.org/plugin/springcache– http://grails.org/plugin/resources

24Monday, 30 May 2011

Q&A

Thank you!

25Monday, 30 May 2011