SlideShare a Scribd company logo
This slide was intentionally left blank
Speed is a feature
A mystical journey through Django performance
optimization techniques, tools and gotchas
@martinblech @polmuz
Roadmap
How to find performance problems
Sneak peak: Front-end performance
How to fix them in Django
Why?
[Google] found that Half a
second delay caused a 20% drop
in traffic.
[Amazon] found that even very
small delays would result in
substantial and costly drops in
revenue.
Users really respond to speed
2006
There’s nothing like data
Don’t start with the code. Profile and gather real usage data.
There’s nothing like real data
Identify bottlenecks
New Relic
Very good
Very paid
Google Analytics
Free of charge
Less detail
Your logs
No data to third parties
Harder to use
Profiling
Complex setup required
Overhead
New Relic
New Relic
Google Analytics Site Speed
There’s nothing like data
Let’s find the culprit!
django-debug-toolbar
django-debug-toolbar-template-timings
Time all the things!
Typical backend bottlenecks
Database
External Services
CPU Intensive task
Template Rendering
Database
Missing index
Big, unused fields
Excessive # of queries
Order
Missing Index
class Comment(Model):
...
created_at = DateTimeField(db_index=True)
blogpost = ForeignKey(Blogpost)
class Meta:
index_together = [
["created_at", "blogpost"],
]
select_related()
>>> for c in Comment.objects.all():
print c.user.name
# select * from comments;
# select * from users where id = 1;
# select * from users where id = 2;
...
>>> comments = Comment.objects.all();
>>> for c in comments.select_related(“user”):
print c.user.name
# select comments.*, users.*
# from comments, users
# where comments.user_id = users.id;
prefech_related()
>>> for u in User.objects.filter(id__lt=10):
print len(u.comments.all())
# select * from users;
# select * from comments where user_id = 1;
# select * from comments where user_id = 2;
...
>>> users = User.objects.filter(id__lt=10)
>>> for u in users.prefetch_related(“comments”):
print len(u.comments.all())
# select * from users where id < 10;
# select * from comments
# where user_id in (1,2,3,4,5,6,7,8,9);
## Joins them in python
Demo
Background Jobs
Celery
from celery import task
@task
def send_confirmation_email(user_id):
...
def signup(req):
...
send_confirmation_email.delay(req.user.id)
return HttpResponseRedirect(“/home/”)
Template Compilation
Use
django.template.loaders.cached.Loader
TEMPLATE_LOADERS = (
('django.template.loaders.cached.Loader', (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)),
)
Template Fragment Caching
{% load cache %}
{% cache 500 “last_comments” %}
.. last comments ..
{% endcache %}
{% load cache %}
{% cache 500 “my_recent_comments” user.id %}
.. user’s recent comments ..
{% endcache %}
Caching!
Per view cache
from django.views.decorators.cache import
cache_page
@cache_page(60 * 15) # seconds
def my_view(request):
...
Caching!
Per view cache
Gotchas:
User-specific content
CSRF token
Caching!
Low level cache API
>>> from django.core.cache import get_cache
>>> cache = get_cache('default')
>>> cache.set('my_key', 'hello, world!', 30)
>>> cache.get('my_key')
>>> from django.core.cache import caches
>>> cache = caches['default']
Caching!
from django.core.cache import get_cache
cache = get_cache('default')
def last_comments(request):
comments_ids = cache.get('last_comments')
if comments_ids:
comments = Comment.objects.filter(id__in=comments_ids)
else:
comments = fetch_last_comments()
comments_ids = [c.id for c in comments]
cache.set('last_comments', comments_ids)
...
Low level cache API
Cache Invalidation
“There are two hard things in computer
science: cache invalidation, naming
things, and off-by-one errors.”
post_save/post_delete are a good place
to start, but it doesn’t end there!
@receiver(post_save, sender=Comment)
@receiver(post_delete, sender=Comment)
def invalidate_last_comments(sender, **kwargs):
cache.delete('last_comments')
Caching!
Django Cacheback
from cacheback.decorators import cacheback
@cacheback
def fetch_last_comments_ids():
...
def last_comments(request):
comments_ids = fetch_last_comments_ids()
comments = Comment.objects.filter(id__in=comments_ids)
...
Don’t forget about the browser
80% or more of the end-user response
time is spent in the front end
Combine
Compress
Cache
Less is more
Load slow things later
Focus on making the
important things faster
Don’t forget about the browser
Google PageSpeed Insights
webpagetest.org
Chrome & Firefox
Assets
Django Compressor
{% compress js %}
<script src="/static/js/one.js"></script>
<script>obj.value = "value";</script>
{% endcompress %}
Django Assets
{% assets "js_all" %}
<script async src="{{ ASSET_URL }}"></script>
{% endassets %}
Assets
aload.js
<script data-aload="http://foo.com/foo.js"></script>
<link data-aload="http://foo.com/foo.css" rel="stylesheet">
Assets
Shameless plug: django-critical
{% critical %}
<link rel="stylesheet" href="bootstrap.min.css">
{% endcritical %}
Alpha stage, use at your own risk!
Q & Maybe A
References
Performance is a feature - http://blog.codinghorror.com/performance-is-a-feature/
Marissa Mayer at Web 2.0 - http://glinden.blogspot.com.ar/2006/11/marissa-mayer-at-web-20.html
Psychology of Web Performance - http://www.websiteoptimization.com/speed/tweak/psychology-web-performance/
New Relic - http://newrelic.com
Google Analytics - http://www.google.com/analytics/
Tracking Application Response Time with Nginx - http://lincolnloop.com/blog/tracking-application-response-time-nginx/
Logging Apache response times - http://www.moeding.net/archives/33-Logging-Apache-response-times.html
Django Debug Toolbar - http://django-debug-toolbar.readthedocs.org/
DDT Template Timings - https://github.com/orf/django-debug-toolbar-template-timings
Django Database Optimizations - https://docs.djangoproject.com/en/1.7/topics/db/optimization/
Two Hard Things - http://martinfowler.com/bliki/TwoHardThings.html
Django Cache Docs - https://docs.djangoproject.com/en/1.7/topics/cache/
Django Cacheback - http://django-cacheback.readthedocs.org/en/latest/index.html
Cached Templates - https://docs.djangoproject.com/en/1.7/ref/templates/api/#django.template.loaders.cached.Loader
Google PageSpeed Insights - https://developers.google.com/speed/pagespeed/insights/
Web Page Test - http://www.webpagetest.org/
Django Compressor - http://django-compressor.readthedocs.org/en/1.3/
Django Assets - http://django-assets.readthedocs.org/en/0.8/
aload.js - https://github.com/pazguille/aload
django-critical - https://github.com/martinblech/django-critical
Demo - https://github.com/martinblech/pyconar2014_perfdemo

More Related Content

What's hot

Java script programms
Java script programmsJava script programms
Java script programms
Mukund Gandrakota
 
The Future of CSS with Web Components
The Future of CSS with Web ComponentsThe Future of CSS with Web Components
The Future of CSS with Web Components
ColdFusionConference
 
The Big Picture and How to Get Started
The Big Picture and How to Get StartedThe Big Picture and How to Get Started
The Big Picture and How to Get Started
guest1af57e
 
Cache Money Talk: Practical Application
Cache Money Talk: Practical ApplicationCache Money Talk: Practical Application
Cache Money Talk: Practical Application
Wolfram Arnold
 
Opensocial Codelab
Opensocial CodelabOpensocial Codelab
Opensocial Codelab
Pieter De Schepper
 
Micro app-framework
Micro app-frameworkMicro app-framework
Micro app-framework
Michael Dawson
 
Django Architecture Introduction
Django Architecture IntroductionDjango Architecture Introduction
Django Architecture Introduction
Haiqi Chen
 
Djangoアプリのデプロイに関するプラクティス / Deploy django application
Djangoアプリのデプロイに関するプラクティス / Deploy django applicationDjangoアプリのデプロイに関するプラクティス / Deploy django application
Djangoアプリのデプロイに関するプラクティス / Deploy django application
Masashi Shibata
 
Symfony 1, mi viejo amigo
Symfony 1, mi viejo amigoSymfony 1, mi viejo amigo
Symfony 1, mi viejo amigo
Jose Antonio Pio
 
Security: Odoo Code Hardening
Security: Odoo Code HardeningSecurity: Odoo Code Hardening
Security: Odoo Code Hardening
Odoo
 
YSlow 2.0
YSlow 2.0YSlow 2.0
YSlow 2.0
Stoyan Stefanov
 
JavaScript Testing for Rubyists
JavaScript Testing for RubyistsJavaScript Testing for Rubyists
JavaScript Testing for Rubyists
Jamie Dyer
 
Yearning jQuery
Yearning jQueryYearning jQuery
Yearning jQuery
Remy Sharp
 
jQuery UI and Plugins
jQuery UI and PluginsjQuery UI and Plugins
jQuery UI and Plugins
Marc Grabanski
 
2012 SVCodeCamp: In App Payments with HTML5
2012 SVCodeCamp: In App Payments with HTML52012 SVCodeCamp: In App Payments with HTML5
2012 SVCodeCamp: In App Payments with HTML5
Jonathan LeBlanc
 
Jquery examples
Jquery examplesJquery examples
Jquery examples
programmingslides
 
Make More Money With Advanced Custom Fields - WordCampYYC 2015
Make More Money With Advanced Custom Fields - WordCampYYC 2015Make More Money With Advanced Custom Fields - WordCampYYC 2015
Make More Money With Advanced Custom Fields - WordCampYYC 2015
buildstudio
 
Progressive Downloads and Rendering
Progressive Downloads and RenderingProgressive Downloads and Rendering
Progressive Downloads and Rendering
Stoyan Stefanov
 
The Devil and HTML5
The Devil and HTML5The Devil and HTML5
The Devil and HTML5
Myles Braithwaite
 

What's hot (19)

Java script programms
Java script programmsJava script programms
Java script programms
 
The Future of CSS with Web Components
The Future of CSS with Web ComponentsThe Future of CSS with Web Components
The Future of CSS with Web Components
 
The Big Picture and How to Get Started
The Big Picture and How to Get StartedThe Big Picture and How to Get Started
The Big Picture and How to Get Started
 
Cache Money Talk: Practical Application
Cache Money Talk: Practical ApplicationCache Money Talk: Practical Application
Cache Money Talk: Practical Application
 
Opensocial Codelab
Opensocial CodelabOpensocial Codelab
Opensocial Codelab
 
Micro app-framework
Micro app-frameworkMicro app-framework
Micro app-framework
 
Django Architecture Introduction
Django Architecture IntroductionDjango Architecture Introduction
Django Architecture Introduction
 
Djangoアプリのデプロイに関するプラクティス / Deploy django application
Djangoアプリのデプロイに関するプラクティス / Deploy django applicationDjangoアプリのデプロイに関するプラクティス / Deploy django application
Djangoアプリのデプロイに関するプラクティス / Deploy django application
 
Symfony 1, mi viejo amigo
Symfony 1, mi viejo amigoSymfony 1, mi viejo amigo
Symfony 1, mi viejo amigo
 
Security: Odoo Code Hardening
Security: Odoo Code HardeningSecurity: Odoo Code Hardening
Security: Odoo Code Hardening
 
YSlow 2.0
YSlow 2.0YSlow 2.0
YSlow 2.0
 
JavaScript Testing for Rubyists
JavaScript Testing for RubyistsJavaScript Testing for Rubyists
JavaScript Testing for Rubyists
 
Yearning jQuery
Yearning jQueryYearning jQuery
Yearning jQuery
 
jQuery UI and Plugins
jQuery UI and PluginsjQuery UI and Plugins
jQuery UI and Plugins
 
2012 SVCodeCamp: In App Payments with HTML5
2012 SVCodeCamp: In App Payments with HTML52012 SVCodeCamp: In App Payments with HTML5
2012 SVCodeCamp: In App Payments with HTML5
 
Jquery examples
Jquery examplesJquery examples
Jquery examples
 
Make More Money With Advanced Custom Fields - WordCampYYC 2015
Make More Money With Advanced Custom Fields - WordCampYYC 2015Make More Money With Advanced Custom Fields - WordCampYYC 2015
Make More Money With Advanced Custom Fields - WordCampYYC 2015
 
Progressive Downloads and Rendering
Progressive Downloads and RenderingProgressive Downloads and Rendering
Progressive Downloads and Rendering
 
The Devil and HTML5
The Devil and HTML5The Devil and HTML5
The Devil and HTML5
 

Viewers also liked

ಮಾಯಾವಿ ಮೊಬೈಲ್ - ಸುಧಾ ಲೇಖನ - ಸೆಪ್ಟೆಂಬರ್ ೧೯, ೨೦೧೩
ಮಾಯಾವಿ ಮೊಬೈಲ್ - ಸುಧಾ ಲೇಖನ - ಸೆಪ್ಟೆಂಬರ್ ೧೯, ೨೦೧೩ಮಾಯಾವಿ ಮೊಬೈಲ್ - ಸುಧಾ ಲೇಖನ - ಸೆಪ್ಟೆಂಬರ್ ೧೯, ೨೦೧೩
ಮಾಯಾವಿ ಮೊಬೈಲ್ - ಸುಧಾ ಲೇಖನ - ಸೆಪ್ಟೆಂಬರ್ ೧೯, ೨೦೧೩
Omshivaprakash H L
 
Linux for Web Developers
Linux for Web DevelopersLinux for Web Developers
Linux for Web Developers
Omshivaprakash H L
 
Top Interview Questions
Top Interview QuestionsTop Interview Questions
Top Interview Questions
Sudha Koya
 
Water Disaster with Water Bottles
Water Disaster with Water BottlesWater Disaster with Water Bottles
Water Disaster with Water Bottles
Omshivaprakash H L
 
Technology for Media Students
Technology for Media StudentsTechnology for Media Students
Technology for Media Students
Omshivaprakash H L
 
Job Posting
Job PostingJob Posting
Job Posting
Sudha Koya
 
Conserving Linguistic Heritage the FOSS way...
Conserving Linguistic Heritage the FOSS way...Conserving Linguistic Heritage the FOSS way...
Conserving Linguistic Heritage the FOSS way...
Omshivaprakash H L
 
Trixbox Pro
Trixbox ProTrixbox Pro
Trixbox Pro
fahadshaikh
 
V-Soft Consulting Group Inc.
V-Soft Consulting Group Inc.V-Soft Consulting Group Inc.
V-Soft Consulting Group Inc.
Sudha Koya
 
Why Would Anyone Want To Work In PR?
Why Would Anyone Want To Work In PR?Why Would Anyone Want To Work In PR?
Why Would Anyone Want To Work In PR?
James Lee
 
IPT Symposium Social Media Presentation
IPT Symposium Social Media PresentationIPT Symposium Social Media Presentation
IPT Symposium Social Media Presentation
James Lee
 
Swine Flu H1N1
Swine Flu H1N1Swine Flu H1N1
Swine Flu H1N1
Omshivaprakash H L
 
Sub Prime Permier
Sub Prime PermierSub Prime Permier
Sub Prime Permier
Omshivaprakash H L
 
Storytelling museumstudies-vs2
Storytelling museumstudies-vs2Storytelling museumstudies-vs2
Storytelling museumstudies-vs2
Erfgoed 2.0
 
International Year of Astronomy 2009
International Year of Astronomy 2009International Year of Astronomy 2009
International Year of Astronomy 2009
Omshivaprakash H L
 
21 Tips Of Telephone Etiquettes
21 Tips Of Telephone Etiquettes 21 Tips Of Telephone Etiquettes
21 Tips Of Telephone Etiquettes
Sudha Koya
 
Bangalore Traffic Solution
Bangalore Traffic SolutionBangalore Traffic Solution
Bangalore Traffic Solution
Omshivaprakash H L
 
Telephone Etiquette
Telephone EtiquetteTelephone Etiquette
Telephone Etiquette
Sudha Koya
 
Recruitment Strategy
Recruitment StrategyRecruitment Strategy
Recruitment Strategy
Sudha Koya
 

Viewers also liked (19)

ಮಾಯಾವಿ ಮೊಬೈಲ್ - ಸುಧಾ ಲೇಖನ - ಸೆಪ್ಟೆಂಬರ್ ೧೯, ೨೦೧೩
ಮಾಯಾವಿ ಮೊಬೈಲ್ - ಸುಧಾ ಲೇಖನ - ಸೆಪ್ಟೆಂಬರ್ ೧೯, ೨೦೧೩ಮಾಯಾವಿ ಮೊಬೈಲ್ - ಸುಧಾ ಲೇಖನ - ಸೆಪ್ಟೆಂಬರ್ ೧೯, ೨೦೧೩
ಮಾಯಾವಿ ಮೊಬೈಲ್ - ಸುಧಾ ಲೇಖನ - ಸೆಪ್ಟೆಂಬರ್ ೧೯, ೨೦೧೩
 
Linux for Web Developers
Linux for Web DevelopersLinux for Web Developers
Linux for Web Developers
 
Top Interview Questions
Top Interview QuestionsTop Interview Questions
Top Interview Questions
 
Water Disaster with Water Bottles
Water Disaster with Water BottlesWater Disaster with Water Bottles
Water Disaster with Water Bottles
 
Technology for Media Students
Technology for Media StudentsTechnology for Media Students
Technology for Media Students
 
Job Posting
Job PostingJob Posting
Job Posting
 
Conserving Linguistic Heritage the FOSS way...
Conserving Linguistic Heritage the FOSS way...Conserving Linguistic Heritage the FOSS way...
Conserving Linguistic Heritage the FOSS way...
 
Trixbox Pro
Trixbox ProTrixbox Pro
Trixbox Pro
 
V-Soft Consulting Group Inc.
V-Soft Consulting Group Inc.V-Soft Consulting Group Inc.
V-Soft Consulting Group Inc.
 
Why Would Anyone Want To Work In PR?
Why Would Anyone Want To Work In PR?Why Would Anyone Want To Work In PR?
Why Would Anyone Want To Work In PR?
 
IPT Symposium Social Media Presentation
IPT Symposium Social Media PresentationIPT Symposium Social Media Presentation
IPT Symposium Social Media Presentation
 
Swine Flu H1N1
Swine Flu H1N1Swine Flu H1N1
Swine Flu H1N1
 
Sub Prime Permier
Sub Prime PermierSub Prime Permier
Sub Prime Permier
 
Storytelling museumstudies-vs2
Storytelling museumstudies-vs2Storytelling museumstudies-vs2
Storytelling museumstudies-vs2
 
International Year of Astronomy 2009
International Year of Astronomy 2009International Year of Astronomy 2009
International Year of Astronomy 2009
 
21 Tips Of Telephone Etiquettes
21 Tips Of Telephone Etiquettes 21 Tips Of Telephone Etiquettes
21 Tips Of Telephone Etiquettes
 
Bangalore Traffic Solution
Bangalore Traffic SolutionBangalore Traffic Solution
Bangalore Traffic Solution
 
Telephone Etiquette
Telephone EtiquetteTelephone Etiquette
Telephone Etiquette
 
Recruitment Strategy
Recruitment StrategyRecruitment Strategy
Recruitment Strategy
 

Similar to Speed is a Feature - PyConAr 2014

Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
Richard Leland
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
Jagdeep Singh Malhi
 
國民雲端架構 Django + GAE
國民雲端架構 Django + GAE國民雲端架構 Django + GAE
國民雲端架構 Django + GAE
Winston Chen
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
Richard Leland
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
Richard Leland
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
Richard Leland
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
Richard Leland
 
End-to-end testing with geb
End-to-end testing with gebEnd-to-end testing with geb
End-to-end testing with geb
Jesús L. Domínguez Muriel
 
Django Show
Django ShowDjango Show
Django Show
Andrews Medina
 
Gae Meets Django
Gae Meets DjangoGae Meets Django
Gae Meets Django
fool2nd
 
Caching Up and Down the Stack
Caching Up and Down the StackCaching Up and Down the Stack
Caching Up and Down the Stack
Dan Kuebrich
 
Mini Curso de Django
Mini Curso de DjangoMini Curso de Django
Mini Curso de Django
Felipe Queiroz
 
Metrics-Driven Engineering
Metrics-Driven EngineeringMetrics-Driven Engineering
Metrics-Driven Engineering
Mike Brittain
 
Implementation of GUI Framework part3
Implementation of GUI Framework part3Implementation of GUI Framework part3
Implementation of GUI Framework part3
masahiroookubo
 
Introduce Django
Introduce DjangoIntroduce Django
Introduce Django
Chui-Wen Chiu
 
High Performance Web Pages - 20 new best practices
High Performance Web Pages - 20 new best practicesHigh Performance Web Pages - 20 new best practices
High Performance Web Pages - 20 new best practices
Stoyan Stefanov
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
Joaquim Rocha
 
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With DeadlinesJBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
Tikal Knowledge
 
Intro To Django
Intro To DjangoIntro To Django
Intro To Django
Udi Bauman
 
PHPConf-TW 2012 # Twig
PHPConf-TW 2012 # TwigPHPConf-TW 2012 # Twig
PHPConf-TW 2012 # Twig
Wake Liu
 

Similar to Speed is a Feature - PyConAr 2014 (20)

Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
國民雲端架構 Django + GAE
國民雲端架構 Django + GAE國民雲端架構 Django + GAE
國民雲端架構 Django + GAE
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
End-to-end testing with geb
End-to-end testing with gebEnd-to-end testing with geb
End-to-end testing with geb
 
Django Show
Django ShowDjango Show
Django Show
 
Gae Meets Django
Gae Meets DjangoGae Meets Django
Gae Meets Django
 
Caching Up and Down the Stack
Caching Up and Down the StackCaching Up and Down the Stack
Caching Up and Down the Stack
 
Mini Curso de Django
Mini Curso de DjangoMini Curso de Django
Mini Curso de Django
 
Metrics-Driven Engineering
Metrics-Driven EngineeringMetrics-Driven Engineering
Metrics-Driven Engineering
 
Implementation of GUI Framework part3
Implementation of GUI Framework part3Implementation of GUI Framework part3
Implementation of GUI Framework part3
 
Introduce Django
Introduce DjangoIntroduce Django
Introduce Django
 
High Performance Web Pages - 20 new best practices
High Performance Web Pages - 20 new best practicesHigh Performance Web Pages - 20 new best practices
High Performance Web Pages - 20 new best practices
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With DeadlinesJBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
 
Intro To Django
Intro To DjangoIntro To Django
Intro To Django
 
PHPConf-TW 2012 # Twig
PHPConf-TW 2012 # TwigPHPConf-TW 2012 # Twig
PHPConf-TW 2012 # Twig
 

Recently uploaded

Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
Karya Keeper
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
YousufSait3
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
TaghreedAltamimi
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
kalichargn70th171
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 

Recently uploaded (20)

Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 

Speed is a Feature - PyConAr 2014

  • 1. This slide was intentionally left blank
  • 2. Speed is a feature A mystical journey through Django performance optimization techniques, tools and gotchas @martinblech @polmuz
  • 3. Roadmap How to find performance problems Sneak peak: Front-end performance How to fix them in Django
  • 4. Why? [Google] found that Half a second delay caused a 20% drop in traffic. [Amazon] found that even very small delays would result in substantial and costly drops in revenue. Users really respond to speed 2006
  • 5. There’s nothing like data Don’t start with the code. Profile and gather real usage data.
  • 6. There’s nothing like real data Identify bottlenecks New Relic Very good Very paid Google Analytics Free of charge Less detail Your logs No data to third parties Harder to use Profiling Complex setup required Overhead
  • 10. There’s nothing like data Let’s find the culprit! django-debug-toolbar django-debug-toolbar-template-timings
  • 11. Time all the things!
  • 12. Typical backend bottlenecks Database External Services CPU Intensive task Template Rendering
  • 13. Database Missing index Big, unused fields Excessive # of queries Order
  • 14. Missing Index class Comment(Model): ... created_at = DateTimeField(db_index=True) blogpost = ForeignKey(Blogpost) class Meta: index_together = [ ["created_at", "blogpost"], ]
  • 15. select_related() >>> for c in Comment.objects.all(): print c.user.name # select * from comments; # select * from users where id = 1; # select * from users where id = 2; ... >>> comments = Comment.objects.all(); >>> for c in comments.select_related(“user”): print c.user.name # select comments.*, users.* # from comments, users # where comments.user_id = users.id;
  • 16. prefech_related() >>> for u in User.objects.filter(id__lt=10): print len(u.comments.all()) # select * from users; # select * from comments where user_id = 1; # select * from comments where user_id = 2; ... >>> users = User.objects.filter(id__lt=10) >>> for u in users.prefetch_related(“comments”): print len(u.comments.all()) # select * from users where id < 10; # select * from comments # where user_id in (1,2,3,4,5,6,7,8,9); ## Joins them in python
  • 17. Demo
  • 18. Background Jobs Celery from celery import task @task def send_confirmation_email(user_id): ... def signup(req): ... send_confirmation_email.delay(req.user.id) return HttpResponseRedirect(“/home/”)
  • 19. Template Compilation Use django.template.loaders.cached.Loader TEMPLATE_LOADERS = ( ('django.template.loaders.cached.Loader', ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', )), )
  • 20. Template Fragment Caching {% load cache %} {% cache 500 “last_comments” %} .. last comments .. {% endcache %} {% load cache %} {% cache 500 “my_recent_comments” user.id %} .. user’s recent comments .. {% endcache %}
  • 21. Caching! Per view cache from django.views.decorators.cache import cache_page @cache_page(60 * 15) # seconds def my_view(request): ...
  • 23. Caching! Low level cache API >>> from django.core.cache import get_cache >>> cache = get_cache('default') >>> cache.set('my_key', 'hello, world!', 30) >>> cache.get('my_key') >>> from django.core.cache import caches >>> cache = caches['default']
  • 24. Caching! from django.core.cache import get_cache cache = get_cache('default') def last_comments(request): comments_ids = cache.get('last_comments') if comments_ids: comments = Comment.objects.filter(id__in=comments_ids) else: comments = fetch_last_comments() comments_ids = [c.id for c in comments] cache.set('last_comments', comments_ids) ... Low level cache API
  • 25. Cache Invalidation “There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors.” post_save/post_delete are a good place to start, but it doesn’t end there! @receiver(post_save, sender=Comment) @receiver(post_delete, sender=Comment) def invalidate_last_comments(sender, **kwargs): cache.delete('last_comments')
  • 26. Caching! Django Cacheback from cacheback.decorators import cacheback @cacheback def fetch_last_comments_ids(): ... def last_comments(request): comments_ids = fetch_last_comments_ids() comments = Comment.objects.filter(id__in=comments_ids) ...
  • 27. Don’t forget about the browser 80% or more of the end-user response time is spent in the front end Combine Compress Cache Less is more Load slow things later Focus on making the important things faster
  • 28. Don’t forget about the browser Google PageSpeed Insights webpagetest.org Chrome & Firefox
  • 29. Assets Django Compressor {% compress js %} <script src="/static/js/one.js"></script> <script>obj.value = "value";</script> {% endcompress %} Django Assets {% assets "js_all" %} <script async src="{{ ASSET_URL }}"></script> {% endassets %}
  • 31. Assets Shameless plug: django-critical {% critical %} <link rel="stylesheet" href="bootstrap.min.css"> {% endcritical %} Alpha stage, use at your own risk!
  • 33. References Performance is a feature - http://blog.codinghorror.com/performance-is-a-feature/ Marissa Mayer at Web 2.0 - http://glinden.blogspot.com.ar/2006/11/marissa-mayer-at-web-20.html Psychology of Web Performance - http://www.websiteoptimization.com/speed/tweak/psychology-web-performance/ New Relic - http://newrelic.com Google Analytics - http://www.google.com/analytics/ Tracking Application Response Time with Nginx - http://lincolnloop.com/blog/tracking-application-response-time-nginx/ Logging Apache response times - http://www.moeding.net/archives/33-Logging-Apache-response-times.html Django Debug Toolbar - http://django-debug-toolbar.readthedocs.org/ DDT Template Timings - https://github.com/orf/django-debug-toolbar-template-timings Django Database Optimizations - https://docs.djangoproject.com/en/1.7/topics/db/optimization/ Two Hard Things - http://martinfowler.com/bliki/TwoHardThings.html Django Cache Docs - https://docs.djangoproject.com/en/1.7/topics/cache/ Django Cacheback - http://django-cacheback.readthedocs.org/en/latest/index.html Cached Templates - https://docs.djangoproject.com/en/1.7/ref/templates/api/#django.template.loaders.cached.Loader Google PageSpeed Insights - https://developers.google.com/speed/pagespeed/insights/ Web Page Test - http://www.webpagetest.org/ Django Compressor - http://django-compressor.readthedocs.org/en/1.3/ Django Assets - http://django-assets.readthedocs.org/en/0.8/ aload.js - https://github.com/pazguille/aload django-critical - https://github.com/martinblech/django-critical Demo - https://github.com/martinblech/pyconar2014_perfdemo