|
@@ -11,6 +11,7 @@ import yaml
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
from django.contrib.messages import get_messages
|
|
from django.contrib.messages import get_messages
|
|
|
from django.db import connection
|
|
from django.db import connection
|
|
|
|
|
+from django.db.models import FETCH_PEERS
|
|
|
from django.http import QueryDict, StreamingHttpResponse
|
|
from django.http import QueryDict, StreamingHttpResponse
|
|
|
from django.test import override_settings, tag
|
|
from django.test import override_settings, tag
|
|
|
from django.test.utils import CaptureQueriesContext
|
|
from django.test.utils import CaptureQueriesContext
|
|
@@ -22,7 +23,6 @@ from core.models import ObjectChange, ObjectType
|
|
|
from dcim.choices import *
|
|
from dcim.choices import *
|
|
|
from dcim.constants import *
|
|
from dcim.constants import *
|
|
|
from dcim.models import *
|
|
from dcim.models import *
|
|
|
-from dcim.views import DeviceTypeListView, ModuleTypeListView
|
|
|
|
|
from extras.models import ConfigContext, ConfigTemplate
|
|
from extras.models import ConfigContext, ConfigTemplate
|
|
|
from ipam.models import ASN, RIR, VLAN, VRF, IPAddress, VLANGroup
|
|
from ipam.models import ASN, RIR, VLAN, VRF, IPAddress, VLANGroup
|
|
|
from netbox.choices import (
|
|
from netbox.choices import (
|
|
@@ -1288,15 +1288,12 @@ inventory-items:
|
|
|
one_bay_device_type = make_device_type('Export Query DT One Bay', 1)
|
|
one_bay_device_type = make_device_type('Export Query DT One Bay', 1)
|
|
|
five_bay_device_type = make_device_type('Export Query DT Five Bays', 5)
|
|
five_bay_device_type = make_device_type('Export Query DT Five Bays', 5)
|
|
|
|
|
|
|
|
- view = DeviceTypeListView()
|
|
|
|
|
exporter = get_exporter('yaml')
|
|
exporter = get_exporter('yaml')
|
|
|
- view.queryset = DeviceType.objects.filter(pk=one_bay_device_type.pk)
|
|
|
|
|
with CaptureQueriesContext(connection) as one_bay_queries:
|
|
with CaptureQueriesContext(connection) as one_bay_queries:
|
|
|
- exporter.export(request=None, queryset=view.get_export_queryset(exporter))
|
|
|
|
|
|
|
+ exporter.export(request=None, queryset=DeviceType.objects.filter(pk=one_bay_device_type.pk))
|
|
|
|
|
|
|
|
- view.queryset = DeviceType.objects.filter(pk=five_bay_device_type.pk)
|
|
|
|
|
with CaptureQueriesContext(connection) as five_bay_queries:
|
|
with CaptureQueriesContext(connection) as five_bay_queries:
|
|
|
- exporter.export(request=None, queryset=view.get_export_queryset(exporter))
|
|
|
|
|
|
|
+ exporter.export(request=None, queryset=DeviceType.objects.filter(pk=five_bay_device_type.pk))
|
|
|
|
|
|
|
|
self.assertEqual(len(one_bay_queries), len(five_bay_queries))
|
|
self.assertEqual(len(one_bay_queries), len(five_bay_queries))
|
|
|
|
|
|
|
@@ -2187,10 +2184,10 @@ interfaces:
|
|
|
self.assertEqual(channel.parent, imported_parent)
|
|
self.assertEqual(channel.parent, imported_parent)
|
|
|
|
|
|
|
|
def test_bulk_yaml_export_prefetches_module_bay_types_on_the_module_type_itself(self):
|
|
def test_bulk_yaml_export_prefetches_module_bay_types_on_the_module_type_itself(self):
|
|
|
- """Compares an unprefetched to_yaml() call per instance against a YAML export (which
|
|
|
|
|
- prefetches and issues no queries of its own beyond that), rather than a row-count
|
|
|
|
|
- comparison, which other per-instance relations that legitimately scale with it would
|
|
|
|
|
- swamp."""
|
|
|
|
|
|
|
+ """Compares an unprefetched to_yaml() call per instance (using the same fetch mode) against
|
|
|
|
|
+ a YAML export (which prefetches and issues no queries of its own beyond that), rather than
|
|
|
|
|
+ a row-count comparison, which other per-instance relations that legitimately scale with it
|
|
|
|
|
+ would swamp."""
|
|
|
manufacturer = Manufacturer.objects.create(name='Export Query MT Manufacturer', slug='export-query-mt-mfr')
|
|
manufacturer = Manufacturer.objects.create(name='Export Query MT Manufacturer', slug='export-query-mt-mfr')
|
|
|
bay_type = ModuleBayType.objects.create(name='Export Query MT SFP28', slug='export-query-mt-sfp28')
|
|
bay_type = ModuleBayType.objects.create(name='Export Query MT SFP28', slug='export-query-mt-sfp28')
|
|
|
|
|
|
|
@@ -2202,13 +2199,10 @@ interfaces:
|
|
|
pks = [mt.pk for mt in module_types]
|
|
pks = [mt.pk for mt in module_types]
|
|
|
|
|
|
|
|
with CaptureQueriesContext(connection) as unprefetched:
|
|
with CaptureQueriesContext(connection) as unprefetched:
|
|
|
- [obj.to_yaml() for obj in ModuleType.objects.filter(pk__in=pks)]
|
|
|
|
|
|
|
+ [obj.to_yaml() for obj in ModuleType.objects.filter(pk__in=pks).fetch_mode(FETCH_PEERS)]
|
|
|
|
|
|
|
|
- view = ModuleTypeListView()
|
|
|
|
|
- exporter = get_exporter('yaml')
|
|
|
|
|
- view.queryset = ModuleType.objects.filter(pk__in=pks)
|
|
|
|
|
with CaptureQueriesContext(connection) as prefetched:
|
|
with CaptureQueriesContext(connection) as prefetched:
|
|
|
- exporter.export(request=None, queryset=view.get_export_queryset(exporter))
|
|
|
|
|
|
|
+ get_exporter('yaml').export(request=None, queryset=ModuleType.objects.filter(pk__in=pks))
|
|
|
|
|
|
|
|
# Without the prefetch, each of the 5 module types issues its own module_bay_types
|
|
# Without the prefetch, each of the 5 module types issues its own module_bay_types
|
|
|
# query; with it, exactly one query serves all 5.
|
|
# query; with it, exactly one query serves all 5.
|