Here's my section of code, logs, as well as an App Stats screenshot. I'm trying to optimize the speed at which a user can get information out of a query really quickly, but I'm getting bogged down by overhead. I'm using an F1
instance here with automatic_scaling
, if that helps any. It's really hard to have a responsive app, and I'm not sure what to do to fix performance here. If there's something else I should be doing here, though, I'd really appreciate it if someone could tell me. Thanks.
Code for post request:
@ndb.toplevel
def post(self):
start_time = time.time()
search_query = escape(self.request.get('search_query'))
lat = escape(self.request.get('lat'))
long = escape(self.request.get('long'))
query_id = escape(self.request.get('query_id'))
query_location = escape(self.request.get('location'))
logging.debug("Getting self vars took %s seconds"%(time.time()-start_time))
session_id = MD5.new(str(datetime.datetime.now())).hexdigest() # to change later
logging.debug("To get to MD5 gen took %s seconds"%(time.time()-start_time))
index = search.Index(name = INDEX_NAME)
query_string = search_query+" distance(location,geopoint("+lat+","+long+")) < 30000"
#TODO: this block will need to change if we already have a cursor
results = index.search(search.Query(
query_string=query_string,
options=search.QueryOptions(
limit=50,
cursor=search.Cursor(),
)))
cursor = results.cursor # if not None, then there are more results
logging.debug("To get to index search took %s seconds"%(time.time()-start_time))
search_results_pickle = jsonpickle.encode(results, unpicklable=False)
logging.debug("To get to pickling took %s seconds"%(time.time()-start_time))
key_names = []
for tmpResult in results:
key_names.append(tmpResult.field("location_id").value)
# put values into memcache
logging.debug("To get to database key gen took %s seconds"%(time.time()-start_time))
ndb.get_multi_async([ndb.Key('locationDatabase', key_name) for key_name in key_names])
logging.debug("To get to database async lookup took %s seconds"%(time.time()-start_time))
context = {
'session_id': session_id,
'lat': lat,
'long': long,
'location':query_location,
'query_id': query_id,
'search_results': search_results_pickle
}
logging.debug("To get to context generation took %s seconds"%(time.time()-start_time))
template = template_env.get_template('templates/map_no_jinja.html')
logging.debug("To get to template HTML gen took %s seconds"%(time.time()-start_time))
self.response.out.write(template.render(context))
logging.debug("To get to writing output HTML took %s seconds"%(time.time()-start_time))
return
Logs: Request took 1867ms according to App Engine logs
D 2014-07-22 14:29:24.112 Getting self vars took 0.00068998336792 seconds
D 2014-07-22 14:29:24.112 To get to MD5 gen took 0.0011100769043 seconds
D 2014-07-22 14:29:24.732 To get to index search took 0.620759963989 seconds
D 2014-07-22 14:29:24.749 To get to pickling took 0.637880086899 seconds
D 2014-07-22 14:29:24.750 To get to database key gen took 0.638570070267 seconds
D 2014-07-22 14:29:24.772 To get to database async lookup took 0.661170005798 seconds
D 2014-07-22 14:29:24.772 To get to context generation took 0.661360025406 seconds
D 2014-07-22 14:29:24.835 To get to template HTML gen took 0.723920106888 seconds
D 2014-07-22 14:29:24.836 To get to writing output HTML took 0.725300073624 seconds
That last logging.debug() statement is right before the return
statement.
App Stats:
EDIT:
Just for kicks I upped the instance class to F4
and with three instances running (and this was the only request my server was running at the time), I'm still seeing ~1 second overhead that's independent of RPC calls!
Corresponding logs (1893 ms in the header):
D 2014-07-22 15:29:46.261 Getting self vars took 0.000779867172241 seconds
D 2014-07-22 15:29:46.261 To get to MD5 gen took 0.00116991996765 seconds
D 2014-07-22 15:29:46.929 To get to index search took 0.668219804764 seconds
D 2014-07-22 15:29:46.949 To get to pickling took 0.689079999924 seconds
D 2014-07-22 15:29:46.950 To get to database key gen took 0.690039873123 seconds
D 2014-07-22 15:29:46.961 To get to database async lookup took 0.701029777527 seconds
D 2014-07-22 15:29:46.962 To get to context generation took 0.701309919357 seconds
D 2014-07-22 15:29:46.962 To get to template HTML gen took 0.70161986351 seconds
D 2014-07-22 15:29:46.963 To get to writing output HTML took 0.702219963074 seconds