Skip to content

Instantly share code, notes, and snippets.

@edeca
Last active April 23, 2020 20:57
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 edeca/214d7a7c51f84b9c2dc2a2c2656c191e to your computer and use it in GitHub Desktop.
Save edeca/214d7a7c51f84b9c2dc2a2c2656c191e to your computer and use it in GitHub Desktop.
A simple Datastore PoC that uses 1MiB extra per request
"""
Tested with library versions:
google-api-core==1.16.0
google-auth==1.13.1
google-cloud-core==1.3.0
google-cloud-datastore==1.11.0
grpcio==1.28.1
"""
import os
import psutil
import humanize
from google.cloud import datastore
from google.oauth2 import service_account
def test_datastore(entity_type: str) -> list:
creds = service_account.Credentials.from_service_account_file("/path/to/creds")
client = datastore.Client(credentials=creds, project="my-project")
query = client.query(kind=entity_type, namespace="my-namespace")
query.keys_only()
# Note that memory increases even if there are zero results
for result in query.fetch(1):
print(f"[+] Got a result: {result}")
def main():
process = psutil.Process(os.getpid())
for n in range(100):
print("[+] Iteration {}, memory usage {} bytes".format(n, humanize.naturalsize(process.memory_info().rss, binary=True)))
test_datastore("my-entity-type")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment