Identifier generating command
* adds a test command for calculating the collision rate for e.g. an intervention (Not important for production or anything)
This commit is contained in:
parent
2c3f6bbcd1
commit
5137572127
51
konova/management/commands/test_identifier_generating.py
Normal file
51
konova/management/commands/test_identifier_generating.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
"""
|
||||||
|
Author: Michel Peltriaux
|
||||||
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
|
Created on: 19.08.21
|
||||||
|
|
||||||
|
"""
|
||||||
|
from django.core.management import BaseCommand
|
||||||
|
|
||||||
|
from intervention.models import Intervention
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = "Performs test on collisions using the identifier generation"
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
identifiers = {}
|
||||||
|
max_iterations = 100000
|
||||||
|
try:
|
||||||
|
collisions = 0
|
||||||
|
len_ids = len(identifiers)
|
||||||
|
while len_ids < max_iterations:
|
||||||
|
tmp_intervention = Intervention()
|
||||||
|
_id = tmp_intervention._generate_new_identifier()
|
||||||
|
len_ids = len(identifiers)
|
||||||
|
if _id not in identifiers:
|
||||||
|
if len_ids % (max_iterations/5) == 0:
|
||||||
|
print(len_ids)
|
||||||
|
identifiers[_id] = None
|
||||||
|
else:
|
||||||
|
collisions += 1
|
||||||
|
print("+++ Collision after {} identifiers +++".format(len_ids))
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
self._break_line()
|
||||||
|
exit(-1)
|
||||||
|
print(
|
||||||
|
"\n{} collisions in {} identifiers; Collision rate {}%".format(
|
||||||
|
collisions,
|
||||||
|
len_ids,
|
||||||
|
(collisions / len_ids)*100,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def _break_line(self):
|
||||||
|
""" Simply prints a line break
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
self.stdout.write("\n")
|
Loading…
Reference in New Issue
Block a user