1515package spannerdriver
1616
1717import (
18- "bytes"
19- "encoding/gob"
2018 "math/big"
2119 "testing"
2220 "time"
@@ -26,13 +24,6 @@ import (
2624)
2725
2826func TestUpdateChecksum (t * testing.T ) {
29- buffer1 := & bytes.Buffer {}
30- enc1 := gob .NewEncoder (buffer1 )
31- buffer2 := & bytes.Buffer {}
32- enc2 := gob .NewEncoder (buffer2 )
33- buffer3 := & bytes.Buffer {}
34- enc3 := gob .NewEncoder (buffer3 )
35-
3627 row1 , err := spanner .NewRow (
3728 []string {
3829 "ColBool" , "ColInt64" , "ColFloat64" , "ColNumeric" , "ColString" , "ColBytes" , "ColDate" , "ColTimestamp" , "ColJson" ,
@@ -111,17 +102,17 @@ func TestUpdateChecksum(t *testing.T) {
111102 t .Fatalf ("could not create row 3: %v" , err )
112103 }
113104 initial1 := new ([32 ]byte )
114- checksum1 , err := updateChecksum (enc1 , buffer1 , initial1 , row1 )
105+ checksum1 , err := updateChecksum (initial1 , row1 )
115106 if err != nil {
116107 t .Fatalf ("could not calculate checksum 1: %v" , err )
117108 }
118109 initial2 := new ([32 ]byte )
119- checksum2 , err := updateChecksum (enc2 , buffer2 , initial2 , row2 )
110+ checksum2 , err := updateChecksum (initial2 , row2 )
120111 if err != nil {
121112 t .Fatalf ("could not calculate checksum 2: %v" , err )
122113 }
123114 initial3 := new ([32 ]byte )
124- checksum3 , err := updateChecksum (enc3 , buffer3 , initial3 , row3 )
115+ checksum3 , err := updateChecksum (initial3 , row3 )
125116 if err != nil {
126117 t .Fatalf ("could not calculate checksum 3: %v" , err )
127118 }
@@ -136,11 +127,11 @@ func TestUpdateChecksum(t *testing.T) {
136127
137128 // Updating checksums 1 and 3 with the data from row 2 should also produce
138129 // the same checksum.
139- checksum1_2 , err := updateChecksum (enc1 , buffer1 , checksum1 , row2 )
130+ checksum1_2 , err := updateChecksum (checksum1 , row2 )
140131 if err != nil {
141132 t .Fatalf ("could not calculate checksum 1_2: %v" , err )
142133 }
143- checksum3_2 , err := updateChecksum (enc3 , buffer3 , checksum3 , row2 )
134+ checksum3_2 , err := updateChecksum (checksum3 , row2 )
144135 if err != nil {
145136 t .Fatalf ("could not calculate checksum 1_2: %v" , err )
146137 }
@@ -150,7 +141,7 @@ func TestUpdateChecksum(t *testing.T) {
150141
151142 // The combination of row 3 and 2 will produce a different checksum than the
152143 // combination 2 and 3, because they are in a different order.
153- checksum2_3 , err := updateChecksum (enc2 , buffer2 , checksum2 , row3 )
144+ checksum2_3 , err := updateChecksum (checksum2 , row3 )
154145 if err != nil {
155146 t .Fatalf ("could not calculate checksum 2_3: %v" , err )
156147 }
@@ -160,9 +151,6 @@ func TestUpdateChecksum(t *testing.T) {
160151}
161152
162153func TestUpdateChecksumForNullValues (t * testing.T ) {
163- buffer := & bytes.Buffer {}
164- enc := gob .NewEncoder (buffer )
165-
166154 row , err := spanner .NewRow (
167155 []string {
168156 "ColBool" , "ColInt64" , "ColFloat64" , "ColNumeric" , "ColString" , "ColBytes" , "ColDate" , "ColTimestamp" , "ColJson" ,
@@ -182,7 +170,7 @@ func TestUpdateChecksumForNullValues(t *testing.T) {
182170 }
183171 initial := new ([32 ]byte )
184172 // Create the initial checksum.
185- checksum , err := updateChecksum (enc , buffer , initial , row )
173+ checksum , err := updateChecksum (initial , row )
186174 if err != nil {
187175 t .Fatalf ("could not calculate checksum 1: %v" , err )
188176 }
@@ -192,14 +180,88 @@ func TestUpdateChecksumForNullValues(t *testing.T) {
192180 t .Fatalf ("checksum value should not be equal to the initial value" )
193181 }
194182 // Calculating the same checksum again should yield the same result.
195- buffer2 := & bytes.Buffer {}
196- enc2 := gob .NewEncoder (buffer2 )
197183 initial2 := new ([32 ]byte )
198- checksum2 , err := updateChecksum (enc2 , buffer2 , initial2 , row )
184+ checksum2 , err := updateChecksum (initial2 , row )
199185 if err != nil {
200186 t .Fatalf ("failed to update checksum: %v" , err )
201187 }
202188 if * checksum != * checksum2 {
203189 t .Fatalf ("recalculated checksum does not match the initial calculation" )
204190 }
205191}
192+
193+ func BenchmarkChecksumRowIterator (b * testing.B ) {
194+ row1 , _ := spanner .NewRow (
195+ []string {
196+ "ColBool" , "ColInt64" , "ColFloat64" , "ColNumeric" , "ColString" , "ColBytes" , "ColDate" , "ColTimestamp" , "ColJson" ,
197+ "ArrBool" , "ArrInt64" , "ArrFloat64" , "ArrNumeric" , "ArrString" , "ArrBytes" , "ArrDate" , "ArrTimestamp" , "ArrJson" ,
198+ },
199+ []interface {}{
200+ true , int64 (1 ), 3.14 , numeric ("6.626" ), "test" , []byte ("testbytes" ), civil.Date {Year : 2021 , Month : 8 , Day : 5 },
201+ time .Date (2021 , 8 , 5 , 13 , 19 , 23 , 123456789 , time .UTC ),
202+ nullJson (true , `"key": "value", "other-key": ["value1", "value2"]}` ),
203+ []bool {true , false }, []int64 {1 , 2 }, []float64 {3.14 , 6.626 }, []big.Rat {numeric ("3.14" ), numeric ("6.626" )},
204+ []string {"test1" , "test2" }, [][]byte {[]byte ("testbytes1" ), []byte ("testbytes1" )},
205+ []civil.Date {{Year : 2021 , Month : 8 , Day : 5 }, {Year : 2021 , Month : 8 , Day : 6 }},
206+ []time.Time {
207+ time .Date (2021 , 8 , 5 , 13 , 19 , 23 , 123456789 , time .UTC ),
208+ time .Date (2021 , 8 , 6 , 13 , 19 , 23 , 123456789 , time .UTC ),
209+ },
210+ []spanner.NullJSON {
211+ nullJson (true , `"key1": "value1", "other-key1": ["value1", "value2"]}` ),
212+ nullJson (true , `"key2": "value2", "other-key2": ["value1", "value2"]}` ),
213+ },
214+ },
215+ )
216+ row2 , _ := spanner .NewRow (
217+ []string {
218+ "ColBool" , "ColInt64" , "ColFloat64" , "ColNumeric" , "ColString" , "ColBytes" , "ColDate" , "ColTimestamp" , "ColJson" ,
219+ "ArrBool" , "ArrInt64" , "ArrFloat64" , "ArrNumeric" , "ArrString" , "ArrBytes" , "ArrDate" , "ArrTimestamp" , "ArrJson" ,
220+ },
221+ []interface {}{
222+ true , int64 (2 ), 6.626 , numeric ("3.14" ), "test2" , []byte ("testbytes2" ), civil.Date {Year : 2020 , Month : 8 , Day : 5 },
223+ time .Date (2020 , 8 , 5 , 13 , 19 , 23 , 123456789 , time .UTC ),
224+ nullJson (true , `"key": "other-value", "other-key": ["other-value1", "other-value2"]}` ),
225+ []bool {true , false }, []int64 {1 , 2 }, []float64 {3.14 , 6.626 }, []big.Rat {numeric ("3.14" ), numeric ("6.626" )},
226+ []string {"test1_" , "test2_" }, [][]byte {[]byte ("testbytes1_" ), []byte ("testbytes1_" )},
227+ []civil.Date {{Year : 2020 , Month : 8 , Day : 5 }, {Year : 2020 , Month : 8 , Day : 6 }},
228+ []time.Time {
229+ time .Date (2020 , 8 , 5 , 13 , 19 , 23 , 123456789 , time .UTC ),
230+ time .Date (2020 , 8 , 6 , 13 , 19 , 23 , 123456789 , time .UTC ),
231+ },
232+ []spanner.NullJSON {
233+ nullJson (true , `"key1": "other-value1", "other-key1": ["other-value1", "other-value2"]}` ),
234+ nullJson (true , `"key2": "other-value2", "other-key2": ["other-value1", "other-value2"]}` ),
235+ },
236+ },
237+ )
238+ row3 , _ := spanner .NewRow (
239+ []string {
240+ "ColBool" , "ColInt64" , "ColFloat64" , "ColNumeric" , "ColString" , "ColBytes" , "ColDate" , "ColTimestamp" , "ColJson" ,
241+ "ArrBool" , "ArrInt64" , "ArrFloat64" , "ArrNumeric" , "ArrString" , "ArrBytes" , "ArrDate" , "ArrTimestamp" , "ArrJson" ,
242+ },
243+ []interface {}{
244+ true , int64 (1 ), 3.14 , numeric ("6.626" ), "test" , []byte ("testbytes" ), civil.Date {Year : 2021 , Month : 8 , Day : 5 },
245+ time .Date (2021 , 8 , 5 , 13 , 19 , 23 , 123456789 , time .UTC ),
246+ nullJson (true , `"key": "value", "other-key": ["value1", "value2"]}` ),
247+ []bool {true , false }, []int64 {1 , 2 }, []float64 {3.14 , 6.626 }, []big.Rat {numeric ("3.14" ), numeric ("6.626" )},
248+ []string {"test1" , "test2" }, [][]byte {[]byte ("testbytes1" ), []byte ("testbytes1" )},
249+ []civil.Date {{Year : 2021 , Month : 8 , Day : 5 }, {Year : 2021 , Month : 8 , Day : 6 }},
250+ []time.Time {
251+ time .Date (2021 , 8 , 5 , 13 , 19 , 23 , 123456789 , time .UTC ),
252+ time .Date (2021 , 8 , 6 , 13 , 19 , 23 , 123456789 , time .UTC ),
253+ },
254+ []spanner.NullJSON {
255+ nullJson (true , `"key1": "value1", "other-key1": ["value1", "value2"]}` ),
256+ nullJson (true , `"key2": "value2", "other-key2": ["value1", "value2"]}` ),
257+ },
258+ },
259+ )
260+
261+ for b .Loop () {
262+ initial := new ([32 ]byte )
263+ checksum , _ := updateChecksum (initial , row1 )
264+ checksum , _ = updateChecksum (checksum , row2 )
265+ checksum , _ = updateChecksum (checksum , row3 )
266+ }
267+ }
0 commit comments