Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is template expansion and how can we reduce it if our google app engine application?

I am making my first app on google app engine. Before i was just checking the correct results for my app. But then my app started responding really late. Then i went through google app engine docs and now started using appstats. I am really new to that. I watched a video about it and got some stuff but still i am little bit confused. Following is the graph for one login request in my app:

enter image description here

and here following is the code for my LoginCheckServlet:

 public class LoginCheckServlet extends HttpServlet {
     @SuppressWarnings("unchecked")
     public void doPost(HttpServletRequest req, HttpServletResponse resp)
     throws IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession(true);
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try
        {
            List<User> result = null;
            Query query = pm.newQuery(User.class);
            query.setFilter("email == emailParam");
            query.declareParameters("String emailParam");
            result = (List<User>) query.execute(req.getParameter("email"));
            if(result.size() == 0){
                out.println(0);
            }else{
                String pwd = req.getParameter("password");
                String userPwd = result.get(0).getPassword();
                if(pwd.equals(userPwd)){
                    result.get(0).setActive(true);
                    session.setAttribute("email", result.get(0).getEmail());
                    session.setAttribute("name", result.get(0).getName());
                    out.println("1");
                }else{
                    out.println("2");
                }
            }
         }catch(Exception ex)
         {
            out.println(ex);
         }
         finally
         {
            pm.close();
         }
     }
 }

according to google app engine a query takes most of the time and it is around 50-100 ms. But in graph total time taken is 15167 ms. And the time in which my app is doing nothing (template expansion) called by the guy in presentation is almost 140000ms. I do not understand that what is that template expansion and why my app is taking a big amount of that? How can i reduce it? May be its a basic question but i am very new to this and i searched but could not find something helping. Thanks in advance.

like image 277
Piscean Avatar asked Nov 13 '22 10:11

Piscean


1 Answers

As @allyourcode menitoend template are used for generating HTML. Some template engines that are built in Google app engine are Django , jinja.

First of all I would like to let you know storing passwords inclear is not a good Idea.Make sure they are hashed.If your website goes commercial and it gets hacked oyur customers are gonna be pissed off.Consider using hashing libraries.

Secondly to reduce the queries time go through this concept called memcache.This will drastically reduce your query time.

Here is simple example to use memcache:- from google.appengine.ext import db from google.appengine.api import memcache

def top_arts(update = False):
  key = 'top'

  #Getting arts from memcache
  arts = memcache.get(key)

  #Check if key is defined in memcache
  #or an update has been invoked
  if update or not arts:
    #Querying the Google Data store using GQL
    arts = db.GqlQuery('SELECT * from Art ORDER BY created DESC LIMIT 10')
    memcache.set(key, arts)
  return arts

You can use the same function for reading from memcache and then writing data into memcache

Eg:for reading from memcache:- arts = top_arts()

when writing into database:-

#write your entry in database
<some database code>
#update memcache with this new entry
top_arts(update=True)
like image 66
Axesh Ajmera Avatar answered Jun 06 '23 19:06

Axesh Ajmera