1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package spanner
import (
"reflect"
"strconv"
"testing"
"cloud.google.com/go/civil"
proto3 "github.com/golang/protobuf/ptypes/struct"
sppb "google.golang.org/genproto/googleapis/spanner/v1"
)
func BenchmarkEncodeIntArray(b *testing.B) {
for _, s := range []struct {
name string
f func(a []int) (*proto3.Value, *sppb.Type, error)
}{
{"Orig", encodeIntArrayOrig},
{"Func", encodeIntArrayFunc},
{"Reflect", encodeIntArrayReflect},
} {
b.Run(s.name, func(b *testing.B) {
for _, size := range []int{1, 10, 100, 1000} {
a := make([]int, size)
b.Run(strconv.Itoa(size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
s.f(a)
}
})
}
})
}
}
func encodeIntArrayOrig(a []int) (*proto3.Value, *sppb.Type, error) {
vs := make([]*proto3.Value, len(a))
var err error
for i := range a {
vs[i], _, err = encodeValue(a[i])
if err != nil {
return nil, nil, err
}
}
return listProto(vs...), listType(intType()), nil
}
func encodeIntArrayFunc(a []int) (*proto3.Value, *sppb.Type, error) {
v, err := encodeArray(len(a), func(i int) interface{} { return a[i] })
if err != nil {
return nil, nil, err
}
return v, listType(intType()), nil
}
func encodeIntArrayReflect(a []int) (*proto3.Value, *sppb.Type, error) {
v, err := encodeArrayReflect(a)
if err != nil {
return nil, nil, err
}
return v, listType(intType()), nil
}
func encodeArrayReflect(a interface{}) (*proto3.Value, error) {
va := reflect.ValueOf(a)
len := va.Len()
vs := make([]*proto3.Value, len)
var err error
for i := 0; i < len; i++ {
vs[i], _, err = encodeValue(va.Index(i).Interface())
if err != nil {
return nil, err
}
}
return listProto(vs...), nil
}
func BenchmarkDecodeGeneric(b *testing.B) {
v := stringProto("test")
t := stringType()
var g GenericColumnValue
b.ResetTimer()
for i := 0; i < b.N; i++ {
decodeValue(v, t, &g)
}
}
func BenchmarkDecodeString(b *testing.B) {
v := stringProto("test")
t := stringType()
var s string
b.ResetTimer()
for i := 0; i < b.N; i++ {
decodeValue(v, t, &s)
}
}
func BenchmarkDecodeCustomString(b *testing.B) {
v := stringProto("test")
t := stringType()
type CustomString string
var s CustomString
b.ResetTimer()
for i := 0; i < b.N; i++ {
decodeValue(v, t, &s)
}
}
func BenchmarkDecodeArray(b *testing.B) {
for _, size := range []int{1, 10, 100, 1000} {
vals := make([]*proto3.Value, size)
for i := 0; i < size; i++ {
vals[i] = dateProto(d1)
}
lv := &proto3.ListValue{Values: vals}
b.Run(strconv.Itoa(size), func(b *testing.B) {
for _, s := range []struct {
name string
decode func(*proto3.ListValue)
}{
{"DateDirect", decodeArrayDateDirect},
{"DateFunc", decodeArrayDateFunc},
{"DateReflect", decodeArrayDateReflect},
{"StringDecodeStringArray", decodeStringArrayWrap},
{"StringDirect", decodeArrayStringDirect},
{"StringFunc", decodeArrayStringFunc},
{"StringReflect", decodeArrayStringReflect},
} {
b.Run(s.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
s.decode(lv)
}
})
}
})
}
}
func decodeArrayDateDirect(pb *proto3.ListValue) {
a := make([]civil.Date, len(pb.Values))
t := dateType()
for i, v := range pb.Values {
if err := decodeValue(v, t, &a[i]); err != nil {
panic(err)
}
}
}
func decodeArrayDateFunc(pb *proto3.ListValue) {
a := make([]civil.Date, len(pb.Values))
if err := decodeArrayFunc(pb, "DATE", dateType(), func(i int) interface{} { return &a[i] }); err != nil {
panic(err)
}
}
func decodeArrayDateReflect(pb *proto3.ListValue) {
var a []civil.Date
if err := decodeArrayReflect(pb, "DATE", dateType(), &a); err != nil {
panic(err)
}
}
func decodeStringArrayWrap(pb *proto3.ListValue) {
if _, err := decodeStringArray(pb); err != nil {
panic(err)
}
}
func decodeArrayStringDirect(pb *proto3.ListValue) {
a := make([]string, len(pb.Values))
t := stringType()
for i, v := range pb.Values {
if err := decodeValue(v, t, &a[i]); err != nil {
panic(err)
}
}
}
func decodeArrayStringFunc(pb *proto3.ListValue) {
a := make([]string, len(pb.Values))
if err := decodeArrayFunc(pb, "STRING", stringType(), func(i int) interface{} { return &a[i] }); err != nil {
panic(err)
}
}
func decodeArrayStringReflect(pb *proto3.ListValue) {
var a []string
if err := decodeArrayReflect(pb, "STRING", stringType(), &a); err != nil {
panic(err)
}
}
func decodeArrayFunc(pb *proto3.ListValue, name string, typ *sppb.Type, elptr func(int) interface{}) error {
if pb == nil {
return errNilListValue(name)
}
for i, v := range pb.Values {
if err := decodeValue(v, typ, elptr(i)); err != nil {
return errDecodeArrayElement(i, v, name, err)
}
}
return nil
}
func decodeArrayReflect(pb *proto3.ListValue, name string, typ *sppb.Type, aptr interface{}) error {
if pb == nil {
return errNilListValue(name)
}
av := reflect.ValueOf(aptr).Elem()
av.Set(reflect.MakeSlice(av.Type(), len(pb.Values), len(pb.Values)))
for i, v := range pb.Values {
if err := decodeValue(v, typ, av.Index(i).Addr().Interface()); err != nil {
av.Set(reflect.Zero(av.Type())) // reset slice to nil
return errDecodeArrayElement(i, v, name, err)
}
}
return nil
}
|