Skip to content

Instantly share code, notes, and snippets.

@gasman
Created June 3, 2020 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gasman/9ea03d238374c733b4b05e9153b946bd to your computer and use it in GitHub Desktop.
Save gasman/9ea03d238374c733b4b05e9153b946bd to your computer and use it in GitHub Desktop.
Rich text benchmarking - management command
On a real-world site with 3058 HTML fragments
with richtext-benchmark/original:
1. 13.098440
2. 14.320241
3. 13.525923
with richtext-benchmark/loic:
1. 32.462429
2. 34.154890
3. 34.121001
with richtext-benchmark/matthew:
1. 13.270576
2. 13.325513
3. 15.039238
import time
from django.core.management.base import BaseCommand
from wagtail.core.blocks import RichTextBlock
from wagtail.core.models import get_page_models
from wagtail.core.fields import RichTextField, StreamField
from wagtail.core.rich_text import expand_db_html
class Command(BaseCommand):
def handle(self, **options):
print("Building test corpus...")
corpus = []
for page_model in get_page_models():
rich_text_fields = []
stream_fields = []
for field in page_model._meta.get_fields():
if isinstance(field, RichTextField):
rich_text_fields.append(field.name)
elif isinstance(field, StreamField):
stream_fields.append(field.name)
for page in page_model.objects.all():
for field in rich_text_fields:
corpus.append(getattr(page, field))
for field in stream_fields:
for block in getattr(page, field):
if isinstance(block.block, RichTextBlock):
corpus.append(block.value.source)
print("Built test corpus of %d HTML fragments." % len(corpus))
start = time.time()
for i, html in enumerate(corpus):
if i % 1000 == 0:
print("processed %d fragments" % i)
expand_db_html(html)
end = time.time()
print("Completed in %fs" % (end - start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment