Created
June 3, 2020 08:24
-
-
Save gasman/9ea03d238374c733b4b05e9153b946bd to your computer and use it in GitHub Desktop.
Rich text benchmarking - management command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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