16
16
A test suite to check Spanner dialect for SQLAlchemy performance
17
17
in comparison with the original Spanner client.
18
18
"""
19
+ import base64
19
20
import datetime
20
21
import random
21
22
from scipy .stats import sem
22
23
import statistics
23
24
import time
24
25
25
26
from google .api_core .exceptions import Aborted
27
+ from google .api_core .exceptions import NotFound
28
+ from google .cloud import spanner
26
29
from google .cloud import spanner_dbapi
27
30
from google .cloud .spanner_v1 import Client , KeySet
28
31
from sqlalchemy import (
@@ -63,27 +66,43 @@ class BenchmarkTestBase:
63
66
64
67
Organizes testing data preparation and cleanup.
65
68
"""
69
+ _many_rows_ids = []
70
+ _many_rows2_ids = []
66
71
67
72
def __init__ (self ):
73
+ self ._cleanup ()
68
74
self ._create_table ()
69
75
70
- self ._one_row = (
71
- 1 ,
72
- "Pete" ,
73
- "Allison" ,
74
- datetime .datetime (1998 , 10 , 6 ).strftime ("%Y-%m-%d" ),
75
- b"123" ,
76
- )
76
+ self ._one_row = {
77
+ "id" : 1 ,
78
+ "first_name" : "Pete" ,
79
+ "last_name" : "Allison" ,
80
+ "birth_date" : datetime .date (1998 , 10 , 6 ),
81
+ "picture" : b"123" ,
82
+ }
83
+ self .keys = set ([1 ])
84
+ if not self ._many_rows_ids :
85
+ for i in range (99 ):
86
+ self ._many_rows_ids .append (self ._generate_id ())
87
+ self ._many_rows2_ids .append (self ._generate_id ())
88
+
77
89
78
90
def _cleanup (self ):
79
91
"""Drop the test table."""
80
92
conn = spanner_dbapi .connect (INSTANCE , DATABASE )
81
- conn .database .update_ddl (["DROP TABLE Singers" ])
93
+ try :
94
+ conn .database .update_ddl (["DROP TABLE Singers" ])
95
+ except NotFound :
96
+ pass
82
97
conn .close ()
83
98
84
99
def _create_table (self ):
85
100
"""Create a table for performace testing."""
86
101
conn = spanner_dbapi .connect (INSTANCE , DATABASE )
102
+ try :
103
+ conn .database .update_ddl (["DROP TABLE Singers" ])
104
+ except NotFound :
105
+ pass
87
106
conn .database .update_ddl (
88
107
[
89
108
"""
@@ -96,10 +115,17 @@ def _create_table(self):
96
115
) PRIMARY KEY (id)
97
116
"""
98
117
]
99
- ).result (120 )
118
+ ).result ()
100
119
101
120
conn .close ()
102
121
122
+ def _generate_id (self ):
123
+ num = 1
124
+ while num in self .keys :
125
+ num = round (random .random () * 1000000 )
126
+ self .keys .add (num )
127
+ return num
128
+
103
129
def run (self ):
104
130
"""Execute every test case."""
105
131
measures = {}
@@ -117,7 +143,7 @@ def run(self):
117
143
118
144
119
145
class SpannerBenchmarkTest (BenchmarkTestBase ):
120
- """The original Spanner performace testing class."""
146
+ """The original Spanner performance testing class."""
121
147
122
148
def __init__ (self ):
123
149
super ().__init__ ()
@@ -127,12 +153,20 @@ def __init__(self):
127
153
128
154
self ._many_rows = []
129
155
self ._many_rows2 = []
130
- birth_date = datetime .datetime (1998 , 10 , 6 ).strftime ("%Y-%m-%d" )
131
- for i in range (99 ):
132
- num = round (random .random () * 1000000 )
133
- self ._many_rows .append ((num , "Pete" , "Allison" , birth_date , b"123" ))
134
- num2 = round (random .random () * 1000000 )
135
- self ._many_rows2 .append ((num2 , "Pete" , "Allison" , birth_date , b"123" ))
156
+ birth_date = datetime .date (1998 , 10 , 6 )
157
+ picture = base64 .b64encode (u"123" .encode ())
158
+ for num in self ._many_rows_ids :
159
+ self ._many_rows .append (
160
+ {
161
+ "id" : num ,
162
+ "first_name" : "Pete" ,
163
+ "last_name" : "Allison" ,
164
+ "birth_date" : birth_date ,
165
+ "picture" : picture ,
166
+ }
167
+ )
168
+ for num in self ._many_rows2_ids :
169
+ self ._many_rows2 .append ((num , "Pete" , "Allison" , birth_date , picture ))
136
170
137
171
# initiate a session
138
172
with self ._database .snapshot ():
@@ -192,9 +226,8 @@ def __init__(self):
192
226
193
227
self ._many_rows = []
194
228
self ._many_rows2 = []
195
- birth_date = datetime .datetime (1998 , 10 , 6 ).strftime ("%Y-%m-%d" )
196
- for i in range (99 ):
197
- num = round (random .random () * 1000000 )
229
+ birth_date = datetime .date (1998 , 10 , 6 )
230
+ for num in self ._many_rows_ids :
198
231
self ._many_rows .append (
199
232
{
200
233
"id" : num ,
@@ -204,10 +237,10 @@ def __init__(self):
204
237
"picture" : b"123" ,
205
238
}
206
239
)
207
- num2 = round ( random . random () * 1000000 )
240
+ for num in self . _many_rows2_ids :
208
241
self ._many_rows2 .append (
209
242
{
210
- "id" : num2 ,
243
+ "id" : num ,
211
244
"first_name" : "Pete" ,
212
245
"last_name" : "Allison" ,
213
246
"birth_date" : birth_date ,
@@ -255,8 +288,16 @@ def insert_one_row(transaction, one_row):
255
288
Inserts a single row into a database and then fetches it back.
256
289
"""
257
290
transaction .execute_update (
258
- "INSERT Singers (id, first_name, last_name, birth_date, picture) "
259
- " VALUES {}" .format (str (one_row ))
291
+ "INSERT INTO `Singers` (id, first_name, last_name, birth_date, picture)"
292
+ " VALUES (@id, @first_name, @last_name, @birth_date, @picture)" ,
293
+ params = one_row ,
294
+ param_types = {
295
+ "id" : spanner .param_types .INT64 ,
296
+ "first_name" : spanner .param_types .STRING ,
297
+ "last_name" : spanner .param_types .STRING ,
298
+ "birth_date" : spanner .param_types .DATE ,
299
+ "picture" : spanner .param_types .BYTES ,
300
+ }
260
301
)
261
302
last_name = transaction .execute_sql (
262
303
"SELECT last_name FROM Singers WHERE id=1"
@@ -273,8 +314,18 @@ def insert_many_rows(transaction, many_rows):
273
314
statements = []
274
315
for row in many_rows :
275
316
statements .append (
276
- "INSERT Singers (id, first_name, last_name, birth_date, picture) "
277
- " VALUES {}" .format (str (row ))
317
+ (
318
+ "INSERT INTO `Singers` (id, first_name, last_name, birth_date, picture) "
319
+ "VALUES (@id, @first_name, @last_name, @birth_date, @picture)" ,
320
+ row ,
321
+ {
322
+ "id" : spanner .param_types .INT64 ,
323
+ "first_name" : spanner .param_types .STRING ,
324
+ "last_name" : spanner .param_types .STRING ,
325
+ "birth_date" : spanner .param_types .DATE ,
326
+ "picture" : spanner .param_types .BYTES ,
327
+ }
328
+ )
278
329
)
279
330
_ , count = transaction .batch_update (statements )
280
331
if sum (count ) != 99 :
0 commit comments