-
-
Save tseaver/4cf9a78a5dfbc492ef20e80fffdd2498 to your computer and use it in GitHub Desktop.
Compare Datastore 1.5.3 / 2.x unmarshalling entities.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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