简体   繁体   中英

Django generating test data incredibly slow

Django 1.8, Postgres 8.4.20

I'm trying to prepare some performance test data in DB - generating 100 000 Django Users as well as other model instances. Here is my code:

for n in range(100000):
    # Generate User with one related Profile
    user = User.objects.create(username=str(random.random()))
    UserProfile.objects.create(custom_id=str(random.random()), user=user)

    # For each User generate a Journey with 10 related Visits
    journey = Journey.objects.create(user_id=user.id)
    for i in range(10):
        journey.visit_set.add(Visit(custom_id=str(random.random())))

    # Also for each User generate a Progress with 100 related Challenges
    progress = Progress.objects.create(user_id=user.id)
    for j in range(100):
        progress.challenge_set.add(Challenge(custom_id=str(random.random())))

It works as expected, the only issue is that it is incredibly slow. On a VPS with 1 core and 1 Gb RAM it took 1 hour to generate just 4000 users with corresponding related entries.

The consumed CPU is about 10%, memory 200mb and the load average floats around 1.00.

How can I speed this up?

As it was suggested in comments, I used bulk_create and launched my script 10 times in parallel. This increased the speed to about 100 User entries per second, which was fine in my case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM