Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Performances Django REST framework - DjangoCong...

Performances Django REST framework - DjangoCong 2016

Un petit coup d'oeil sur les performances de Django REST framework.

xordoquy

June 16, 2016
Tweet

More Decks by xordoquy

Other Decks in Programming

Transcript

  1. • Négociation de contenu • Passeurs • Renderers • Throttling

    • Pagination • Authentification • Permissions
  2. 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)
  3. @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', )
  4. @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', )
  5. 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)
  6. 0 0,9 1,8 2,7 3,6 3,473 ms 2,159 ms Django

    DRF Liste avec 2 objets +60%
  7. 0 3,25 6,5 9,75 13 12,717 ms 9,599 ms Django

    DRF Liste avec 100 objets +32%
  8. 0 3,25 6,5 9,75 13 10,578 ms 12,868 ms Django

    DRF Creation d’un objet -21%
  9. ORM

  10. Vue

  11. 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
  12. 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
  13. 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
  14. 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)
  15. 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)
  16. • Nettoyer les middlewares • Faire un rendu direct •

    Désactiver la négociation de contenu