Project

Profile

Help

Issue #3813 ยป artifact_test.py

generate and save many Artifacts using bulk-create and individual save() - bmbouter, 07/09/2018 10:59 PM

 
import argparse
import hashlib
import time

if __name__ == '__main__':
import django
django.setup()

from pulpcore.plugin.models import Artifact


parser = argparse.ArgumentParser()
parser.add_argument('--num', default=1000)


def make_artifact(seed):
artifact_dict = {}
artifact_dict['size'] = 1
artifact_dict['md5'] = hashlib.md5(str(seed).encode('ascii')).hexdigest()
artifact_dict['sha1'] = hashlib.sha1(str(seed).encode('ascii')).hexdigest()
artifact_dict['sha224'] = hashlib.sha224(str(seed).encode('ascii')).hexdigest()
artifact_dict['sha256'] = hashlib.sha256(str(seed).encode('ascii')).hexdigest()
artifact_dict['sha384'] = hashlib.sha384(str(seed).encode('ascii')).hexdigest()
artifact_dict['sha512'] = hashlib.sha512(str(seed).encode('ascii')).hexdigest()
artifact_dict['sha224'] = hashlib.sha224(str(seed).encode('ascii')).hexdigest()
return Artifact(**artifact_dict)

def get_new_artifacts_and_reset(args):
Artifact.objects.all().delete()
artifacts = []
for i in range(int(args.num)):
artifacts.append(make_artifact(i))
return artifacts


def main():
args = parser.parse_args()
artifacts = get_new_artifacts_and_reset(args)
start = time.time()
for artifact in artifacts:
artifact.save()
end = time.time()

diff = end - start
print('{num} units: indidvidual save in seconds: {diff}'.format(diff=diff, num=args.num))

artifacts = get_new_artifacts_and_reset(args)
start = time.time()
Artifact.objects.bulk_create(artifacts)
end = time.time()

diff = end - start
print('{num} units: bulk save in seconds: {diff}'.format(diff=diff, num=args.num))


if __name__=='__main__':
main()

    (1-1/1)