xenua
2 years ago
7 changed files with 134 additions and 35 deletions
@ -0,0 +1,19 @@
@@ -0,0 +1,19 @@
|
||||
from django.http import Http404 |
||||
from django.utils.deprecation import MiddlewareMixin |
||||
|
||||
from leftists.models import Domain |
||||
|
||||
|
||||
class DomainAutoCreateMiddleware(MiddlewareMixin): |
||||
def __init__(self, get_response): |
||||
super().__init__(get_response) |
||||
self.cache = set() |
||||
|
||||
def process_request(self, r): |
||||
if (host := r.get_host()) in self.cache: |
||||
return |
||||
try: |
||||
Domain.get_from_request(r) |
||||
self.cache.add(host) |
||||
except Domain.DoesNotExist: |
||||
Domain.objects.create(fqdn=host.lower()) |
@ -1,4 +1,2 @@
@@ -1,4 +1,2 @@
|
||||
django |
||||
django_scopes |
||||
git+https://git.xenua.me/xenua/django_scopedsites.git@main#egg=django_scopedsites |
||||
git+https://git.xenua.me/xenua/xenua_tools.git@main#egg=xenua_tools |
@ -0,0 +1,39 @@
@@ -0,0 +1,39 @@
|
||||
import asyncio |
||||
import sys |
||||
import time |
||||
|
||||
import aiohttp |
||||
import uvloop |
||||
|
||||
|
||||
async def do_req(sesh, url): |
||||
async with sesh.get(url, allow_redirects=False) as resp: |
||||
assert resp.status == 302 |
||||
|
||||
|
||||
async def main(): |
||||
if len(sys.argv) < 2: |
||||
exit("usage: python performance.py <url>") |
||||
url = sys.argv[1] |
||||
async with aiohttp.ClientSession() as sesh: |
||||
await do_req(sesh, url) |
||||
|
||||
await asyncio.sleep(1) |
||||
|
||||
tasks = [] |
||||
for i in range(10000): |
||||
tasks.append(do_req(sesh, url)) |
||||
|
||||
# aaand liftoff! |
||||
before = time.time() |
||||
await asyncio.gather(*tasks) |
||||
after = time.time() |
||||
|
||||
return after - before |
||||
|
||||
uvloop.install() |
||||
t = asyncio.run(main()) |
||||
|
||||
print(f"completed 10k requests in {t:.4f} seconds") |
||||
if t > 10: |
||||
print('if that was a redirect endpoint something seems off here. go optimize performance') |
Loading…
Reference in new issue