Debian Codegraph @

tests/django_restframework_gis_tests/test_performance.py

raw
1 import sys
2
3 from django.conf import settings
4
5 # this test must be run explicitly
6 # either by calling:
7 # django test --keepdb django_restframework_gis_tests.test_performance
8 # or by setting ``settings.TEST_PERFORMANCE`` to ``True``
9 if (
10 "django_restframework_gis_tests.test_performance" in sys.argv
11 or settings.TEST_PERFORMANCE
12 ):
13 from contexttimer import Timer
14 from django.test import TestCase
15 from rest_framework.renderers import JSONRenderer
16
17 from rest_framework_gis import serializers as gis_serializers
18
19 from .models import Location
20
21 class TestRestFrameworkGisPerformance(TestCase):
22 NUMBER_OF_LOCATIONS = 10000
23
24 def _create_data(self):
25 """creates a bunch of gis models instances"""
26 locations = []
27 name = "l{0}"
28 slug = "l{0}"
29 wkt = "POINT (13.{0}125000020002 42.{0}565179379999)"
30 for n in range(1, self.NUMBER_OF_LOCATIONS):
31 locations.append(
32 Location(
33 name=name.format(n), slug=slug.format(n), geometry=wkt.format(n)
34 )
35 )
36 Location.objects.bulk_create(locations)
37
38 def test_geojson_performance(self):
39 class PerfSerializer(gis_serializers.GeoFeatureModelSerializer):
40 class Meta:
41 model = Location
42 geo_field = "geometry"
43 fields = "__all__"
44
45 # create data
46 self._create_data()
47 # initialize serializer
48 serializer = PerfSerializer(Location.objects.all(), many=True)
49 with Timer() as t:
50 JSONRenderer().render(serializer.data)
51 # print results
52 msg = "GeoJSON rendering of {} objects " "completed in {}".format(
53 self.NUMBER_OF_LOCATIONS, t.elapsed
54 )
55 print(f"\n\033[95m{msg}\033[0m")