Skip to content

Commit 0299739

Browse files
authored
chore: update benchmark to better compare performance (#157)
* chore: update benchmark to better compare performance * style: fix lint Co-authored-by: larkee <larkee@users.noreply.github.com>
1 parent e6dbd83 commit 0299739

File tree

2 files changed

+77
-25
lines changed

2 files changed

+77
-25
lines changed

create_test_database.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def create_test_instance():
8282

8383
instance = CLIENT.instance(instance_id, instance_config, labels=labels)
8484

85+
8586
try:
8687
created_op = instance.create()
8788
created_op.result(1800) # block until completion

test/benchmark.py

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
A test suite to check Spanner dialect for SQLAlchemy performance
1717
in comparison with the original Spanner client.
1818
"""
19+
import base64
1920
import datetime
2021
import random
2122
from scipy.stats import sem
2223
import statistics
2324
import time
2425

2526
from google.api_core.exceptions import Aborted
27+
from google.api_core.exceptions import NotFound
28+
from google.cloud import spanner
2629
from google.cloud import spanner_dbapi
2730
from google.cloud.spanner_v1 import Client, KeySet
2831
from sqlalchemy import (
@@ -64,26 +67,42 @@ class BenchmarkTestBase:
6467
Organizes testing data preparation and cleanup.
6568
"""
6669

70+
_many_rows_ids = []
71+
_many_rows2_ids = []
72+
6773
def __init__(self):
74+
self._cleanup()
6875
self._create_table()
6976

70-
self._one_row = (
71-
1,
72-
"Pete",
73-
"Allison",
74-
datetime.datetime(1998, 10, 6).strftime("%Y-%m-%d"),
75-
b"123",
76-
)
77+
self._one_row = {
78+
"id": 1,
79+
"first_name": "Pete",
80+
"last_name": "Allison",
81+
"birth_date": datetime.date(1998, 10, 6),
82+
"picture": b"123",
83+
}
84+
self.keys = set([1])
85+
if not self._many_rows_ids:
86+
for i in range(99):
87+
self._many_rows_ids.append(self._generate_id())
88+
self._many_rows2_ids.append(self._generate_id())
7789

7890
def _cleanup(self):
7991
"""Drop the test table."""
8092
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
8297
conn.close()
8398

8499
def _create_table(self):
85100
"""Create a table for performace testing."""
86101
conn = spanner_dbapi.connect(INSTANCE, DATABASE)
102+
try:
103+
conn.database.update_ddl(["DROP TABLE Singers"])
104+
except NotFound:
105+
pass
87106
conn.database.update_ddl(
88107
[
89108
"""
@@ -96,10 +115,17 @@ def _create_table(self):
96115
) PRIMARY KEY (id)
97116
"""
98117
]
99-
).result(120)
118+
).result()
100119

101120
conn.close()
102121

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+
103129
def run(self):
104130
"""Execute every test case."""
105131
measures = {}
@@ -117,7 +143,7 @@ def run(self):
117143

118144

119145
class SpannerBenchmarkTest(BenchmarkTestBase):
120-
"""The original Spanner performace testing class."""
146+
"""The original Spanner performance testing class."""
121147

122148
def __init__(self):
123149
super().__init__()
@@ -127,12 +153,20 @@ def __init__(self):
127153

128154
self._many_rows = []
129155
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))
136170

137171
# initiate a session
138172
with self._database.snapshot():
@@ -192,9 +226,8 @@ def __init__(self):
192226

193227
self._many_rows = []
194228
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:
198231
self._many_rows.append(
199232
{
200233
"id": num,
@@ -204,10 +237,10 @@ def __init__(self):
204237
"picture": b"123",
205238
}
206239
)
207-
num2 = round(random.random() * 1000000)
240+
for num in self._many_rows2_ids:
208241
self._many_rows2.append(
209242
{
210-
"id": num2,
243+
"id": num,
211244
"first_name": "Pete",
212245
"last_name": "Allison",
213246
"birth_date": birth_date,
@@ -255,8 +288,16 @@ def insert_one_row(transaction, one_row):
255288
Inserts a single row into a database and then fetches it back.
256289
"""
257290
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+
},
260301
)
261302
last_name = transaction.execute_sql(
262303
"SELECT last_name FROM Singers WHERE id=1"
@@ -273,8 +314,18 @@ def insert_many_rows(transaction, many_rows):
273314
statements = []
274315
for row in many_rows:
275316
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+
)
278329
)
279330
_, count = transaction.batch_update(statements)
280331
if sum(count) != 99:

0 commit comments

Comments
 (0)