Skip to content

Commit

Permalink
Merge pull request #14 from bilbeyt/feature/GH-10
Browse files Browse the repository at this point in the history
Benchmark is added to README resolves #10
  • Loading branch information
bilbeyt authored May 21, 2020
2 parents 8c69f6d + 446e029 commit 7d50556
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 5 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,28 @@ FastPaginator has a built-in cache system. It does not cache QuerySets but cache
```python
./manage.py test fast_pagination.tests
```

## Benchmark

This benchmark is executed on Postgres9.6 with a table that has 1000000 rows, and fetched only one field. Results can be seen below.

| Paginator | Min | Max | Mean | StdDev | Median |
|---------------|------------------|------------------|------------------|---------------|------------------|
| Django | 93.5535 (1.53) | 95.7217 (1.54) | 94.7340 (1.53) | 0.9755 (2.32) | 94.9046 (1.54) |
| FastPaginator | 61.1488 (1.0) | 62.3316 (1.0) | 61.7489 (1.0) | 0.4205 (1.0) | 61.7649 (1.0) |


You can also run benchmark tests following instructions:

1. Run migrations if app needs using
```bash
./manage.py migrate
```
2. Call generate_users command from command line using
```bash
./manage.py generate_users 1000000
```
3. Run tests using
```bash
pytest fast_pagination/tests.py
```
5 changes: 5 additions & 0 deletions fast_pagination/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class FastPaginationConfig(AppConfig):
name = 'fast_pagination'
Empty file.
Empty file.
26 changes: 26 additions & 0 deletions fast_pagination/management/commands/generate_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import logging
from django.core.management.base import BaseCommand
from django.contrib.auth.models import User


logger = logging.getLogger(__name__)


class Command(BaseCommand):

def add_arguments(self, parser):
parser.add_argument("user_count", type=int)

def handle(self, *args, **options):
batch_size = 1000
user_count = options["user_count"]
users = []
for i in range(1, user_count + 1):
username = f"user_{i}"
user = User(username=username)
user.set_unusable_password()
users.append(user)
if i % batch_size == 0:
logger.info("User #%s created", i)
User.objects.bulk_create(
users, batch_size=batch_size, ignore_conflicts=True)
45 changes: 44 additions & 1 deletion fast_pagination/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import unittest

import logging
import pytest
from django.conf import settings
from django.contrib.auth.models import User
from django.core.paginator import (
InvalidPage, PageNotAnInteger, EmptyPage, Paginator)

from fast_pagination.helpers import FastPaginator
from django.core.paginator import InvalidPage, PageNotAnInteger, EmptyPage



logger = logging.getLogger(__name__)


class FastObjectPaginatorTestMethods(unittest.TestCase):
Expand Down Expand Up @@ -36,5 +47,37 @@ def test_key_not_found(self):
example_list, 2)


@pytest.fixture(scope='session')
def django_db_setup():
settings.DATABASES['default'] = {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'paginator',
'USER': 'postgres',
'HOST': 'localhost',
'PORT': 5432,
}


def speed_test(queryset, paginator_cls):
paginator = paginator_cls(queryset, 1000)
for page in paginator.page_range:
objs = paginator.page(page).object_list\
.values_list('username', flat=True)
usernames = ",".join(objs)
logger.info("%s\n", usernames)


@pytest.mark.django_db
def test_django_paginator_speed(benchmark):
queryset = User.objects.all()
benchmark(speed_test, queryset, Paginator)


@pytest.mark.django_db
def test_fast_paginator_speed(benchmark):
queryset = User.objects.all()
benchmark(speed_test, queryset, FastPaginator)


if __name__ == '__main__':
unittest.main()
21 changes: 21 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pagination_app.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
Empty file added pagination_app/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions pagination_app/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for pagination_app project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pagination_app.settings')

application = get_asgi_application()
150 changes: 150 additions & 0 deletions pagination_app/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
"""
Django settings for pagination_app project.
Generated by 'django-admin startproject' using Django 3.0.4.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import os
import sys

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'y4qx=gte724g(x032&=n2fd*4kjdf#10a%(=@ka!2s8mb6!rx6'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'fast_pagination'
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'pagination_app.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'pagination_app.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'paginator',
'USER': 'postgres',
'HOST': 'localhost',
'PORT': 5432,
}
}


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'format': '[ %(asctime)s - %(levelname)s ] %(message)s'
}
},
'handlers': {
'default': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'stream': sys.stdout,
'formatter': 'default'
},
},
'loggers': {
'': {
'handlers': ['default'],
'propagate': True,
'level': 'INFO',
},
}
}
21 changes: 21 additions & 0 deletions pagination_app/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""pagination_app URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
path('admin/', admin.site.urls),
]
16 changes: 16 additions & 0 deletions pagination_app/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for pagination_app project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pagination_app.settings')

application = get_wsgi_application()
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Django==3.0.4
psycopg2-binary==2.8.4
pytest==5.4.2
pytest-benchmark==3.2.3
pytest-django==3.9.0
15 changes: 11 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[metadata]
name = django-fast-paginator
version = v1.0.3
version = v1.0.4
description = A Django app to paginate querysets faster.
long_description = file: README.md
long_description_content_type = text/markdown
author = Tolga Bilbey
author_email = bilbeyt@gmail.com
license = MIT
url = https://github.com/bilbeyt/django-fast-pagination
download_url = https://github.com/bilbeyt/django-fast-pagination/archive/v1.0.3.tar.gz
download_url = https://github.com/bilbeyt/django-fast-pagination/archive/v1.0.4.tar.gz
keywords =
django
pagination
Expand All @@ -18,6 +18,7 @@ classifiers =
Environment :: Web Environment
Framework :: Django
Framework :: Django :: 2.2
Framework :: Django :: 3.0
Intended Audience :: Developers
License :: OSI Approved :: MIT License
Operating System :: OS Independent
Expand All @@ -32,7 +33,13 @@ classifiers =

[options]
include_package_data = true
packages = find:
packages =
fast_pagination
python_requires = >= 3.0.0
setup_requires =
setuptools
setuptools
test_requires =
pytest
pytest-benchmark
pytest-django
Django

0 comments on commit 7d50556

Please sign in to comment.