forked from django-json-api/django-rest-framework-json-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_renderers.py
137 lines (102 loc) · 4.54 KB
/
test_renderers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import time
import pytest
from rest_framework import status
from rest_framework.response import Response
from rest_framework.routers import SimpleRouter
from rest_framework_json_api.parsers import JSONParser
from rest_framework_json_api.relations import ResourceRelatedField
from rest_framework_json_api.renderers import JSONRenderer
from rest_framework_json_api.views import ModelViewSet
from tests.models import ForeignKeySource, ForeignKeyTarget
from rest_framework_json_api import serializers
from django.urls import reverse
from django.core.signals import request_finished, request_started
import json
@pytest.fixture
def service(db):
# configure self.attribute
service = ForeignKeyTarget.objects.create(name="service")
layers = [ForeignKeySource(name="l", target=service) for x in range(10000)]
ForeignKeySource.objects.bulk_create(objs=layers)
return service
class TestIncludePerformance:
@pytest.mark.urls(__name__)
def test_benchmark(self, client, service):
url = reverse("benchmark-detail", kwargs={"pk": service.pk})
response = client.get(f"{url}?include=sources")
result = json.loads(response.content)
print(len(result["included"]))
assert response.status_code == status.HTTP_200_OK
assert len(result["included"]) == 10000
class ForeignKeyTargetSerializer(serializers.ModelSerializer):
a = serializers.CharField(default=serializers.CreateOnlyDefault("default"))
b = serializers.CharField(default=serializers.CreateOnlyDefault("default"))
c = serializers.CharField(default=serializers.CreateOnlyDefault("default"))
d = serializers.CharField(default=serializers.CreateOnlyDefault("default"))
e = serializers.CharField(default=serializers.CreateOnlyDefault("default"))
f = serializers.CharField(default=serializers.CreateOnlyDefault("default"))
g = serializers.CharField(default=serializers.CreateOnlyDefault("default"))
h = serializers.CharField(default=serializers.CreateOnlyDefault("default"))
i = serializers.CharField(default=serializers.CreateOnlyDefault("default"))
j = serializers.CharField(default=serializers.CreateOnlyDefault("default"))
k = serializers.CharField(default=serializers.CreateOnlyDefault("default"))
l = serializers.CharField(default=serializers.CreateOnlyDefault("default"))
sources = ResourceRelatedField(
model=ForeignKeySource,
many=True,
read_only=True,
)
class Meta:
model = ForeignKeyTarget
fields = "__all__"
class IncludeableForeignKeyTargetSerializer(ForeignKeyTargetSerializer):
included_serializers = {
"sources": ForeignKeyTargetSerializer
}
class BenchmarkAPIView(ModelViewSet):
parser_classes = [JSONParser]
renderer_classes = [JSONRenderer]
resource_name = "service"
serializer_class = IncludeableForeignKeyTargetSerializer
queryset = ForeignKeyTarget.objects.all()
def retrieve(self, request, *args, **kwargs):
global db_time
global serializer_time
global started
started = time.time()
db_start = time.time()
instance = self.get_object()
db_time = time.time() - db_start
serializer_start = time.time()
serializer = self.get_serializer(instance)
data = serializer.data
serializer_time = time.time() - serializer_start
return Response(status=status.HTTP_200_OK, data=data)
def dispatch(self, request, *args, **kwargs):
global dispatch_time
global render_time
dispatch_start = time.time()
ret = super().dispatch(request, *args, **kwargs)
render_start = time.time()
ret.render()
render_time = time.time() - render_start
dispatch_time = time.time() - dispatch_start
return ret
def started(sender, **kwargs):
global started
started = time.time()
def finished(sender, **kwargs):
total = time.time() - started
api_view_time = dispatch_time - (render_time + serializer_time + db_time)
request_response_time = dispatch_time - total
print()
print("Database lookup | %.4fs" % db_time)
print("Serialization | %.4fs" % serializer_time)
print("Django request/response | %.4fs" % request_response_time)
print("API view | %.4fs" % api_view_time)
print("Response rendering | %.4fs" % render_time)
request_started.connect(started)
request_finished.connect(finished)
router = SimpleRouter()
router.register(r"benchmark", BenchmarkAPIView, basename="benchmark")
urlpatterns = router.urls