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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
import ndb
import sys,time,random,struct,math
import myprep
if len(sys.argv) != 3:
print "Usage:\n\ttest.py NUM_OF_ITERATIONS NUM_OF_ROWS "
sys.exit(1)
num_iter=int(sys.argv[1])
INSERT_NUM=int(sys.argv[2])
BATCH_SIZE=1000
myprep.prep()
### Connect to cluster
print "connecting to cluster\n"
connection = ndb.Ndb_cluster_connection()
if (connection.connect(5,3,1)):
print "Connect to cluster management server failed."
sys.exit(-1)
if (connection.wait_until_ready(30,30)):
print "Cluster was not ready within 30 secs."
sys.exit(-1);
myNdb = ndb.Ndb(connection,"TEST_DB_1" )
if (myNdb.init(4) == -1):
print myNdb.getNdbError().getMessage()
sys.exit(-1)
print "running tests:\n"
### Fill db
before_t = time.time()
for t in range(0,int(math.ceil((int(INSERT_NUM)*1.0)/(1.0*BATCH_SIZE)))):
myTransaction= myNdb.startTransaction()
val = ((t+1)*BATCH_SIZE)-INSERT_NUM
offset = 0
if ( val > 0 ):
offset = val
for i in range(0,BATCH_SIZE-offset):
myOperation=myTransaction.getNdbOperation("MYTABLENAME")
myOperation.insertTuple()
auto_id = myNdb.getAutoIncrementValue("MYTABLENAME",BATCH_SIZE)
myOperation.equal("ATTR1",auto_id);
myOperation.setValue("ATTR2", i);
ret = myTransaction.execute( ndb.Commit )
if ret == -1:
print myTransaction.getNdbError().getMessage()
myNdb.closeTransaction(myTransaction)
after_t = time.time()
print "Insert time for %s: %s -- %s" % ( INSERT_NUM, after_t, before_t)
print "\t %s" % (after_t - before_t)
### Get list of ids
myTransaction = myNdb.startTransaction()
myScanOperation=myTransaction.getNdbScanOperation("MYTABLENAME");
if myScanOperation is None:
print myTransaction.getNdbError().getMessage()
if myScanOperation.readTuples(ndb.NdbOperation.LM_CommittedRead):
print myScanOperation.getNdbError().getMessage()
myRecAttr=myScanOperation.getValue("ATTR1");
myTransaction.execute(ndb.NoCommit);
ids=[]
while 1:
if (myScanOperation.nextResult(True) != 0) :
break
random_id = myRecAttr.u_32_value()
ids.append(random_id)
myNdb.closeTransaction(myTransaction)
### Fetch ids using ndb
before_t = time.time()
for f in range(0,int(num_iter)):
id_num = ids[random.randrange(0,len(ids))]
myTrans = myNdb.startTransaction("MYTABLENAME","%s"%id_num)
if myTrans is None:
print myNdb.getNdbError().getMessage()
myOper = myTrans.getNdbOperation("MYTABLENAME")
myOper.readTuple(ndb.NdbOperation.LM_Read)
myOper.equal("ATTR1",id_num)
myRecAttr= myOper.getValue("ATTR2")
if myTrans.execute( ndb.Commit ) == -1:
print myTrans.getNdbError().getMessage()
#foo=myRecAttr.u_32_value()
foo=myRecAttr.get_value()
#print foo, myRecAttr.getColType(), myRecAttr.get_value()
myNdb.closeTransaction(myTrans)
after_t = time.time()
print "NDBAPI time for %s: %s -- %s" % ( num_iter, after_t, before_t)
print "\t %s" % (after_t - before_t)
cur=db.cursor()
before_t = time.time()
for f in range(0,int(num_iter)):
id_num = ids[random.randrange(0,len(ids))]
cur.execute("select ATTR2 from MYTABLENAME where ATTR1=%s" % (id_num))
res=cur.fetchall()
after_t = time.time()
after_t = time.time()
print "MySQL time for %s: %s -- %s" % ( num_iter, after_t, before_t)
print "\t %s" % (after_t - before_t)
|