ryan
ryan

Reputation: 2331

How do I calculate the size of an entity stored in Google App Engine?

Is there an easy way to calculate the size of an entity stored in App Engine? I would like to know how close a particular entity is to hitting the 1 MB upper limit on entity size.

Upvotes: 13

Views: 2338

Answers (1)

David Underhill
David Underhill

Reputation: 16243

App engine stores each entity as a protobuf. You can use the db.model_to_protobuf() function described here to manually convert your entity into a protobuf and then use the standard len() method to determine its size in bytes.

Example usage:

from google.appengine.ext import db
sz_in_bytes = len(db.model_to_protobuf(some_entity).Encode())

Update for ndb

Kekito points out in the comments below that for ndb entities a different approach is needed (Thanks Kekito!):

len(some_entity._to_pb().Encode())

Upvotes: 20

Related Questions