forked from openwisp/django-rest-framework-gis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_performance.py
55 lines (47 loc) · 1.89 KB
/
test_performance.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
import sys
from django.conf import settings
# this test must be run explicitly
# either by calling:
# django test --keepdb django_restframework_gis_tests.test_performance
# or by setting ``settings.TEST_PERFORMANCE`` to ``True``
if (
'django_restframework_gis_tests.test_performance' in sys.argv
or settings.TEST_PERFORMANCE
):
from contexttimer import Timer
from django.test import TestCase
from rest_framework.renderers import JSONRenderer
from rest_framework_gis import serializers as gis_serializers
from .models import Location
class TestRestFrameworkGisPerformance(TestCase):
NUMBER_OF_LOCATIONS = 10000
def _create_data(self):
"""creates a bunch of gis models instances"""
locations = []
name = 'l{0}'
slug = 'l{0}'
wkt = 'POINT (13.{0}125000020002 42.{0}565179379999)'
for n in range(1, self.NUMBER_OF_LOCATIONS):
locations.append(
Location(
name=name.format(n), slug=slug.format(n), geometry=wkt.format(n)
)
)
Location.objects.bulk_create(locations)
def test_geojson_performance(self):
class PerfSerializer(gis_serializers.GeoFeatureModelSerializer):
class Meta:
model = Location
geo_field = 'geometry'
fields = '__all__'
# create data
self._create_data()
# initialize serializer
serializer = PerfSerializer(Location.objects.all(), many=True)
with Timer() as t:
JSONRenderer().render(serializer.data)
# print results
msg = 'GeoJSON rendering of {} objects ' 'completed in {}'.format(
self.NUMBER_OF_LOCATIONS, t.elapsed
)
print(f'\n\033[95m{msg}\033[0m')