1
1
import random
2
2
import time
3
3
import unittest
4
- from typing import Any
5
4
6
5
import pandas as pd
7
6
import pytest
@@ -63,8 +62,9 @@ def insert_many_rows(transaction, many_rows):
63
62
raise ValueError ("Wrong number of inserts: " + str (sum (count )))
64
63
65
64
66
- class DjangoBenchmarkTest () :
65
+ class DjangoBenchmarkTest :
67
66
"""The Django performace testing class."""
67
+
68
68
def __init__ (self ):
69
69
with connection .schema_editor () as editor :
70
70
# Create the tables
@@ -73,18 +73,15 @@ def __init__(self):
73
73
self ._many_rows = []
74
74
self ._many_rows2 = []
75
75
for i in range (99 ):
76
- num = round (random .randint (0 ,100000000 ))
76
+ num = round (random .randint (0 , 100000000 ))
77
77
self ._many_rows .append (Author (num , "Pete" , "Allison" , "2.1" ))
78
- num2 = round (random .randint (0 ,100000000 ))
78
+ num2 = round (random .randint (0 , 100000000 ))
79
79
self ._many_rows2 .append (Author (num2 , "Pete" , "Allison" , "2.1" ))
80
80
81
81
@measure_execution_time
82
82
def insert_one_row_with_fetch_after (self ):
83
83
author_kent = Author (
84
- id = 2 ,
85
- first_name = "Pete" ,
86
- last_name = "Allison" ,
87
- rating = "2.1" ,
84
+ id = 2 , first_name = "Pete" , last_name = "Allison" , rating = "2.1" ,
88
85
)
89
86
author_kent .save ()
90
87
last_name = Author .objects .get (pk = author_kent .id ).last_name
@@ -133,8 +130,9 @@ def run(self):
133
130
return measures
134
131
135
132
136
- class SpannerBenchmarkTest () :
133
+ class SpannerBenchmarkTest :
137
134
"""The Spanner performace testing class."""
135
+
138
136
def __init__ (self ):
139
137
self ._create_table ()
140
138
self ._one_row = (
@@ -150,14 +148,15 @@ def __init__(self):
150
148
self ._many_rows = []
151
149
self ._many_rows2 = []
152
150
for i in range (99 ):
153
- num = round (random .randint (0 ,100000000 ))
151
+ num = round (random .randint (0 , 100000000 ))
154
152
self ._many_rows .append ((num , "Pete" , "Allison" , "2.1" ))
155
- num2 = round (random .randint (0 ,100000000 ))
153
+ num2 = round (random .randint (0 , 100000000 ))
156
154
self ._many_rows2 .append ((num2 , "Pete" , "Allison" , "2.1" ))
157
155
158
156
# initiate a session
159
157
with self ._database .snapshot ():
160
158
pass
159
+
161
160
def _create_table (self ):
162
161
"""Create a table for performace testing."""
163
162
conn = spanner_dbapi .connect (INSTANCE_ID , DATABASE_NAME )
@@ -232,28 +231,60 @@ def run(self):
232
231
self ._cleanup ()
233
232
return measures
234
233
234
+
235
235
@pytest .mark .django_db ()
236
236
class BenchmarkTest (unittest .TestCase ):
237
237
def setUp (self ):
238
238
setup_instance ()
239
239
setup_database ()
240
240
241
241
def test_run (self ):
242
- django_obj = pd .DataFrame (columns = ['insert_one_row_with_fetch_after' , 'read_one_row' , 'insert_many_rows' , 'select_many_rows' ,
243
- 'insert_many_rows_with_mutations' ])
244
- spanner_obj = pd .DataFrame (columns = ['insert_one_row_with_fetch_after' , 'read_one_row' , 'insert_many_rows' , 'select_many_rows' ,
245
- 'insert_many_rows_with_mutations' ])
242
+ django_obj = pd .DataFrame (
243
+ columns = [
244
+ "insert_one_row_with_fetch_after" ,
245
+ "read_one_row" ,
246
+ "insert_many_rows" ,
247
+ "select_many_rows" ,
248
+ "insert_many_rows_with_mutations" ,
249
+ ]
250
+ )
251
+ spanner_obj = pd .DataFrame (
252
+ columns = [
253
+ "insert_one_row_with_fetch_after" ,
254
+ "read_one_row" ,
255
+ "insert_many_rows" ,
256
+ "select_many_rows" ,
257
+ "insert_many_rows_with_mutations" ,
258
+ ]
259
+ )
246
260
247
261
for _ in range (50 ):
248
- django_obj = django_obj .append (DjangoBenchmarkTest ().run (), ignore_index = True )
249
- spanner_obj = spanner_obj .append (SpannerBenchmarkTest ().run (), ignore_index = True )
250
-
251
- avg = pd .concat ([django_obj .mean (axis = 0 ), spanner_obj .mean (axis = 0 )], axis = 1 )
252
- avg .columns = ['Django' ,'Spanner' ]
253
- std = pd .concat ([django_obj .std (axis = 0 ), spanner_obj .std (axis = 0 )], axis = 1 )
254
- std .columns = ['Django' ,'Spanner' ]
255
- err = pd .concat ([django_obj .sem (axis = 0 ), spanner_obj .sem (axis = 0 )], axis = 1 )
256
- err .columns = ['Django' ,'Spanner' ]
257
-
258
- print ("Average: " , avg , "Standard Deviation: " , std , "Error:" , err , sep = '\n ' )
259
-
262
+ django_obj = django_obj .append (
263
+ DjangoBenchmarkTest ().run (), ignore_index = True
264
+ )
265
+ spanner_obj = spanner_obj .append (
266
+ SpannerBenchmarkTest ().run (), ignore_index = True
267
+ )
268
+
269
+ avg = pd .concat (
270
+ [django_obj .mean (axis = 0 ), spanner_obj .mean (axis = 0 )], axis = 1
271
+ )
272
+ avg .columns = ["Django" , "Spanner" ]
273
+ std = pd .concat (
274
+ [django_obj .std (axis = 0 ), spanner_obj .std (axis = 0 )], axis = 1
275
+ )
276
+ std .columns = ["Django" , "Spanner" ]
277
+ err = pd .concat (
278
+ [django_obj .sem (axis = 0 ), spanner_obj .sem (axis = 0 )], axis = 1
279
+ )
280
+ err .columns = ["Django" , "Spanner" ]
281
+
282
+ print (
283
+ "Average: " ,
284
+ avg ,
285
+ "Standard Deviation: " ,
286
+ std ,
287
+ "Error:" ,
288
+ err ,
289
+ sep = "\n " ,
290
+ )
0 commit comments