-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathputbench.py
100 lines (78 loc) · 2.38 KB
/
putbench.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Benchmark for put_multi().
Run this using 'make x CUSTOM=putbench FLAGS=-n'.
Use FLAGS=-o to get the corresponding profile for the old db package.
"""
import cProfile
import os
import pstats
import sys
import time
# Pay no attention to the testbed behind the curtain.
from google.appengine.ext import testbed
tb = testbed.Testbed()
tb.activate()
tb.init_datastore_v3_stub()
tb.init_memcache_stub()
from google.appengine.ext import db
import ndb
N = 1000
class Person(db.Model):
a0 = db.StringProperty(default='a0')
a1 = db.StringProperty(default='a1')
a2 = db.StringProperty(default='a2')
a3 = db.StringProperty(default='a3')
a4 = db.StringProperty(default='a4')
a5 = db.StringProperty(default='a5')
a6 = db.StringProperty(default='a6')
a7 = db.StringProperty(default='a7')
a8 = db.StringProperty(default='a8')
a9 = db.StringProperty(default='a9')
OldPerson = Person
class Person(ndb.Model):
a0 = ndb.StringProperty(default='a0')
a1 = ndb.StringProperty(default='a1')
a2 = ndb.StringProperty(default='a2')
a3 = ndb.StringProperty(default='a3')
a4 = ndb.StringProperty(default='a4')
a5 = ndb.StringProperty(default='a5')
a6 = ndb.StringProperty(default='a6')
a7 = ndb.StringProperty(default='a7')
a8 = ndb.StringProperty(default='a8')
a9 = ndb.StringProperty(default='a9')
NewPerson = Person
def put_old(people):
keys = db.put(people)
def put_new(people):
keys = ndb.put_multi(people, use_cache=False, use_memcache=False)
def timer(func, people):
t0 = time.time()
func(people)
t1 = time.time()
print '%.3f seconds' % (t1-t0)
def main(k=0):
if k > 0:
return main(k-1)
try:
n = int(sys.argv[-1])
except:
n = N
if '-o' in sys.argv and '-n' not in sys.argv:
people = [OldPerson() for i in xrange(n)]
func = put_old
elif '-n' in sys.argv and '-o' not in sys.argv:
people = [NewPerson() for i in xrange(n)]
func = put_new
else:
sys.stderr.write('Usage: $0 (-o|-n)\n')
sys.exit(2)
prof = cProfile.Profile()
prof = prof.runctx('timer(func, people)', globals(), locals())
stats = pstats.Stats(prof)
stats.strip_dirs()
stats.sort_stats('calls') # 'time', 'cumulative', 'file' or 'calls'
stats.print_stats(20) # Arg: how many to print (optional)
# Uncomment (and tweak) the following calls for more details.
# stats.print_callees(100)
# stats.print_callers(100)
if __name__ == '__main__':
main(9)