From 9b69b08d2e48830c36bbeb25e04bb81d80066044 Mon Sep 17 00:00:00 2001 From: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> Date: Thu, 18 Apr 2024 09:08:20 +0200 Subject: [PATCH] Add benchmarks for query operations. --- .../query_operators/__init__.py | 0 .../query_operators/contains/__init__.py | 0 .../query_operators/contains/benchmark.py | 19 +++++ .../query_operators/endswith/__init__.py | 0 .../query_operators/endswith/benchmark.py | 19 +++++ .../query_operators/exact/__init__.py | 0 .../query_operators/exact/benchmark.py | 19 +++++ .../fixtures/initial_data.json | 72 +++++++++++++++++++ .../query_operators/icontains/__init__.py | 0 .../query_operators/icontains/benchmark.py | 19 +++++ .../query_operators/iendswith/__init__.py | 0 .../query_operators/iendswith/benchmark.py | 19 +++++ .../query_operators/iexact/__init__.py | 0 .../query_operators/iexact/benchmark.py | 19 +++++ .../query_operators/istartswith/__init__.py | 0 .../query_operators/istartswith/benchmark.py | 19 +++++ .../query_operators/models.py | 9 +++ .../query_operators/startswith/__init__.py | 0 .../query_operators/startswith/benchmark.py | 19 +++++ benchmarks/settings.py | 8 +++ 20 files changed, 241 insertions(+) create mode 100644 benchmarks/query_benchmarks/query_operators/__init__.py create mode 100644 benchmarks/query_benchmarks/query_operators/contains/__init__.py create mode 100644 benchmarks/query_benchmarks/query_operators/contains/benchmark.py create mode 100644 benchmarks/query_benchmarks/query_operators/endswith/__init__.py create mode 100644 benchmarks/query_benchmarks/query_operators/endswith/benchmark.py create mode 100644 benchmarks/query_benchmarks/query_operators/exact/__init__.py create mode 100644 benchmarks/query_benchmarks/query_operators/exact/benchmark.py create mode 100644 benchmarks/query_benchmarks/query_operators/fixtures/initial_data.json create mode 100644 benchmarks/query_benchmarks/query_operators/icontains/__init__.py create mode 100644 benchmarks/query_benchmarks/query_operators/icontains/benchmark.py create mode 100644 benchmarks/query_benchmarks/query_operators/iendswith/__init__.py create mode 100644 benchmarks/query_benchmarks/query_operators/iendswith/benchmark.py create mode 100644 benchmarks/query_benchmarks/query_operators/iexact/__init__.py create mode 100644 benchmarks/query_benchmarks/query_operators/iexact/benchmark.py create mode 100644 benchmarks/query_benchmarks/query_operators/istartswith/__init__.py create mode 100644 benchmarks/query_benchmarks/query_operators/istartswith/benchmark.py create mode 100644 benchmarks/query_benchmarks/query_operators/models.py create mode 100644 benchmarks/query_benchmarks/query_operators/startswith/__init__.py create mode 100644 benchmarks/query_benchmarks/query_operators/startswith/benchmark.py diff --git a/benchmarks/query_benchmarks/query_operators/__init__.py b/benchmarks/query_benchmarks/query_operators/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/benchmarks/query_benchmarks/query_operators/contains/__init__.py b/benchmarks/query_benchmarks/query_operators/contains/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/benchmarks/query_benchmarks/query_operators/contains/benchmark.py b/benchmarks/query_benchmarks/query_operators/contains/benchmark.py new file mode 100644 index 0000000000..95485d1ef5 --- /dev/null +++ b/benchmarks/query_benchmarks/query_operators/contains/benchmark.py @@ -0,0 +1,19 @@ +from ....utils import bench_setup +from ..models import Book + + +class QueryFilter: + def setup(self): + bench_setup(migrate=True) + + def time_query_operator_contains(self): + list(Book.objects.filter(title__contains="in")) + list(Book.objects.filter(title__contains="the")) + list(Book.objects.filter(title__contains="do not exist")) + list(Book.objects.filter(title__contains="Peter")) + list(Book.objects.filter(title__contains="ALICE")) + list(Book.objects.filter(title__contains="charlotte")) + list(Book.objects.filter(title__contains="Willows")) + list(Book.objects.filter(title__contains="Rabbit")) + list(Book.objects.filter(title__contains="Pooh")) + list(Book.objects.filter(title__contains="Prince")) diff --git a/benchmarks/query_benchmarks/query_operators/endswith/__init__.py b/benchmarks/query_benchmarks/query_operators/endswith/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/benchmarks/query_benchmarks/query_operators/endswith/benchmark.py b/benchmarks/query_benchmarks/query_operators/endswith/benchmark.py new file mode 100644 index 0000000000..3592a94ee7 --- /dev/null +++ b/benchmarks/query_benchmarks/query_operators/endswith/benchmark.py @@ -0,0 +1,19 @@ +from ....utils import bench_setup +from ..models import Book + + +class QueryFilter: + def setup(self): + bench_setup(migrate=True) + + def time_query_operator_iendswith(self): + list(Book.objects.filter(title__iendswith="Where")) + list(Book.objects.filter(title__iendswith="the")) + list(Book.objects.filter(title__iendswith="do not exist")) + list(Book.objects.filter(title__iendswith="PAN")) + list(Book.objects.filter(title__iendswith="ALICE")) + list(Book.objects.filter(title__iendswith="charlotte")) + list(Book.objects.filter(title__iendswith="Willows")) + list(Book.objects.filter(title__iendswith="Rabbit")) + list(Book.objects.filter(title__iendswith="Pooh")) + list(Book.objects.filter(title__iendswith="Prince")) diff --git a/benchmarks/query_benchmarks/query_operators/exact/__init__.py b/benchmarks/query_benchmarks/query_operators/exact/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/benchmarks/query_benchmarks/query_operators/exact/benchmark.py b/benchmarks/query_benchmarks/query_operators/exact/benchmark.py new file mode 100644 index 0000000000..b628677ef4 --- /dev/null +++ b/benchmarks/query_benchmarks/query_operators/exact/benchmark.py @@ -0,0 +1,19 @@ +from ....utils import bench_setup +from ..models import Book + + +class QueryFilter: + def setup(self): + bench_setup(migrate=True) + + def time_query_operator_iexact(self): + list(Book.objects.filter(title__iexact="Alice in Wonderland")) + list(Book.objects.filter(title__iexact="Peter Pan")) + list(Book.objects.filter(title__iexact="do not exist")) + list(Book.objects.filter(title__iexact="Peter")) + list(Book.objects.filter(title__iexact="ALICE")) + list(Book.objects.filter(title__iexact="charlotte")) + list(Book.objects.filter(title__iexact="Willows")) + list(Book.objects.filter(title__iexact="Rabbit")) + list(Book.objects.filter(title__iexact="Pooh")) + list(Book.objects.filter(title__iexact="Prince")) diff --git a/benchmarks/query_benchmarks/query_operators/fixtures/initial_data.json b/benchmarks/query_benchmarks/query_operators/fixtures/initial_data.json new file mode 100644 index 0000000000..33b4f352ec --- /dev/null +++ b/benchmarks/query_benchmarks/query_operators/fixtures/initial_data.json @@ -0,0 +1,72 @@ +[ + { + "model": "query_filter.book", + "pk": 1, + "fields": { + "title": "Peter Pan" + } + }, + { + "model": "query_filter.book", + "pk": 2, + "fields": { + "title": "Alice in Wonderland" + } + }, + { + "model": "query_filter.book", + "pk": 3, + "fields": { + "title": "Charlotte’s Web" + } + }, + { + "model": "query_filter.book", + "pk": 4, + "fields": { + "title": "The Wind in the Willows" + } + }, + { + "model": "query_filter.book", + "pk": 5, + "fields": { + "title": "Charlie and the Chocolate Factory" + } + }, + { + "model": "query_filter.book", + "pk": 6, + "fields": { + "title": "The Velveteen Rabbit" + } + }, + { + "model": "query_filter.book", + "pk": 7, + "fields": { + "title": "Winnie the Pooh" + } + }, + { + "model": "query_filter.book", + "pk": 8, + "fields": { + "title": "The Little Prince" + } + }, + { + "model": "query_filter.book", + "pk": 9, + "fields": { + "title": "The Lion, the Witch, and the Wardrobe" + } + }, + { + "model": "query_filter.book", + "pk": 10, + "fields": { + "title": "Where the Wild Things Are" + } + } +] diff --git a/benchmarks/query_benchmarks/query_operators/icontains/__init__.py b/benchmarks/query_benchmarks/query_operators/icontains/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/benchmarks/query_benchmarks/query_operators/icontains/benchmark.py b/benchmarks/query_benchmarks/query_operators/icontains/benchmark.py new file mode 100644 index 0000000000..044e9cd964 --- /dev/null +++ b/benchmarks/query_benchmarks/query_operators/icontains/benchmark.py @@ -0,0 +1,19 @@ +from ....utils import bench_setup +from ..models import Book + + +class QueryFilter: + def setup(self): + bench_setup(migrate=True) + + def time_query_operator_icontains(self): + list(Book.objects.filter(title__icontains="in")) + list(Book.objects.filter(title__icontains="the")) + list(Book.objects.filter(title__icontains="do not exist")) + list(Book.objects.filter(title__icontains="Peter")) + list(Book.objects.filter(title__icontains="ALICE")) + list(Book.objects.filter(title__icontains="charlotte")) + list(Book.objects.filter(title__icontains="Willows")) + list(Book.objects.filter(title__icontains="Rabbit")) + list(Book.objects.filter(title__icontains="Pooh")) + list(Book.objects.filter(title__icontains="Prince")) diff --git a/benchmarks/query_benchmarks/query_operators/iendswith/__init__.py b/benchmarks/query_benchmarks/query_operators/iendswith/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/benchmarks/query_benchmarks/query_operators/iendswith/benchmark.py b/benchmarks/query_benchmarks/query_operators/iendswith/benchmark.py new file mode 100644 index 0000000000..442a9b07ad --- /dev/null +++ b/benchmarks/query_benchmarks/query_operators/iendswith/benchmark.py @@ -0,0 +1,19 @@ +from ....utils import bench_setup +from ..models import Book + + +class QueryFilter: + def setup(self): + bench_setup(migrate=True) + + def time_query_operator_endswith(self): + list(Book.objects.filter(title__endswith="Where")) + list(Book.objects.filter(title__endswith="the")) + list(Book.objects.filter(title__endswith="do not exist")) + list(Book.objects.filter(title__endswith="PAN")) + list(Book.objects.filter(title__endswith="ALICE")) + list(Book.objects.filter(title__endswith="charlotte")) + list(Book.objects.filter(title__endswith="Willows")) + list(Book.objects.filter(title__endswith="Rabbit")) + list(Book.objects.filter(title__endswith="Pooh")) + list(Book.objects.filter(title__endswith="Prince")) diff --git a/benchmarks/query_benchmarks/query_operators/iexact/__init__.py b/benchmarks/query_benchmarks/query_operators/iexact/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/benchmarks/query_benchmarks/query_operators/iexact/benchmark.py b/benchmarks/query_benchmarks/query_operators/iexact/benchmark.py new file mode 100644 index 0000000000..f1850f06c7 --- /dev/null +++ b/benchmarks/query_benchmarks/query_operators/iexact/benchmark.py @@ -0,0 +1,19 @@ +from ....utils import bench_setup +from ..models import Book + + +class QueryFilter: + def setup(self): + bench_setup(migrate=True) + + def time_query_operator_exact(self): + list(Book.objects.filter(title__exact="Alice in Wonderland")) + list(Book.objects.filter(title__exact="Peter Pan")) + list(Book.objects.filter(title__exact="do not exist")) + list(Book.objects.filter(title__exact="Peter")) + list(Book.objects.filter(title__exact="ALICE")) + list(Book.objects.filter(title__exact="charlotte")) + list(Book.objects.filter(title__exact="Willows")) + list(Book.objects.filter(title__exact="Rabbit")) + list(Book.objects.filter(title__exact="Pooh")) + list(Book.objects.filter(title__exact="Prince")) diff --git a/benchmarks/query_benchmarks/query_operators/istartswith/__init__.py b/benchmarks/query_benchmarks/query_operators/istartswith/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/benchmarks/query_benchmarks/query_operators/istartswith/benchmark.py b/benchmarks/query_benchmarks/query_operators/istartswith/benchmark.py new file mode 100644 index 0000000000..71b57661c2 --- /dev/null +++ b/benchmarks/query_benchmarks/query_operators/istartswith/benchmark.py @@ -0,0 +1,19 @@ +from ....utils import bench_setup +from ..models import Book + + +class QueryFilter: + def setup(self): + bench_setup(migrate=True) + + def time_query_operator_istartswith(self): + list(Book.objects.filter(title__istartswith="Where")) + list(Book.objects.filter(title__istartswith="the")) + list(Book.objects.filter(title__istartswith="do not exist")) + list(Book.objects.filter(title__istartswith="Peter")) + list(Book.objects.filter(title__istartswith="ALICE")) + list(Book.objects.filter(title__istartswith="charlotte")) + list(Book.objects.filter(title__istartswith="Willows")) + list(Book.objects.filter(title__istartswith="Rabbit")) + list(Book.objects.filter(title__istartswith="Pooh")) + list(Book.objects.filter(title__istartswith="Prince")) diff --git a/benchmarks/query_benchmarks/query_operators/models.py b/benchmarks/query_benchmarks/query_operators/models.py new file mode 100644 index 0000000000..5cb4035a2c --- /dev/null +++ b/benchmarks/query_benchmarks/query_operators/models.py @@ -0,0 +1,9 @@ +from django.db import models + +from ...utils import bench_setup + +bench_setup() + + +class Book(models.Model): + title = models.CharField(max_length=100) diff --git a/benchmarks/query_benchmarks/query_operators/startswith/__init__.py b/benchmarks/query_benchmarks/query_operators/startswith/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/benchmarks/query_benchmarks/query_operators/startswith/benchmark.py b/benchmarks/query_benchmarks/query_operators/startswith/benchmark.py new file mode 100644 index 0000000000..3fd88a5148 --- /dev/null +++ b/benchmarks/query_benchmarks/query_operators/startswith/benchmark.py @@ -0,0 +1,19 @@ +from ....utils import bench_setup +from ..models import Book + + +class QueryFilter: + def setup(self): + bench_setup(migrate=True) + + def time_query_operator_startswith(self): + list(Book.objects.filter(title__startswith="Where")) + list(Book.objects.filter(title__startswith="the")) + list(Book.objects.filter(title__startswith="do not exist")) + list(Book.objects.filter(title__startswith="Peter")) + list(Book.objects.filter(title__startswith="ALICE")) + list(Book.objects.filter(title__startswith="charlotte")) + list(Book.objects.filter(title__startswith="Willows")) + list(Book.objects.filter(title__startswith="Rabbit")) + list(Book.objects.filter(title__startswith="Pooh")) + list(Book.objects.filter(title__startswith="Prince")) diff --git a/benchmarks/settings.py b/benchmarks/settings.py index 4724796c5c..b11895e17d 100644 --- a/benchmarks/settings.py +++ b/benchmarks/settings.py @@ -35,6 +35,14 @@ "benchmarks.query_benchmarks.query_filter", "benchmarks.query_benchmarks.query_in_bulk", "benchmarks.query_benchmarks.query_latest", + "benchmarks.query_benchmarks.operators.contains", + "benchmarks.query_benchmarks.operators.icontains", + "benchmarks.query_benchmarks.operators.endswith", + "benchmarks.query_benchmarks.operators.iendswith", + "benchmarks.query_benchmarks.operators.exact", + "benchmarks.query_benchmarks.operators.iexact", + "benchmarks.query_benchmarks.operators.startswith", + "benchmarks.query_benchmarks.operators.istartswith", "benchmarks.query_benchmarks.query_order_by", "benchmarks.query_benchmarks.query_none", "benchmarks.query_benchmarks.query_prefetch_related",