目前我的应用程序缓存模型在内存缓存是这样的:执行AppEngine模型Memcaching的最佳方式是什么?
memcache.set("somekey", aModel)
但尼克斯后在http://blog.notdot.net/2009/9/Efficient-model-memcaching表明,首先将其转换为protobuffers是很多更有效。但经过一些测试后,我发现它的尺寸确实比较小,但实际上比较慢(〜10%)。
其他人是否有相同的经历或我做错了什么?
测试结果:http://1.latest.sofatest.appspot.com/?times=1000
import pickle
import time
import uuid
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import util
from google.appengine.datastore import entity_pb
from google.appengine.api import memcache
class Person(db.Model):
name = db.StringProperty()
times = 10000
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
m = Person(name='Koen Bok')
t1 = time.time()
for i in xrange(int(self.request.get('times', 1))):
key = uuid.uuid4().hex
memcache.set(key, m)
r = memcache.get(key)
self.response.out.write('Pickle took: %.2f' % (time.time() - t1))
t1 = time.time()
for i in xrange(int(self.request.get('times', 1))):
key = uuid.uuid4().hex
memcache.set(key, db.model_to_protobuf(m).Encode())
r = db.model_from_protobuf(entity_pb.EntityProto(memcache.get(key)))
self.response.out.write('Proto took: %.2f' % (time.time() - t1))
def main():
application = webapp.WSGIApplication([('/', MainHandler)], debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
我刚刚尝试过真正大型和复杂的模型,但结果大致相同。 – 2010-02-19 21:36:34
也许GAE上有http://docs.python.org/library/timeit.html?这应该显示更准确的结果,但仍然 - 在阅读您链接到的博客条目后,我会预期protobuffers的性能与pickle之间的数量级差异 - 并且这应该由time.time()无论如何赶上。 – 2010-02-21 23:34:36
我是使用java appengine,所以我懒得测试这个理论 - pickle()在某个地方缓存幕后结果,而to_protobuf不是?基于这篇文章,我不确定我会期望速度会有一个完整的数量级增长,因为即使使用protobuf版本,pickle仍然被称为。尽管如此,使用的空间肯定会大大缩小。 – 2010-02-22 02:45:28