Google App Engine Application Code Optimization

The google app engine tells me to optimize this code. Anyone have any ideas what I can do?

def index(request):
    user = users.get_current_user()
    return base.views.render('XXX.html', 
                 dict(profiles=Profile.gql("").fetch(limit=100), user=user))

And later in the template, I:

{% for profile in profiles %}
  <a href="/profile/{{profile.user.email}}/"><img src="{{profile.gravatarUrl}}"></a>
  <a href="/profile/{{profile.user.email}}/">{{ profile.user.nickname }}</a>
  <br/>{{ profile.shortDisplay }}

Where the following methods are used:

def shortDisplay(self):
    return "%s/day; %s/week; %s days" % (self.maxPerDay, self.maxPerWeek, self.days)

def gravatarUrl(self):
    email = self.user.email().lower()
    default = "..."
    gravatar_url = "http://www.gravatar.com/avatar.php?"
    gravatar_url += urllib.urlencode({'gravatar_id':hashlib.md5(email).hexdigest(), 
        'default':default, 'size':"64"})
    return gravatar_url
+3
source share
4 answers

High CPU usage will be associated with receiving 100 objects per request. Here you have several options:

  • Using Profile.all (). fetch (100) will always be a little faster and easier to read.
  • Remove any extraneous properties from the profile model. There are significant deserialization essential service objects based on properties.
  • Show fewer users per page.
  • memcache memcache, . , , , .
+6

, md5 . gravatar -.

+3
+1

, .

, , , CPU , .

10 , .

, x.xx , , . .

I found that many Django templates do not take up many CPU applications (50-100 Mcycle). If all fields for the template are precomputed.

0
source

Source: https://habr.com/ru/post/1699701/


All Articles