Skip to content

Instantly share code, notes, and snippets.

@mikebridge
Last active April 15, 2020 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikebridge/dcbd7a679728a28eaf7ca8170d75c73a to your computer and use it in GitHub Desktop.
Save mikebridge/dcbd7a679728a28eaf7ca8170d75c73a to your computer and use it in GitHub Desktop.
django-simple-history test
from decimal import Decimal
from functools import reduce
from django.db import connection, reset_queries
from apps.accounts.models import User
me = User.objects.get(email='test@example.org')
iterations = 1000
def test_save(iterations1 = 1000):
reset_queries()
for i in range(iterations1):
me.first_name="Test {}".format(i)
me.save()
total_time = reduce(lambda x, y: x+y, [Decimal(x["time"]) for x in connection.queries])
reset_queries()
return total_time / iterations1
print (f"current revisions: { me.history.count()}")
average_query_time_with_revisions = test_save(iterations)
print(f"saving with revisions took {average_query_time_with_revisions}")
me.skip_history_when_saving = True
print (f"current revisions: { me.history.count()}")
average_query_time_without_revisions = test_save(iterations)
print(f"saving without revisions took {average_query_time_without_revisions}")
print (f"current revisions: { me.history.count()}")
print((average_query_time_without_revisions - average_query_time_with_revisions) / average_query_time_without_revisions)
del me.skip_history_when_saving
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment