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
310
mauvaises bonnes idées pour REST
xordoquy
1
370
Authentication and Permissions with Django REST framework
xordoquy
0
180
Buildbot 0.9
xordoquy
0
100
Présentation de l'architecture REST - meetup Django Paris
xordoquy
0
110
Django REST framework workshop @Djangocon Europe 2015
xordoquy
0
120
Django REST framework - DjangoConG 2015
xordoquy
3
140
Django REST framework workshop - DjangoCong 2015
xordoquy
1
120
Packaging pratique (fr) - pycon.fr 2014
xordoquy
1
170
Other Decks in Programming
See All in Programming
Claude CodeによるAI駆動開発の実践 〜そこから見えてきたこれからのプログラミング〜
iriikeita
0
300
Leading Effective Engineering Teams in the AI Era
addyosmani
7
470
What's new in Spring Modulith?
olivergierke
1
160
「ちょっと古いから」って避けてた技術書、今だからこそ読もう
mottyzzz
11
6.9k
AI駆動で0→1をやって見えた光と伸びしろ
passion0102
1
460
詳しくない分野でのVibe Codingで困ったことと学び/vibe-coding-in-unfamiliar-area
shibayu36
3
5.1k
Range on Rails ―「多重範囲型」という新たな選択肢が、複雑ロジックを劇的にシンプルにしたワケ
rizap_tech
0
6.7k
CSC305 Lecture 08
javiergs
PRO
0
230
iOSエンジニア向けの英語学習アプリを作る!
yukawashouhei
0
200
登壇は dynamic! な営みである / speech is dynamic
da1chi
0
350
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
440
Introduce Hono CLI
yusukebe
6
2.8k
Featured
See All Featured
How to Ace a Technical Interview
jacobian
280
24k
Fireside Chat
paigeccino
40
3.7k
It's Worth the Effort
3n
187
28k
How STYLIGHT went responsive
nonsquared
100
5.8k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.7k
Thoughts on Productivity
jonyablonski
70
4.9k
How GitHub (no longer) Works
holman
315
140k
Being A Developer After 40
akosma
91
590k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.6k
4 Signs Your Business is Dying
shpigford
185
22k
Code Reviewing Like a Champion
maltzj
526
40k
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