目前我的應用程序緩存模型在內存緩存是這樣的:執行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