diff --git a/core/benchmark.sh b/core/benchmark.sh new file mode 100755 index 00000000..eab9e57e --- /dev/null +++ b/core/benchmark.sh @@ -0,0 +1 @@ +go test -v -bench=. -run=XXX diff --git a/core/db.go b/core/db.go index f3fbbd43..21d276f0 100644 --- a/core/db.go +++ b/core/db.go @@ -93,7 +93,7 @@ func (rs *Rows) ScanStruct2(dest interface{}) error { if !ok { cache = make(map[string]int) for i := 0; i < vvv.NumField(); i++ { - cache[vvv.Type().Field(i).Name] = i + cache[rs.Mapper.Obj2Table(vvv.Type().Field(i).Name)] = i } fieldCacheMutex.Lock() fieldCache[t] = cache @@ -103,7 +103,7 @@ func (rs *Rows) ScanStruct2(dest interface{}) error { newDest := make([]interface{}, len(cols)) var v EmptyScanner for j, name := range cols { - if i, ok := cache[rs.Mapper.Table2Obj(name)]; ok { + if i, ok := cache[name]; ok { newDest[j] = vvv.Field(i).Addr().Interface() } else { newDest[j] = &v @@ -141,7 +141,8 @@ func (rs *Rows) ScanSlice(dest interface{}) error { return err } - for i, _ := range cols { + srcLen := vvv.Len() + for i := srcLen; i < len(cols); i++ { vvv = reflect.Append(vvv, reflect.ValueOf(newDest[i]).Elem()) } return nil diff --git a/core/db_test.go b/core/db_test.go index 1ea670a5..2d0e85c5 100644 --- a/core/db_test.go +++ b/core/db_test.go @@ -1,6 +1,8 @@ package core import ( + "errors" + "fmt" "os" "testing" @@ -98,7 +100,10 @@ func BenchmarkStructQuery(b *testing.B) { if err != nil { b.Error(err) } - //fmt.Println(user) + if user.Name != "xlw" { + fmt.Println(user) + b.Error(errors.New("name should be xlw")) + } } rows.Close() } @@ -140,7 +145,10 @@ func BenchmarkStruct2Query(b *testing.B) { if err != nil { b.Error(err) } - //fmt.Println(user) + if user.Name != "xlw" { + fmt.Println(user) + b.Error(errors.New("name should be xlw")) + } } rows.Close() } @@ -186,7 +194,10 @@ func BenchmarkSliceQuery(b *testing.B) { if err != nil { b.Error(err) } - //fmt.Println(slice) + if slice[1].(string) != "xlw" { + fmt.Println(slice) + b.Error(errors.New("name should be xlw")) + } } rows.Close() @@ -228,7 +239,10 @@ func BenchmarkMapInterfaceQuery(b *testing.B) { if err != nil { b.Error(err) } - //fmt.Println(m) + if m["name"].(string) != "xlw" { + fmt.Println(m) + b.Error(errors.New("name should be xlw")) + } } rows.Close() @@ -270,9 +284,10 @@ func BenchmarkMapBytesQuery(b *testing.B) { if err != nil { b.Error(err) } - /*for k, v := range m { - fmt.Printf("%v - %v\n", k, string(v)) - }*/ + if string(m["name"]) != "xlw" { + fmt.Println(m) + b.Error(errors.New("name should be xlw")) + } } rows.Close() @@ -314,9 +329,10 @@ func BenchmarkMapStringQuery(b *testing.B) { if err != nil { b.Error(err) } - /*for k, v := range m { - fmt.Printf("%v - %v\n", k, v) - }*/ + if m["name"] != "xlw" { + fmt.Println(m) + b.Error(errors.New("name should be xlw")) + } } rows.Close()