from django.test import RequestFactory
import django.urls
from django.contrib.auth.models import AnonymousUser 
from django.http import HttpRequest
import cProfile

# inspiré de https://enix.io/fr/blog/django-performance-profiler/

# La fameuse requête, séparée en URI et Query String.
uri = "/"
qstring = "?tags=🎵 concert"

# On construit un objet "request" ..."
request_factory = RequestFactory()
request = request_factory.get(uri + qstring)
request.user = AnonymousUser

# Puis on fait appel au routeur de Django pour trouver
# la vue censée traiter la requête ...
resolvermatch = django.urls.resolve(uri)

resolvermatch.func(request, *resolvermatch.args, **resolvermatch.kwargs)

cProfile.runctx(
    "for i in range(10): resolvermatch.func(request, *resolvermatch.args, **resolvermatch.kwargs)",
    None,
    locals(),
    sort="tottime",
    filename='logs'
)

# puis visualiser avec 
# snakeviz src/logs 
