简体   繁体   English

Django生成的测试数据非常慢

[英]Django generating test data incredibly slow

Django 1.8, Postgres 8.4.20 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. 我正在尝试在DB中准备一些性能测试数据 - 生成10万个Django用户以及其他模型实例。 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. 在具有1核和1 Gb RAM的VPS上,花费1小时仅生成4000个具有相应相关条目的用户。

The consumed CPU is about 10%, memory 200mb and the load average floats around 1.00. 消耗的CPU大约为10%,内存为200mb,负载平均值大约为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. 正如评论中建议的那样,我使用了bulk_create并且并行启动了我的脚本10次。 This increased the speed to about 100 User entries per second, which was fine in my case. 这将速度提高到每秒约100个用户条目,这在我的情况下很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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