├── .gitignore ├── counters ├── __init__.py ├── admin.py ├── apps.py ├── migrations │ ├── 0001_initial.py │ └── __init__.py ├── models.py ├── tests.py └── views.py ├── django_sqlite_benchmark ├── __init__.py ├── asgi.py ├── settings.py ├── urls.py └── wsgi.py ├── generated.db ├── locustfile.py ├── manage.py ├── requirements.txt └── write ├── __init__.py ├── admin.py ├── apps.py ├── migrations ├── 0001_initial.py └── __init__.py ├── models.py ├── tests.py └── views.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.sqlite3 2 | -------------------------------------------------------------------------------- /counters/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonw/django_sqlite_benchmark/a46139e28bf5024e2581300ac8ffc72ad71c5694/counters/__init__.py -------------------------------------------------------------------------------- /counters/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from .models import Counter 3 | 4 | 5 | admin.site.register(Counter) 6 | -------------------------------------------------------------------------------- /counters/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class CountersConfig(AppConfig): 5 | default_auto_field = "django.db.models.BigAutoField" 6 | name = "counters" 7 | -------------------------------------------------------------------------------- /counters/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.2 on 2022-10-20 20:49 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name="Counter", 15 | fields=[ 16 | ( 17 | "id", 18 | models.BigAutoField( 19 | auto_created=True, 20 | primary_key=True, 21 | serialize=False, 22 | verbose_name="ID", 23 | ), 24 | ), 25 | ("slug", models.SlugField()), 26 | ("count", models.IntegerField(default=0)), 27 | ], 28 | ), 29 | ] 30 | -------------------------------------------------------------------------------- /counters/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonw/django_sqlite_benchmark/a46139e28bf5024e2581300ac8ffc72ad71c5694/counters/migrations/__init__.py -------------------------------------------------------------------------------- /counters/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class Counter(models.Model): 5 | slug = models.SlugField() 6 | count = models.IntegerField(default=0) 7 | 8 | def __str__(self): 9 | return "{}: {}".format(self.slug, self.count) 10 | -------------------------------------------------------------------------------- /counters/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /counters/views.py: -------------------------------------------------------------------------------- 1 | from django.http import HttpResponse 2 | from django.db.models import F 3 | from .models import Counter 4 | 5 | 6 | def counter(request, slug): 7 | Counter.objects.get_or_create(slug=slug) 8 | Counter.objects.filter(slug=slug).update(count=F("count") + 1) 9 | return HttpResponse(Counter.objects.get(slug=slug).count, content_type="text/plain") 10 | 11 | 12 | def hello_word(request): 13 | return HttpResponse("Hello World!", content_type="text/plain") 14 | -------------------------------------------------------------------------------- /django_sqlite_benchmark/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonw/django_sqlite_benchmark/a46139e28bf5024e2581300ac8ffc72ad71c5694/django_sqlite_benchmark/__init__.py -------------------------------------------------------------------------------- /django_sqlite_benchmark/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for django_sqlite_benchmark project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_sqlite_benchmark.settings") 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /django_sqlite_benchmark/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for django_sqlite_benchmark project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.2. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = "django-insecure-!xgd7y3q_nhlg3@8u+zb(^8wh17ixg3new&ist0nhcercho@)*" 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | "django.contrib.admin", 35 | "django.contrib.auth", 36 | "django.contrib.contenttypes", 37 | "django.contrib.sessions", 38 | "django.contrib.messages", 39 | "django.contrib.staticfiles", 40 | "counters", 41 | "write", 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | "django.middleware.security.SecurityMiddleware", 46 | "django.contrib.sessions.middleware.SessionMiddleware", 47 | "django.middleware.common.CommonMiddleware", 48 | "django.middleware.csrf.CsrfViewMiddleware", 49 | "django.contrib.auth.middleware.AuthenticationMiddleware", 50 | "django.contrib.messages.middleware.MessageMiddleware", 51 | "django.middleware.clickjacking.XFrameOptionsMiddleware", 52 | ] 53 | 54 | ROOT_URLCONF = "django_sqlite_benchmark.urls" 55 | 56 | TEMPLATES = [ 57 | { 58 | "BACKEND": "django.template.backends.django.DjangoTemplates", 59 | "DIRS": [], 60 | "APP_DIRS": True, 61 | "OPTIONS": { 62 | "context_processors": [ 63 | "django.template.context_processors.debug", 64 | "django.template.context_processors.request", 65 | "django.contrib.auth.context_processors.auth", 66 | "django.contrib.messages.context_processors.messages", 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = "django_sqlite_benchmark.wsgi.application" 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | "default": { 80 | "ENGINE": "django.db.backends.sqlite3", 81 | "NAME": BASE_DIR / "db.sqlite3", 82 | "OPTIONS": { 83 | "timeout": 20 84 | } 85 | } 86 | } 87 | 88 | 89 | # Password validation 90 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 91 | 92 | AUTH_PASSWORD_VALIDATORS = [ 93 | { 94 | "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", 95 | }, 96 | { 97 | "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", 98 | }, 99 | { 100 | "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", 101 | }, 102 | { 103 | "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", 104 | }, 105 | ] 106 | 107 | 108 | # Internationalization 109 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 110 | 111 | LANGUAGE_CODE = "en-us" 112 | 113 | TIME_ZONE = "UTC" 114 | 115 | USE_I18N = True 116 | 117 | USE_TZ = True 118 | 119 | 120 | # Static files (CSS, JavaScript, Images) 121 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 122 | 123 | STATIC_URL = "static/" 124 | 125 | # Default primary key field type 126 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 127 | 128 | DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" 129 | -------------------------------------------------------------------------------- /django_sqlite_benchmark/urls.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from django.urls import path 3 | from counters.views import counter, hello_word 4 | from write.views import write_row_from_json 5 | 6 | urlpatterns = [ 7 | path("admin/", admin.site.urls), 8 | # /counter// runs counter view 9 | path("counter//", counter), 10 | path("write/", write_row_from_json), 11 | path("hello/", hello_word), 12 | ] 13 | -------------------------------------------------------------------------------- /django_sqlite_benchmark/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for django_sqlite_benchmark project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_sqlite_benchmark.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /generated.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonw/django_sqlite_benchmark/a46139e28bf5024e2581300ac8ffc72ad71c5694/generated.db -------------------------------------------------------------------------------- /locustfile.py: -------------------------------------------------------------------------------- 1 | from locust import HttpUser, task 2 | import itertools 3 | import sqlite3 4 | 5 | 6 | # Output as dictionaries 7 | sqlite3.enable_callback_tracebacks(True) 8 | db = sqlite3.connect("generated.db") 9 | db.row_factory = sqlite3.Row 10 | rows = ( 11 | dict(r) 12 | for r in itertools.cycle( 13 | db.execute("SELECT * FROM rows ORDER BY random()").fetchall() 14 | ) 15 | ) 16 | 17 | 18 | class CounterOne(HttpUser): 19 | # @task 20 | # def counter_one(self): 21 | # self.client.get("/counter/one/") 22 | 23 | @task 24 | def write(self): 25 | self.client.post("/write/", next(rows)) 26 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_sqlite_benchmark.settings") 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == "__main__": 22 | main() 23 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | django 2 | locust 3 | uvicorn 4 | gunicorn -------------------------------------------------------------------------------- /write/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonw/django_sqlite_benchmark/a46139e28bf5024e2581300ac8ffc72ad71c5694/write/__init__.py -------------------------------------------------------------------------------- /write/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from .models import Row 3 | 4 | admin.site.register(Row) 5 | -------------------------------------------------------------------------------- /write/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class WriteConfig(AppConfig): 5 | default_auto_field = "django.db.models.BigAutoField" 6 | name = "write" 7 | -------------------------------------------------------------------------------- /write/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.2 on 2022-10-20 22:21 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name="Row", 15 | fields=[ 16 | ( 17 | "id", 18 | models.BigAutoField( 19 | auto_created=True, 20 | primary_key=True, 21 | serialize=False, 22 | verbose_name="ID", 23 | ), 24 | ), 25 | ("name", models.TextField()), 26 | ("campaign", models.TextField()), 27 | ("voice", models.TextField()), 28 | ("recognize", models.TextField()), 29 | ("inside", models.FloatField()), 30 | ("growth", models.TextField()), 31 | ("side", models.FloatField()), 32 | ("yard", models.TextField()), 33 | ("discussion", models.TextField()), 34 | ], 35 | ), 36 | ] 37 | -------------------------------------------------------------------------------- /write/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonw/django_sqlite_benchmark/a46139e28bf5024e2581300ac8ffc72ad71c5694/write/migrations/__init__.py -------------------------------------------------------------------------------- /write/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class Row(models.Model): 5 | name = models.TextField() 6 | campaign = models.TextField() 7 | voice = models.TextField() 8 | recognize = models.TextField() 9 | inside = models.FloatField() 10 | growth = models.TextField() 11 | side = models.FloatField() 12 | yard = models.TextField() 13 | discussion = models.TextField() 14 | 15 | def __str__(self): 16 | return self.name 17 | 18 | def to_json(self): 19 | return { 20 | "name": self.name, 21 | "campaign": self.campaign, 22 | "voice": self.voice, 23 | "recognize": self.recognize, 24 | "inside": self.inside, 25 | "growth": self.growth, 26 | "side": self.side, 27 | "yard": self.yard, 28 | "discussion": self.discussion, 29 | } 30 | -------------------------------------------------------------------------------- /write/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /write/views.py: -------------------------------------------------------------------------------- 1 | from django.http import JsonResponse, HttpResponse 2 | from django.views.decorators.csrf import csrf_exempt 3 | from .models import Row 4 | 5 | 6 | @csrf_exempt 7 | def write_row_from_json(request): 8 | if request.method == "POST": 9 | data = request.POST 10 | row = Row( 11 | name=data["name"], 12 | campaign=data["campaign"], 13 | voice=data["voice"], 14 | recognize=data["recognize"], 15 | inside=data["inside"], 16 | growth=data["growth"], 17 | side=data["side"], 18 | yard=data["yard"], 19 | discussion=data["discussion"], 20 | ) 21 | row.save() 22 | # Return JSON of row 23 | return JsonResponse(row.to_json()) 24 | else: 25 | return HttpResponse("Not allowed", status=405) 26 | --------------------------------------------------------------------------------