服务器响应时间比预期的要长得多



我有一个Django应用程序,它目前运行在我的本地主机上。有些页面需要更长的时间,因此出于调试原因,我用以下简单代码计算了在一个基于类的视图中花费的时间:

class DiscoverView(LoginRequiredMixin, ListView):
model = MyModel
template_name = "app1/discover.html"
context_object_name = 'influencers'  # not necessary, default is object_list
def get_context_data(self, **kwargs):
start_time = time.time()
# ...
# Some tasks which I tought they are the reasons of long response time
# ...
print(time.time() - start_time, "seconds took to complete...")
return context

在实验之后,我看到这个视图需要0.01秒才能完成,而页面加载需要大约5秒。

为了进一步调试,我深入研究了Chrome开发工具。在网络选项卡中,页面的"完成"时间为5.86秒。我还运行了Lighthouse测试,该测试表明服务器的初始响应时间约为2秒。

我不明白为什么这0.01秒变成了2秒,我还使用了django调试工具栏来检查数据库查询时间,这些时间没有那么长。我的应用程序也在生产中(Heroku(,当然那里的加载时间要糟糕得多,但我觉得我需要首先在本地解决这个问题。

我感谢任何形式的帮助或调试建议,如果您需要有关应用程序/系统的更多信息,请告诉我。

编辑:这是get_context_data():的内部

def get_context_data(self, **kwargs):
start_time = time.time()
context = super().get_context_data(**kwargs)
platform = self.kwargs['platform']
page_no = self.request.GET.get('page') # make it int to multiply when slicing
if page_no is not None: page_no = int(page_no)
ITEMS_PER_PAGE = 10
parents = None

if platform == 'instagram':
# Will implement this later
pass
elif platform == 'tiktok':
tiktok_influencers = TiktokInfluencer.objects.all()
context['platform_counter'] = tiktok_influencers.count()
parents = InfluencerParent.objects.filter(tiktok__id__in = tiktok_influencers)
elif platform == 'youtube':
youtube_influencers = YoutubeInfluencer.objects.all()
context['platform_counter'] = youtube_influencers.count()
parents = InfluencerParent.objects.filter(youtube__id__in = youtube_influencers)
elif platform == 'twitch':
twitch_influencers = TwitchInfluencer.objects.all()
context['platform_counter'] = twitch_influencers.count()
parents = InfluencerParent.objects.filter(twitch__id__in = twitch_influencers)
elif platform == 'clubhouse':
clubhouse_influencers = ClubhouseInfluencer.objects.all()
context['platform_counter'] = clubhouse_influencers.count()
parents = InfluencerParent.objects.filter(clubhouse__id__in = clubhouse_influencers)
else:
# unknown platform, redirect to 404
pass

# Pagination stuff
if page_no == None or page_no == 1:
parents = parents[:ITEMS_PER_PAGE]
else:
parents = parents[(page_no-1) * ITEMS_PER_PAGE : (page_no) * ITEMS_PER_PAGE]
context['influencers'] = parents
context['platform'] = platform
print(time.time() - start_time, "seconds took DiscoverView to complete...")
return context

编辑2:我需要进一步的帮助,所以我正在添加模型和模板信息:

# models.py
class InfluencerParent(models.Model):
'''
Parent class which points to influencers' IG, YT & other social media platform accounts
'''

def __str__(self):
if self.instagram.first():
return self.instagram.first().fullname
else:
return "None"
class InstagramInfluencer(models.Model):
# some fields...
influencer_parent = models.ForeignKey(InfluencerParent, on_delete=models.SET_NULL, related_name='instagram', default=None, blank=True, null=True)
class YoutubeInfluencer(models.Model):
# some fields...
influencer_parent = models.ForeignKey(InfluencerParent, on_delete=models.SET_NULL, related_name='instagram', default=None, blank=True, null=True)
class TiktokInfluencer(models.Model):
# some fields...
influencer_parent = models.ForeignKey(InfluencerParent, on_delete=models.SET_NULL, related_name='instagram', default=None, blank=True, null=True)
class TwitchInfluencer(models.Model):
# some fields...
influencer_parent = models.ForeignKey(InfluencerParent, on_delete=models.SET_NULL, related_name='instagram', default=None, blank=True, null=True)
class ClubhouseInfluencer(models.Model):
# some fields...
influencer_parent = models.ForeignKey(InfluencerParent, on_delete=models.SET_NULL, related_name='instagram', default=None, blank=True, null=True)

模板样本:

# template (not the all for the sake of clarity)
<!-- discover.html
A single template to list all influencers (IG, YT, Tiktok...)
Takes platform param from URL
In view we filtered parents according to the platform
For example if platform param is 'tiktok' I use this updated version:   
parents = InfluencerParent.objects.filter(tiktok__isnull=False).prefetch_related('instagram', 'youtube', 'tiktok', 'twitch', 'clubhouse')
There parents influencers are who have tiktok accounts, in template I show all the social media cards in a tabbed design
So I need to send parent and acces parent.instagram.first.username
-->
{% for influencer in influencers %}
<div class="instagram" {% influencer.instagram.first == None %} disabled {% endif %}>
{{influencer.instagram.first.username}}
</div>
<div class="tiktok" {% influencer.tiktok.first == None %} disabled {% endif %}>
{{influencer.tiktok.first.username}}
</div>
<!-- 
. 
. 
OTHER SOCIAL MEDDIA ACCOUNTS OF THAT PARENT
. 
. 
-->
<div class="clubhouse" {% influencer.clubhouse.first == None %} disabled {% endif %}>
{{influencer.clubhouse.first.username}}
</div>

以下是我如何使用prefetch_related(select_related给出错误(:

elif platform == 'tiktok':
context['platform_counter'] = TiktokInfluencer.objects.count()
parents = InfluencerParent.objects.filter(tiktok__isnull=False).prefetch_related('instagram', 'youtube', 'tiktok', 'twitch', 'clubhouse')
# This is how I use prefetch_lated(), I need to send parent objecjts and their all (if exists) platform related influencer objects
# I acces Parent's all existing reverse foreign key fields in one template, for example:
# {{parent.influencer.username}}
# {{parent.youtube.follower_count}}
# {{parent.tiktok.fullname}}
# ...
# The row below gives error: 'Invalid field name(s) given in select_related: 'tiktok'. Choices are: (none)'
# Even tho I need  to give all social platforms as params (select_related('instagram', 'youtube', 'tiktok', 'twitch', 'clubhouse'))
parents = InfluencerParent.objects.select_related('tiktok')

https://docs.djangoproject.com/en/3.2/ref/models/querysets/为了帮助建立基础,select_related适用于具有ForeignKey关系的模型,通常prefetc_related>适用于具有M2M的模型。在这里,我将提供一个";简单的";实现预取相关的方法。请记住,连接到数据库的调用不仅在views.py中,而且在您的模板中,访问属性可能会对数据库造成额外的影响(请参阅上面的链接以获得进一步的解释,即博客作者家乡(。因此,为了减少我们访问数据库的次数,我建议实现这样的操作。现在,如果没有看到模型、模板和实际的SQL查询,这将是我的最佳猜测。


...
if platform == 'instagram':
# Will implement this later
pass
elif platform == 'tiktok':
context['platform_counter'] = TiktokInfluencer.objects.count()
parents = InfluencerParent.objects.prefetch_related('tiktok')
elif platform == 'youtube':
context['platform_counter'] = YoutubeInfluencer.objects.count()
parents = InfluencerParent.objects.prefetch_related('youtube')
... repeat this pattern for others
else:

我认为只要添加我所做的编辑就会极大地改变查询量。

如果你想看看它会如何改变。从你的观点出发,在外壳中进行一些测试

from django.db import reset_queries
from django.db import connection
>>> parents = list(InfluencerParent.objects.prefetch_related('tiktok'))
>>> connection.queries
>>> reset_queries()
>>> parents = list(InfluencerParent.objects.select_related('tiktok')) 
>>> connections.queries
# If both work, then your using a Forgeign Key but more importantly
# notice how many SQL queries are made. If using select_related breaks 
# its because your using an M2M. But again, the importants is getting that list of "connections" as low as possible

现在,为了进一步完善您的代码,我将查看Prefetch,因为您可以使缓存查询与以前实现的查询类似。

from django.db.models import Prefetch
>>> qs = MyModel.objects.filter(some_lookup)
>>> results = MyDesiredResultObjects.objects.prefetch_related(Prefetch('some_attr'), queryset=qs, to_attr='my_new_result')
>>> my_new_result

==========================
由于您提供了
select_related模型

...
elif platform == 'tiktok':
context['platform_counter'] = TiktokInfluencer.objects.count()
parents = InfluencerParent.objects.select_related('tiktok')

最新更新