`HasAPIKey` adds huge performance (speed) hit
See original GitHub issueI’m seeing a huge performance hit when adding the HasAPIKey
permission class. I’ve isolated it down to toggling the following setting:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [],
# 'DEFAULT_PERMISSION_CLASSES': ['rest_framework_api_key.permissions.HasAPIKey'],
}
Which gives the following results:
# Without API Keys
Testing Resource #1...
Done in 0.09s
Testing Resource #2...
Avg after 10 attempts: 0.35s
Testing Resource #3...
Avg after 10 attempts: 0.11s
# With HasAPIKey
Testing Resource #1...
Done in 0.55s
Testing Resource #2...
Avg after 10 attempts: 0.83s
Testing Resource #3...
Avg after 10 attempts: 0.46s
I wasn’t able to find any similar tickets. Has anyone seen this behaviour before?
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
djangorestframework-api-key 2.2.0 - PythonFix.com
... returns strings that are more than 100 chars; this project cannot support ModelViewSet? HasAPIKey adds huge performance (speed) hit.
Read more >Web API performance: profiling Django REST framework
As this article will argue, the biggest performance gains for Web APIs can be made not by code tweaking, but by proper caching...
Read more >User Guide - Django REST Framework API Key - GitHub Pages
The HasAPIKey permission class protects a view behind API key authorization. ... When it is installed, djangorestframework-api-key adds an "API Key ...
Read more >Database Performance Tips with Django - YouTube
Most performance problems in web applications come down to one thing: the database. In this webinar, veteran #Python developer Andrew ...
Read more >Improve Serialization Performance in Django Rest Framework
When a developer chooses Python, Django, or Django Rest Framework, it's usually not because of its blazing fast performance.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So, one for improving performance of key verification, you can tweak PASSWORD_HASHERS for using different hash method.
https://security.stackexchange.com/questions/246062/pbkdf2-usage-will-slow-rest-api-down
https://docs.djangoproject.com/en/4.0/ref/settings/#std:setting-PASSWORD_HASHERS
I did some basic benchmarking on my end using the
test_project
in this repo, slightly edited to add a few routes:And running this script:
Results (in seconds):
/api/public/
(no API key checking): 0.005s (std=0.003) (very low because of being onlocalhost
)/api/protected/
:/api/protected/object/
(Argon2): 0.316 (std=0.039)(Reference: Result w/o API key permissions: 0.005 (std=0.003) (on
localhost
).)So:
PASSWORD_HASHERS
), as @hyzyla suggested..has_object_permissions()
implementation does result in duplicate work, which would explain larger performance hit on detail API views. This hints us to move forward on #150.