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
120
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
230
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
Jakarta EE meets AI
ivargrimstad
0
150
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
330
Macとオーディオ再生 2024/11/02
yusukeito
0
370
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
CSC509 Lecture 12
javiergs
PRO
0
160
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.7k
最新TCAキャッチアップ
0si43
0
140
Quine, Polyglot, 良いコード
qnighy
4
640
受け取る人から提供する人になるということ
little_rubyist
0
230
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
230
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
693
190k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Raft: Consensus for Rubyists
vanstee
136
6.6k
How GitHub (no longer) Works
holman
310
140k
RailsConf 2023
tenderlove
29
900
Scaling GitHub
holman
458
140k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Thoughts on Productivity
jonyablonski
67
4.3k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Docker and Python
trallard
40
3.1k
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