Skip to content

Instantly share code, notes, and snippets.

@tseaver
Created April 14, 2021 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tseaver/4cf9a78a5dfbc492ef20e80fffdd2498 to your computer and use it in GitHub Desktop.
Save tseaver/4cf9a78a5dfbc492ef20e80fffdd2498 to your computer and use it in GitHub Desktop.
Compare Datastore 1.5.3 / 2.x unmarshalling entities.
import random
import string
import time
import uuid
from google.cloud.datastore.entity import Entity
from google.cloud.datastore.helpers import entity_from_protobuf
from google.cloud.datastore.helpers import entity_to_protobuf
from google.cloud.datastore.key import Key
try:
from google.cloud.datastore_v1.proto.entity_pb2 import Entity as EntityRaw
EntityPlus = None
except ImportError:
from google.cloud.datastore_v1.types.entity import Entity as EntityPlus
EntityRaw = EntityPlus.pb()
def generate_entities(count=10000):
for index in range(count):
key = Key("path", "to", str(uuid.uuid4()), project="foo-bar-123")
entity = Entity(key=key)
entity["str_val"] = random.sample(string.printable, 20)
entity["int_val"] = random.randint(0, 2**32)
yield entity
def main():
entities = list(generate_entities())
entity_pbs = [entity_to_protobuf(entity) for entity in entities]
if EntityPlus is not None:
queried = [EntityPlus(pb) for pb in entity_pbs]
else:
queried = entity_pbs
found = []
begin = time.time()
for pb in queried:
found.append(entity_from_protobuf(pb))
end = time.time()
print(f"Time: {end-begin}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment