Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ORM overhead compared to cursor.execute() benchmark added #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file.
32 changes: 32 additions & 0 deletions djangobench/benchmarks/orm_vs_cursor_exec/benchmark.py
Original file line number Diff line number Diff line change
@@ -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()'
}
)
8 changes: 8 additions & 0 deletions djangobench/benchmarks/orm_vs_cursor_exec/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.db import models


class Book(models.Model):
title = models.CharField(max_length=100)

class Meta:
db_table = "book"
3 changes: 3 additions & 0 deletions djangobench/benchmarks/orm_vs_cursor_exec/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from djangobench.base_settings import * # NOQA

INSTALLED_APPS = ['orm_vs_cursor_exec']
15 changes: 13 additions & 2 deletions djangobench/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down