gr8conf 2011: tuning grails applications by peter ledbrook

25
Peter Ledbrook Tuning Grails apps 1 Monday, 30 May 2011

Upload: gr8conf

Post on 17-May-2015

3.856 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

Peter Ledbrook

Tuning Grails apps

1Monday, 30 May 2011

Page 2: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

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

Page 3: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

Premature optimisation

Profile... then optimise

3Monday, 30 May 2011

Page 4: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

Where can things be slow?

DB BusinessLogic Network UI

4Monday, 30 May 2011

Page 5: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

Server-side tools

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

– App Info & Hibernate Stats Plugins

5

5Monday, 30 May 2011

Page 6: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

UI tools

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

• ySlow (Firefox only - requires Firebug)

6

6Monday, 30 May 2011

Page 7: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

7Monday, 30 May 2011

Page 8: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

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

Page 9: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

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

Page 10: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

Database performance

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

• Terracotta• GemFire

• Alternative data store!

10

10Monday, 30 May 2011

Page 11: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

• 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

Page 12: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

Data grid

DB GrailsApp

GemFireGemFireGemFire

12

12Monday, 30 May 2011

Page 13: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

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

Page 14: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

Business logic

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

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

14

14Monday, 30 May 2011

Page 15: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

Business logic

15

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

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

15Monday, 30 May 2011

Page 16: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

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

Page 17: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

Spring Cache with views

Sitemesh

Spring Cache

Render GSP Cached View

17Monday, 30 May 2011

Page 18: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

Spring Cache in code

import grails.plugin.springcache.annotations.*

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

18Monday, 30 May 2011

Page 19: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

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

Page 20: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

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

Page 21: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

21Monday, 30 May 2011

Page 22: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

Rich UIs

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

– JSON– XML– GWT-RPC

22Monday, 30 May 2011

Page 23: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

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

Page 24: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

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

Page 25: GR8Conf 2011: Tuning Grails Applications by Peter Ledbrook

Q&A

Thank you!

25Monday, 30 May 2011