Skip to content

Instantly share code, notes, and snippets.

@jeremydw
Last active December 26, 2015 19:29
Show Gist options
  • Save jeremydw/7201456 to your computer and use it in GitHub Desktop.
Save jeremydw/7201456 to your computer and use it in GitHub Desktop.
A simple test for Google's Cloud Datastore to see if property name length affects entity size on disk.
application: jeremydw-hrd
version: property-name-length-test
api_version: 1
runtime: python27
threadsafe: true
handlers:
- url: /.*
script: main.application
import logging
import time
import webapp2
from google.appengine.ext import ndb
from google.appengine.ext.db import stats
class PersistentClass1(ndb.Model):
super_very_long_property_name = ndb.StringProperty()
class PersistentClass2(ndb.Model):
l = ndb.StringProperty()
class TestHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
# NOTE: You'll have to visit the development server admin console and press the "Generate stats"
# button from the "Datastore Stats" page in order for these statistics to be generated.
vals = stats.KindPropertyNameStat.all()
for val in vals:
self.response.out.write('name: {}, bytes: {}\n'.format(val.key().name(), val.bytes))
bigger = stats.KindPropertyNameStat.get_by_key_name('super_very_long_property_name_PersistentClass1')
smaller = stats.KindPropertyNameStat.get_by_key_name('l_PersistentClass2')
self.response.out.write('difference: {} bytes'.format(bigger.bytes - smaller.bytes))
class InitHandler(webapp2.RequestHandler):
"""Creates 2000 entities each for long and short property names."""
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
start = time.time()
entities = []
for i in xrange(1, 2000):
key_id = 'persistent-class-1-{}'.format(i)
entity = PersistentClass1(id=key_id, super_very_long_property_name='foobar')
entities.append(entity)
ndb.put_multi(entities)
bigger_time = time.time() - start
self.response.out.write('Took {} seconds for long property names.\n'.format(bigger_time))
start = time.time()
entities = []
for i in xrange(1, 2000):
key_id = 'persistent-class-2-{}'.format(i)
entity = PersistentClass2(id=key_id, l='foobar')
entities.append(entity)
ndb.put_multi(entities)
shorter_time = time.time() - start
self.response.out.write('Took {} seconds for short property names.\n'.format(shorter_time))
self.response.out.write('difference: {}'.format(bigger_time - shorter_time))
application = webapp2.WSGIApplication([
('/init', InitHandler),
('/test', TestHandler),
])
@jeremydw
Copy link
Author

Results of running this using dev_appserver:

/init
Took 4.65448188782 seconds for long property names.
Took 4.62077403069 seconds for short property names.
difference: 0.033707857132

/test
name: l_PersistentClass2, bytes: 1507635
name: super_very_long_property_name_PersistentClass1, bytes: 1787607
difference: 279972 bytes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment