Scudder: Hi. I'm Jeff Scudder.
In this video, we'll be looking at a couple of techniques
to improve the efficiency of your App Engine applications.
The first thing we'll be looking at is profiling,
which is a way to view the execution path
taken through your app.
The second thing we'll look at is memcache,
which is a way to speed up the retrieval
of commonly used data.
To view these two techniques,
we'll be looking at the App Gallery
sample application.
Let's take a look.
Now we're looking at the App Gallery application.
This app shows featured applications,
editors' picks,
and the most recently added applications.
This version of the code doesn't include
any profiling code.
So we're going to show how to add that
to get an idea of which pieces of the application
are being exercised when we view the pages.
To learn how to do profiling,
we'll look in the Frequently Asked Questions section
on code.google.com/appengine.
We show two ways to add profiling to your application.
We're going to go with the second one,
which adds the profiling data to the logs.
Now we see the main function of the App Gallery's code.
We're going to add our profiling function here.
To add profiling, we've replaced the main function
with this profile_main.
This captures profiling data and pushes it into the logs.
I've uploaded a new version of the App Gallery application,
which now includes profiling.
You can see that the application
looks exactly the same as before.
But now, if we look on the admin console,
we can see profiling data in the logs.
Here in the dashboard,
I'm viewing version 4 of the App Gallery,
which includes profiling data.
Let's take a look at the logs.
Here we see the profiling data from the logs
that show the path that's taken
through our application's code.
Right around here,
we see some calls to entity.pb,
which is responsible for working with the, uh, data models.
Quite a bit of time is spent in our application
working with these data models from the datastore.
And we can improve some of this by adding memcache.
Let's take a look at how this version of the application
is fetching data from the datastore.
I'm pulling up the main page of the application
and viewing the get_or_fetch_ main_page method.
You can see here that we're querying
for the top apps, recent apps,
and the featured application,
and then plugging these values into our template.
Now, these same queries are repeated
each time we view the main page.
The results don't change all that often.
So querying every time is a little bit expensive.
Since the results are always the same,
we could see a vast improvement by adding memcache,
which stores recent results in memory.
Let's take a look at adding that now.
I can take this same main page class
and add in memcache.
In the get_or_fetch_main_page method,
I'm first calling a memcache.get,
which tries to fetch recent results from memcache.
If the results are in memcache,
I'll return them immediately.
Otherwise, I'll perform the datastore queries
as I did before.
After I construct the values to put into the template,
I add them to the cache so that next time I make a call,
I can use the cache values
instead of calling to the datastore.
After making these changes,
I've uploaded version 5 of the application.
Once again, you can see that the application
looks exactly the same.
All of the changes are under the hood, so to speak.
If we look at version 5 of the application
in the dashboard,
we can see that there's been an improvement
in performance of some of the commonly fetched pages.
For example, if we compare the average CPU
to previous versions, this number has decreased.
If we look at the logs,
we can also see that some data is coming out of memcache
instead of the datastore.
Here's a log entry for viewing the main page.
You can see that calls are now being made to memcache
instead of the datastore.
This should greatly improve the efficiency
of our application.
Now you've seen how to add profiling to your application
and how to use memcache.
These are just two of the many ways
to speed up your application,
and we'll be covering more in the future.
Stay tuned.