11import random
22import time
33import unittest
4- from typing import Any
54
65import pandas as pd
76import pytest
@@ -63,8 +62,9 @@ def insert_many_rows(transaction, many_rows):
6362 raise ValueError ("Wrong number of inserts: " + str (sum (count )))
6463
6564
66- class DjangoBenchmarkTest () :
65+ class DjangoBenchmarkTest :
6766 """The Django performace testing class."""
67+
6868 def __init__ (self ):
6969 with connection .schema_editor () as editor :
7070 # Create the tables
@@ -73,18 +73,15 @@ def __init__(self):
7373 self ._many_rows = []
7474 self ._many_rows2 = []
7575 for i in range (99 ):
76- num = round (random .randint (0 ,100000000 ))
76+ num = round (random .randint (0 , 100000000 ))
7777 self ._many_rows .append (Author (num , "Pete" , "Allison" , "2.1" ))
78- num2 = round (random .randint (0 ,100000000 ))
78+ num2 = round (random .randint (0 , 100000000 ))
7979 self ._many_rows2 .append (Author (num2 , "Pete" , "Allison" , "2.1" ))
8080
8181 @measure_execution_time
8282 def insert_one_row_with_fetch_after (self ):
8383 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" ,
8885 )
8986 author_kent .save ()
9087 last_name = Author .objects .get (pk = author_kent .id ).last_name
@@ -133,8 +130,9 @@ def run(self):
133130 return measures
134131
135132
136- class SpannerBenchmarkTest () :
133+ class SpannerBenchmarkTest :
137134 """The Spanner performace testing class."""
135+
138136 def __init__ (self ):
139137 self ._create_table ()
140138 self ._one_row = (
@@ -150,14 +148,15 @@ def __init__(self):
150148 self ._many_rows = []
151149 self ._many_rows2 = []
152150 for i in range (99 ):
153- num = round (random .randint (0 ,100000000 ))
151+ num = round (random .randint (0 , 100000000 ))
154152 self ._many_rows .append ((num , "Pete" , "Allison" , "2.1" ))
155- num2 = round (random .randint (0 ,100000000 ))
153+ num2 = round (random .randint (0 , 100000000 ))
156154 self ._many_rows2 .append ((num2 , "Pete" , "Allison" , "2.1" ))
157155
158156 # initiate a session
159157 with self ._database .snapshot ():
160158 pass
159+
161160 def _create_table (self ):
162161 """Create a table for performace testing."""
163162 conn = spanner_dbapi .connect (INSTANCE_ID , DATABASE_NAME )
@@ -232,28 +231,60 @@ def run(self):
232231 self ._cleanup ()
233232 return measures
234233
234+
235235@pytest .mark .django_db ()
236236class BenchmarkTest (unittest .TestCase ):
237237 def setUp (self ):
238238 setup_instance ()
239239 setup_database ()
240240
241241 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+ )
246260
247261 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