From b6a6bb3f4c4e57f203bb051fb30dba5fbfc73585 Mon Sep 17 00:00:00 2001 From: Deepak Dinesh Date: Mon, 21 Mar 2022 20:25:03 +0530 Subject: [PATCH] orm_vs_cursor_execute benchmark added and run_comparison_benchmark updated --- TODO | 1 - .../benchmarks/orm_vs_cursor_exec/__init__.py | 0 .../orm_vs_cursor_exec/benchmark.py | 32 +++++++++++++++++++ .../benchmarks/orm_vs_cursor_exec/models.py | 8 +++++ .../benchmarks/orm_vs_cursor_exec/settings.py | 3 ++ djangobench/utils.py | 15 +++++++-- 6 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 djangobench/benchmarks/orm_vs_cursor_exec/__init__.py create mode 100644 djangobench/benchmarks/orm_vs_cursor_exec/benchmark.py create mode 100644 djangobench/benchmarks/orm_vs_cursor_exec/models.py create mode 100644 djangobench/benchmarks/orm_vs_cursor_exec/settings.py diff --git a/TODO b/TODO index 3817c00..6fffa9e 100644 --- a/TODO +++ b/TODO @@ -2,7 +2,6 @@ * Lots and lots and lots more benchmarks. Some remaining ideas - * ORM overhead compared to cursor.execute() * signal dispatch * more datastructures * more intensive url resolving and reversing diff --git a/djangobench/benchmarks/orm_vs_cursor_exec/__init__.py b/djangobench/benchmarks/orm_vs_cursor_exec/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/djangobench/benchmarks/orm_vs_cursor_exec/benchmark.py b/djangobench/benchmarks/orm_vs_cursor_exec/benchmark.py new file mode 100644 index 0000000..44b64b7 --- /dev/null +++ b/djangobench/benchmarks/orm_vs_cursor_exec/benchmark.py @@ -0,0 +1,32 @@ +from django.db import connection + +from djangobench.utils import run_comparison_benchmark + + +def setup(): + global Book + from orm_vs_cursor_exec.models import Book + for i in range(0, 300): + Book(title=f"book_{i}").save() + +def benchmark_orm(): + global Book + from orm_vs_cursor_exec.models import Book + Book.objects.all() + + +def benchmark_cursor_exec(): + cursor = connection.cursor() + cursor.execute( + "select * from book" + ) + list(cursor.fetchall()) + +run_comparison_benchmark( + benchmark_orm, + benchmark_cursor_exec, + setup=setup, + meta={ + 'description': 'Overhead of ORM compared to cursor.execute()' + } +) diff --git a/djangobench/benchmarks/orm_vs_cursor_exec/models.py b/djangobench/benchmarks/orm_vs_cursor_exec/models.py new file mode 100644 index 0000000..f75d54d --- /dev/null +++ b/djangobench/benchmarks/orm_vs_cursor_exec/models.py @@ -0,0 +1,8 @@ +from django.db import models + + +class Book(models.Model): + title = models.CharField(max_length=100) + + class Meta: + db_table = "book" diff --git a/djangobench/benchmarks/orm_vs_cursor_exec/settings.py b/djangobench/benchmarks/orm_vs_cursor_exec/settings.py new file mode 100644 index 0000000..af7572a --- /dev/null +++ b/djangobench/benchmarks/orm_vs_cursor_exec/settings.py @@ -0,0 +1,3 @@ +from djangobench.base_settings import * # NOQA + +INSTALLED_APPS = ['orm_vs_cursor_exec'] diff --git a/djangobench/utils.py b/djangobench/utils.py index 46112ff..0cd405d 100644 --- a/djangobench/utils.py +++ b/djangobench/utils.py @@ -114,8 +114,19 @@ def run_comparison_benchmark(benchmark_a, benchmark_b, migrate=True, setup=None, django.setup() if migrate: - from django.core.management import call_command - call_command("migrate", verbosity=0) + from django.core.management import CommandError, call_command + if django.VERSION < (1, 7): + call_command("syncdb", run_syncdb=True, verbosity=0) + else: + call_command("migrate", run_syncdb=True, verbosity=0) + if django.VERSION >= (1, 8): + try: + call_command("loaddata", "initial_data", verbosity=0) + except CommandError as exc: + # Django 1.10+ raises if the file doesn't exist and not + # all benchmarks have files. + if 'No fixture named' not in str(exc): + raise if setup: setup()