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
360
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
CEDEC2025 長期運営ゲームをあと10年続けるための0から始める自動テスト ~4000項目を50%自動化し、月1→毎日実行にした3年間~
akatsukigames_tech
0
150
コーディングは技術者(エンジニア)の嗜みでして / Learning the System Development Mindset from Rock Lady
mackey0225
2
570
CSC305 Summer Lecture 04
javiergs
PRO
1
110
大規模FlutterプロジェクトのCI実行時間を約8割削減した話
teamlab
PRO
0
490
MCPで実現するAIエージェント駆動のNext.jsアプリデバッグ手法
nyatinte
6
840
UbieのAIパートナーを支えるコンテキストエンジニアリング実践
syucream
2
700
兎に角、コードレビュー
mitohato14
0
150
Dart 参戦!!静的型付き言語界の隠れた実力者
kno3a87
0
210
AI OCR API on Lambdaを Datadogで可視化してみた
nealle
0
180
Flutter로 Gemini와 MCP를 활용한 Agentic App 만들기 - 박제창 2025 I/O Extended Seoul
itsmedreamwalker
0
150
A Gopher's Guide to Vibe Coding
danicat
0
170
令和最新版手のひらコンピュータ
koba789
14
8k
Featured
See All Featured
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.8k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
139
34k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
126
53k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Designing for Performance
lara
610
69k
The Straight Up "How To Draw Better" Workshop
denniskardys
236
140k
Scaling GitHub
holman
462
140k
Practical Orchestrator
shlominoach
190
11k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
820
Optimizing for Happiness
mojombo
379
70k
The Pragmatic Product Professional
lauravandoore
36
6.8k
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