benches.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from __future__ import absolute_import, unicode_literals
  2. from django.test import TestCase
  3. from django.urls import reverse
  4. from django.utils import timezone
  5. from wagtail.core.models import Page, Site
  6. from wagtail.test.benchmark import Benchmark
  7. from wagtail.test.testapp.models import SingleEventPage, StreamPage
  8. from wagtail.test.utils import WagtailTestUtils
  9. class BenchPageExplorerWith50LargePages(Benchmark, WagtailTestUtils, TestCase):
  10. """
  11. Creates 50 pages with large body content and benches the explorer.
  12. This will be slow if the body content is being loaded from the database.
  13. """
  14. def setUp(self):
  15. self.root_page = Page.objects.get(id=1)
  16. # Add a site so the URLs render correctly
  17. Site.objects.create(is_default_site=True, root_page=self.root_page)
  18. # Create a large piece of body text
  19. body = (
  20. "["
  21. + ",".join(['{"type": "text", "value": "%s"}' % ("foo" * 2000)] * 100)
  22. + "]"
  23. )
  24. # Create 50 simple pages with long content fields
  25. for i in range(50):
  26. self.root_page.add_child(
  27. instance=StreamPage(
  28. title="Page {}".format(i + 1),
  29. slug=str(i + 1),
  30. body=body,
  31. )
  32. )
  33. self.login()
  34. def bench(self):
  35. response = self.client.get(
  36. reverse("wagtailadmin_explore", args=(self.root_page.id,))
  37. )
  38. # Check the response was good
  39. self.assertEqual(response.status_code, 200)
  40. # Check every single page was rendered
  41. self.assertContains(response, "Page 1")
  42. self.assertContains(response, "Page 49")
  43. class BenchPageExplorerWithCustomURLPages(Benchmark, WagtailTestUtils, TestCase):
  44. """
  45. Creates 50 pages of a class with a customised the .url property.
  46. This will check how long it takes to generate URLs for all of these
  47. pages.
  48. """
  49. def setUp(self):
  50. self.root_page = Page.objects.get(id=1)
  51. # Add a site so the URLs render correctly
  52. Site.objects.create(is_default_site=True, root_page=self.root_page)
  53. # Create 50 blog pages
  54. for i in range(50):
  55. self.root_page.add_child(
  56. instance=SingleEventPage(
  57. title="Event {}".format(i + 1),
  58. slug=str(i + 1),
  59. date_from=timezone.now(),
  60. audience="public",
  61. location="reykjavik",
  62. cost="cost",
  63. )
  64. )
  65. self.login()
  66. def bench(self):
  67. response = self.client.get(
  68. reverse("wagtailadmin_explore", args=(self.root_page.id,))
  69. )
  70. # Check the response was good
  71. self.assertEqual(response.status_code, 200)
  72. # Check every single page was rendered
  73. self.assertContains(response, "Event 1")
  74. self.assertContains(response, "Event 49")
  75. # Check the URLs were rendered correctly
  76. self.assertContains(response, 'a href="http:///49/pointless-suffix/"')