App Engine:1秒的开销来自哪里?

时间:2014-07-22 18:35:41

标签: google-app-engine

这是我的代码部分,日志以及App Stats截图。我正在尝试优化用户可以非常快速地从查询中获取信息的速度,但我却因开销而陷入困境。我在这里使用F1实例automatic_scaling,如果有帮助的话。有一个快速响应的应用程序真的很难,我不知道如何在这里修复性能。如果还有别的东西我应该在这里做,但是,如果有人能告诉我,我真的很感激。谢谢。

发布请求的代码:

@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

日志:根据App Engine日志,请求耗时1867毫秒

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

最后一个logging.debug()语句就在return语句之前。

App Stats: App Stats screenshot

修改

只是为了踢,我将实例类增加到F4并且运行了三个实例(这是我的服务器当时运行的唯一请求),我仍然看到~1秒的开销,它独立于RPC调用!

enter image description here

对应的日志(标题中为1893 ms):

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

1 个答案:

答案 0 :(得分:0)

看起来主要的延迟是在memcache.get和memcache.set之间。看起来这就是您使用jsonpickle搜索搜索结果的地方。这可能是花了这么长时间。您从搜索结果中获得了多少数据?尝试隔离pickle操作,看看需要多长时间。