73 lines
2.5 KiB
Python
73 lines
2.5 KiB
Python
from __future__ import unicode_literals
|
|
|
|
from django.core.urlresolvers import reverse
|
|
from django.test import TestCase
|
|
from api.models import Inventory, Group
|
|
from django.contrib.auth.models import User
|
|
import time
|
|
from django.db import transaction
|
|
|
|
|
|
class ProductivityTest(TestCase):
|
|
fixtures = ["tests/selenium/user.json"]
|
|
|
|
def test_massive_creation_base(self):
|
|
start = time.time()
|
|
user = User.objects.all()[0]
|
|
inventory = Inventory(user=user, inventory='inv')
|
|
inventory.save()
|
|
for i in range(20000):
|
|
group = Group(inventory=inventory, group="group_{}".format(i))
|
|
group.save()
|
|
|
|
time_is = time.time() - start
|
|
print("base: ", time_is)
|
|
|
|
# @transaction.atomic
|
|
def test_transaction(self):
|
|
start = time.time()
|
|
user = User.objects.all()[0]
|
|
inventory = Inventory(user=user, inventory='inv')
|
|
inventory.save()
|
|
with transaction.atomic():
|
|
for i in range(20000):
|
|
group = Group(inventory=inventory, group="group_{}".format(i))
|
|
group.clean_fields(exclude=['id', 'inventory'])
|
|
# group.setattr(f.attname, f.clean(raw_value, self))
|
|
# for i in group._meta.fields:
|
|
# print(i.name)
|
|
group.save()
|
|
|
|
time_is = time.time() - start
|
|
print("transaction_decorator: ", time_is)
|
|
|
|
# @transaction.atomic
|
|
def test_bulk(self):
|
|
start = time.time()
|
|
user = User.objects.all()[0]
|
|
inventory = Inventory(user=user, inventory='inv')
|
|
inventory.save()
|
|
group_list = []
|
|
# Group.objects.bulk_create([
|
|
# Group(inventory=inventory, group="group_{}".format(group)) for group in range(20000)
|
|
# if Group(inventory=inventory, group="group_{}".format(group)).clean_fields(exclude=['id', 'inventory']) is None
|
|
# ])
|
|
for i in range(20000):
|
|
group = Group(inventory=inventory, group="group_{}".format(i))
|
|
group.clean_fields(exclude=['id', 'inventory'])
|
|
# group.save()
|
|
# group = Group.objects.get(inventory=inventory, group="group_{}".format(i))
|
|
|
|
group_list.append(group)
|
|
Group.objects.bulk_create(group_list)
|
|
# for i in range(20000):
|
|
# group = Group.objects.get(inventory=inventory, group="group_{}".format(i))
|
|
# group.save()
|
|
|
|
time_is = time.time() - start
|
|
print("bulk_decorator: ", time_is)
|
|
|
|
|
|
|
|
|