Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Performances Django REST framework - DjangoCong...
Search
xordoquy
June 16, 2016
Programming
0
130
Performances Django REST framework - DjangoCong 2016
Un petit coup d'oeil sur les performances de Django REST framework.
xordoquy
June 16, 2016
Tweet
Share
More Decks by xordoquy
See All by xordoquy
pycon.fr 2018 - Django REST framework workshop
xordoquy
0
240
mauvaises bonnes idées pour REST
xordoquy
1
330
Authentication and Permissions with Django REST framework
xordoquy
0
170
Buildbot 0.9
xordoquy
0
94
Présentation de l'architecture REST - meetup Django Paris
xordoquy
0
110
Django REST framework workshop @Djangocon Europe 2015
xordoquy
0
110
Django REST framework - DjangoConG 2015
xordoquy
3
140
Django REST framework workshop - DjangoCong 2015
xordoquy
1
110
Packaging pratique (fr) - pycon.fr 2014
xordoquy
1
160
Other Decks in Programming
See All in Programming
14 Years of iOS: Lessons and Key Points
seyfoyun
1
770
From Translations to Multi Dimension Entities
alexanderschranz
2
130
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
310
Haze - Real time background blurring
chrisbanes
1
510
快速入門可觀測性
blueswen
0
340
Jakarta EE meets AI
ivargrimstad
0
240
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
540
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
240
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
250
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
110
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
170
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Docker and Python
trallard
42
3.1k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
440
Designing for humans not robots
tammielis
250
25k
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
Code Review Best Practice
trishagee
65
17k
Designing Experiences People Love
moore
138
23k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Building an army of robots
kneath
302
44k
Transcript
Django REST framework performances DjangoCong 2016 Xavier Ordoquy (@linovia_net)
Extensibilité =/= Performance
Extensible: Plus de requêtes par seconde
Performance: Moins de temps par requête
Extensible: Architecture
Performance: Algorithme
La mesure de performance: subjectif
Python 3.4 Python 3.5 16,711 ms 22,322 ms
Django vs. Django REST framework
• Négociation de contenu • Passeurs • Renderers • Throttling
• Pagination • Authentification • Permissions
class Invoice(models.Model): name = models.CharField(max_length=128) comments = models.TextField() total =
models.DecimalField( max_digits=9, decimal_places=2) owner = models.ForeignKey( settings.AUTH_USER_MODEL) assignee = models.ForeignKey( settings.AUTH_USER_MODEL)
@csrf_exempt def create_invoice(request): if request.method == 'GET': serialized_invoices = [
serialize(invoice) for invoice in models.Invoice.objects.all() ] return HttpResponse( content=json.dumps(serialzied_invoices), content_type='application/json', )
@csrf_exempt def create_invoice(request): if request.method == 'POST': form = forms.Invoice(request.POST
or None) if form.is_valid(): invoice = form.save() result = serialize(invoice) return HttpResponse( content=json.dumps(result), content_type='application/json', )
class InvoiceSerializer(serializers.ModelSerializer): class Meta: model = models.Invoice fields = ('id',
'name', 'comments', 'total') class InvoiceViewSet(viewsets.ModelViewSet): queryset = models.Invoice.objects.all() serializer_class = serializers.Invoice def perform_create(self, serializer): serializer.save(owner_id=1, assignee_id=1)
0 0,9 1,8 2,7 3,6 3,473 ms 2,159 ms Django
DRF Liste avec 2 objets +60%
0 3,25 6,5 9,75 13 12,717 ms 9,599 ms Django
DRF Liste avec 100 objets +32%
0 3,25 6,5 9,75 13 10,578 ms 12,868 ms Django
DRF Creation d’un objet -21%
Qui fait quoi et quand ?
ORM
Cycle de la requête
Sérialisation
Vue
Titre 0,20 0,40 0,60 0,80 1,00 1,20 11,36 % 13,7
% 7,9 % 67,0 % ORM Sérialiseur Vue Requête Détail d’un objet
0,00 % 20,00 % 40,00 % 60,00 % 80,00 %
100,00 % 120,00 % 6,271 % 1,621 % 55,349 % 36,759 % ORM Sérialiseur Vue Requête Liste de 100 objets
0,00 % ms 20,00 % ms 40,00 % ms 60,00
% ms 80,00 % ms 00,00 % ms 20,00 % ms 5,808 % 2,538 % 7,874 % 83,78 % ORM Sérialiseur Vue Requête Création d’un objet
Optimiser
Cacher les requêtes DB
Supprimer la sérialisation
def list(self, request, *args, **kwargs): queryset = self.filter_queryset( self.get_queryset()) #
Pagination serializer = self.get_serializer( queryset, many=True ) return Response(serializer.data)
def list(self, request, *args, **kwargs): queryset = self.filter_queryset( self.get_queryset()) #
Pagination data = models.Invoice.objects.values( 'id', 'name', 'comments', 'total' ) return Response(data)
• Nettoyer les middlewares • Faire un rendu direct •
Désactiver la négociation de contenu
Moments WTF ?!?
queryset évalué 2 fois
class RelatedField(Field): def get_queryset(self): queryset = self.queryset if isinstance(queryset, (QuerySet,
Manager)): queryset = queryset.all() return queryset
WTF ? selected_related n’est pas fonctionnel ?
selected_related n’est pas fonctionnel durant l’update ! (Django #21584)
Stop aux ModelSerializers !
Vive les Serializers !
Questions ? https://www.dabapps.com/blog/api-performance-profiling- django-rest-framework/ https://github.com/xordoquy/django-rest-framework- benchmark