测试用例支持递归处理

This commit is contained in:
zhaocy 2022-07-12 16:02:14 +08:00
parent bd548b5ff5
commit c046fe1b12
19 changed files with 143 additions and 175 deletions

View File

@ -4,21 +4,22 @@ import (
"go_dreamfactory/comm"
"go_dreamfactory/modules/friend"
"go_dreamfactory/pb"
"google.golang.org/protobuf/proto"
)
var (
//friend
friendBuilders = []*TestCase{
{
//list
Desc: "好友列表",
desc: "好友列表",
mainType: string(comm.ModuleFriend),
subType: friend.FriendSubTypeList,
req: &pb.FriendListReq{},
rsp: &pb.FriendListResp{},
// enabled: true,
}, {
//blacklist
desc: "黑名单列表",
mainType: string(comm.ModuleFriend),
subType: friend.FriendSubTypeBlacklist,
req: &pb.FriendBlackListReq{},
@ -34,23 +35,23 @@ var (
rsp: &pb.FriendSearchResp{},
// enabled: true,
}, {
//apply
desc: "好友申请",
mainType: string(comm.ModuleFriend),
subType: friend.FriendSubTypeApply,
req: &pb.FriendApplyReq{
FriendId: "0_62c28ab569b7d416cf9eb8c7",
FriendId: "0_62ccd236e799cee5e7c97930",
},
rsp: &pb.FriendApplyResp{},
// enabled: true,
}, {
//applylist
desc: "申请列表",
mainType: string(comm.ModuleFriend),
subType: friend.FriendSubTypeApplyList,
req: &pb.FriendApplyListReq{},
rsp: &pb.FriendApplyListResp{},
// enabled: true,
}, {
//agree
desc: "同意",
mainType: string(comm.ModuleFriend),
subType: friend.FriendSubTypeAgree,
req: &pb.FriendAgreeReq{
@ -59,25 +60,39 @@ var (
rsp: &pb.FriendAgreeResp{},
// enabled: true,
}, {
//refuse
desc: "拒绝",
mainType: string(comm.ModuleFriend),
subType: friend.FriendSubTypeRefuse,
req: &pb.FriendRefuseReq{
FriendIds: []string{"0_62c28bcb69b7d416cf9eb8c9"},
},
rsp: &pb.FriendRefuseResp{},
rsp: &pb.FriendRefuseResp{},
// enabled: true,
}, {
//addblack
desc: "添加黑名单",
mainType: string(comm.ModuleFriend),
subType: friend.FriendSubTypeAddBlack,
req: &pb.FriendBlackAddReq{
FriendId: "0_62c26323e5cc5577d41bf591",
FriendId: "0_62ccd236e799cee5e7c97930",
},
rsp: &pb.FriendBlackAddResp{},
// enabled: true,
next: func(rsp proto.Message, handle func(nextCase *TestCase)) {
if r, ok := rsp.(*pb.FriendBlackAddResp); ok {
handle(&TestCase{
desc: "删除黑名单",
mainType: string(comm.ModuleFriend),
subType: friend.FriendSubTypeDelBlack,
req: &pb.FriendDelBlackReq{
FriendId: r.FriendId,
},
rsp: &pb.FriendDelBlackResp{},
enabled: true,
})
}
},
}, {
//delblack
desc: "删除黑名单",
mainType: string(comm.ModuleFriend),
subType: friend.FriendSubTypeDelBlack,
req: &pb.FriendDelBlackReq{},
@ -90,5 +105,5 @@ var (
//声明加入到构建器并发起请求
func (r *Robot) RunFriend() {
r.addBuilders(friendBuilders)
r.handleReq()
r.batchHandleReq()
}

View File

@ -13,7 +13,7 @@ var (
//hero
heroBuilders = []*TestCase{
{
Desc: "英雄列表",
desc: "英雄列表",
mainType: string(comm.ModuleHero),
subType: hero.HeroSubTypeList,
req: &pb.HeroListReq{},
@ -24,9 +24,9 @@ var (
fmt.Printf("%d- %v\n", (i + 1), v)
}
},
enabled: true,
// enabled: true,
}, {
Desc: "英雄详情",
desc: "英雄详情",
mainType: string(comm.ModuleHero),
subType: hero.HeroSubTypeInfo,
req: &pb.HeroInfoReq{
@ -35,13 +35,13 @@ var (
rsp: &pb.HeroInfoResp{},
// enabled: true,
}, {
Desc: "抽卡",
desc: "抽卡",
mainType: string(comm.ModuleHero),
subType: hero.HeroSubTypeChouka,
req: &pb.HeroChoukaReq{
HeroIds: []int32{42911},
},
rsp: &pb.HeroChoukaResp{},
rsp: &pb.HeroChoukaResp{},
// enabled: true,
}, {
mainType: string(comm.ModuleHero),
@ -73,5 +73,5 @@ var (
//声明加入到构建器并发起请求
func (r *Robot) RunHero() {
r.addBuilders(heroBuilders)
r.handleReq()
r.batchHandleReq()
}

View File

@ -91,5 +91,5 @@ func (this *LinkCase) Insert(index int, testCase *TestCase) {
}
func (this *CaseNode) String() string {
return fmt.Sprintf("value=%v", this.testCase.Desc)
return fmt.Sprintf("value=%v", this.testCase.desc)
}

View File

@ -1,32 +0,0 @@
package robot_test
import (
"fmt"
"go_dreamfactory/cmd/robot"
"sync"
"testing"
)
func TestJob(t *testing.T) {
// link := robot.NewLinkCase()
link := robot.NewDefault(&robot.TestCase{Desc: "login"})
link.Append(&robot.TestCase{Desc: "aa"})
// link.Append(&robot.TestCase{Desc: "bb"})
// link.Unshift(&robot.TestCase{Desc: "cc"})
// link.Unshift(&robot.TestCase{Desc: "dd"})
// link.Insert(1, &robot.TestCase{Desc: "ee"})
cur := link.Head
for cur != nil {
fmt.Println(cur)
cur = cur.Next
}
}
func TestGJo(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
}

View File

@ -43,10 +43,10 @@ func (r *Robot) AccountLogin() {
Account: r.opts.Account,
Sid: r.opts.ServerId,
},
rsp: &pb.UserLoginResp{},
rsp: &pb.UserLoginResp{},
enabled: true,
},
}
r.addBuilders(builders)
r.handleReq()
r.batchHandleReq()
}

View File

@ -12,7 +12,7 @@ var pack_builders = []*TestCase{
mainType: string(comm.ModulePack),
subType: "queryuserpackreq",
// req: &pb.Pack_Getlist_Req{IType: 1},
rsp: &pb.UserCreateResp{},
rsp: &pb.UserCreateResp{},
enabled: true,
},
}
@ -20,5 +20,5 @@ var pack_builders = []*TestCase{
//声明加入到构建器并发起请求
func (r *Robot) RunPack() {
r.addBuilders(pack_builders)
r.handleReq()
r.batchHandleReq()
}

View File

@ -65,7 +65,7 @@ func (r *Robot) Run() {
if err = proto.Unmarshal(data, msg); err != nil {
log.Fatal(err)
}
r.handleRsp(msg)
r.batchhandleRsp(msg)
}
}()
@ -74,7 +74,7 @@ func (r *Robot) Run() {
}
type TestCase struct {
Desc string
desc string
mainType string
subType string
req proto.Message
@ -83,6 +83,7 @@ type TestCase struct {
start time.Time
requested bool //请求标识 true已发
print func(rsp proto.Message) //定义打印
next func(rsp proto.Message, handle func(nextCase *TestCase))
}
func (r *Robot) addBuilders(builders []*TestCase) {
@ -93,26 +94,30 @@ func (r *Robot) addBuilders(builders []*TestCase) {
}
}
func (r *Robot) handleReq(b *TestCase) {
b.requested = true
b.start = time.Now()
head := &pb.UserMessage{MainType: b.mainType, SubType: b.subType}
defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), b.req)
err := r.SendToClient(head, b.req)
if err != nil {
log.Fatal(err)
}
}
//执行请求
func (r *Robot) handleReq() {
func (r *Robot) batchHandleReq() {
for _, b := range r.builders {
if b.req != nil && !b.requested {
r.wg.Add(1)
time.Sleep(time.Second * 1)
b.requested = true
b.start = time.Now()
head := &pb.UserMessage{MainType: b.mainType, SubType: b.subType}
defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), b.req)
err := r.SendToClient(head, b.req)
if err != nil {
log.Fatal(err)
}
r.handleReq(b)
}
}
}
//执行响应
func (r *Robot) handleRsp(msg *pb.UserMessage) {
func (r *Robot) batchhandleRsp(msg *pb.UserMessage) {
for i, b := range r.builders {
if b.enabled && (msg.MainType == b.mainType &&
msg.SubType == b.subType) {
@ -122,11 +127,15 @@ func (r *Robot) handleRsp(msg *pb.UserMessage) {
if b.print == nil {
printReply(msg, b)
} else {
fmt.Printf("===== %s [%s.%s] =====\n", b.Desc, msg.MainType, msg.SubType)
fmt.Printf("===== %s [%s.%s] =====\n", b.desc, msg.MainType, msg.SubType)
b.print(b.rsp)
fmt.Println("==============================")
}
if b.next != nil {
b.next(b.rsp, r.handleReq)
}
if msg.MainType == "user" && msg.SubType == "login" {
r.loginCallback(b.rsp)
} else {
@ -212,7 +221,7 @@ func (r *Robot) AccountRegister(account string, sid int32) {
//登录
var user_builders = []*TestCase{
{
Desc: "登录",
desc: "登录",
mainType: "user",
subType: "login",
req: &pb.UserLoginReq{
@ -224,7 +233,7 @@ func (r *Robot) AccountRegister(account string, sid int32) {
},
}
r.addBuilders(user_builders)
r.handleReq()
r.batchHandleReq()
}
}
@ -237,9 +246,9 @@ func printReply(msg *pb.UserMessage, builder *TestCase) {
} else {
tt = time.Since(builder.start)
}
log.Printf("rsp %s [%v] [%s.%s] [%v:%v]", builder.Desc, tt, m.ReqMainType, m.ReqSubType, int32(m.Code), m.Data)
log.Printf("rsp %s [%v] [%s.%s] [%v:%v]", builder.desc, tt, m.ReqMainType, m.ReqSubType, int32(m.Code), m.Data)
} else {
log.Printf("rsp %s [%v] [%s.%s] [%v]", builder.Desc, time.Since(builder.start), msg.MainType, msg.SubType, builder.rsp)
log.Printf("rsp %s [%v] [%s.%s] [%v]", builder.desc, time.Since(builder.start), msg.MainType, msg.SubType, builder.rsp)
}
}

View File

@ -13,7 +13,7 @@ var (
taskBuilders = []*TestCase{
{
//list
Desc: "任务列表",
desc: "任务列表",
mainType: string(comm.ModuleTask),
subType: task.TaskSubTypeList,
req: &pb.TaskListReq{
@ -28,7 +28,7 @@ var (
},
// enabled: true,
}, {
Desc: "领取任务奖励",
desc: "领取任务奖励",
mainType: string(comm.ModuleTask),
subType: task.TaskSubTypeReceive,
req: &pb.TaskReceiveReq{
@ -38,7 +38,7 @@ var (
rsp: &pb.TaskReceiveResp{},
// enabled: true,
}, {
Desc: "活跃度",
desc: "活跃度",
mainType: string(comm.ModuleTask),
subType: task.TaskSubTypeActiveList,
req: &pb.TaskActiveListReq{
@ -47,14 +47,14 @@ var (
rsp: &pb.TaskActiveListResp{},
// enabled: true,
}, {
Desc: "活跃度领取",
desc: "活跃度领取",
mainType: string(comm.ModuleTask),
subType: task.TaskSubTypeActiveReceive,
req: &pb.TaskActiveReceiveReq{
Id: "62c676d57deea8b9af8884fb",
TaskTag: int32(comm.TASK_DAILY),
},
rsp: &pb.TaskActiveReceiveResp{},
rsp: &pb.TaskActiveReceiveResp{},
// enabled: true,
},
}
@ -63,5 +63,5 @@ var (
//声明加入到构建器并发起请求
func (r *Robot) RunTask() {
r.addBuilders(taskBuilders)
r.handleReq()
r.batchHandleReq()
}

View File

@ -10,7 +10,7 @@ import (
var user_builders = []*TestCase{
{
//create
Desc: "创角",
desc: "创角",
mainType: string(comm.ModuleUser),
subType: user.UserSubTypeCreate,
req: &pb.UserCreateReq{ //设置请求参数
@ -19,13 +19,13 @@ var user_builders = []*TestCase{
rsp: &pb.UserCreateResp{},
// enabled: true,
}, {
Desc: "添加资源",
desc: "添加资源",
mainType: string(comm.ModuleUser),
subType: user.UserSubTypeAddRes,
req: &pb.UserAddResReq{ //设置请求参数
Res: &pb.UserAssets{
A: comm.ResGold,
T:"",
T: "",
N: 100,
},
},
@ -37,5 +37,5 @@ var user_builders = []*TestCase{
//声明加入到构建器并发起请求
func (r *Robot) RunUser() {
r.addBuilders(user_builders)
r.handleReq()
r.batchHandleReq()
}

View File

@ -20,7 +20,7 @@ func (this *apiComp) Addblack(session comm.IUserSession, req *pb.FriendBlackAddR
if code = this.AddblackCheck(session, req); code != pb.ErrorCode_Success {
return
}
var (
err error
self *pb.DBFriend
@ -61,7 +61,7 @@ func (this *apiComp) Addblack(session comm.IUserSession, req *pb.FriendBlackAddR
}
// 判断自己是否在对方的黑名单中
if _, ok := utils.Find(target.BlackIds, self.UId); ok {
if _, ok := utils.Find(target.BlackIds, self.Uid); ok {
code = pb.ErrorCode_FriendTargetBlackYet
return
}
@ -76,7 +76,7 @@ func (this *apiComp) Addblack(session comm.IUserSession, req *pb.FriendBlackAddR
self.BlackIds = append(self.BlackIds, req.FriendId)
//更新黑名单
err = this.module.modelFriend.Change(self.UId, map[string]interface{}{
err = this.module.modelFriend.Change(self.Uid, map[string]interface{}{
"blackIds": self.BlackIds,
})
if err != nil {

View File

@ -71,13 +71,13 @@ func (this *apiComp) Agree(session comm.IUserSession, req *pb.FriendAgreeReq) (c
code = pb.ErrorCode_FriendTargetNoData
return
}
if _, ok := utils.Find(target.FriendIds, self.UId); !ok {
if _, ok := utils.Find(target.FriendIds, self.Uid); !ok {
if target.FriendIds == nil {
target.FriendIds = []string{}
}
target.FriendIds = append(target.FriendIds, self.UId)
target.FriendIds = append(target.FriendIds, self.Uid)
}
err = this.module.modelFriend.Change(target.UId, map[string]interface{}{
err = this.module.modelFriend.Change(target.Uid, map[string]interface{}{
"friendIds": target.FriendIds,
})
if err != nil {
@ -90,7 +90,7 @@ func (this *apiComp) Agree(session comm.IUserSession, req *pb.FriendAgreeReq) (c
}
//更新
err = this.module.modelFriend.Change(self.UId, map[string]interface{}{
err = this.module.modelFriend.Change(self.Uid, map[string]interface{}{
"applyIds": self.ApplyIds,
"friendIds": self.FriendIds,
})

View File

@ -86,7 +86,7 @@ func (this *apiComp) Apply(session comm.IUserSession, req *pb.FriendApplyReq) (c
}
//判断自己是否在目标用户的申请列表中
if _, ok := utils.Find(target.ApplyIds, self.UId); ok {
if _, ok := utils.Find(target.ApplyIds, self.Uid); ok {
code = pb.ErrorCode_FriendApplyYet
return
}
@ -98,7 +98,7 @@ func (this *apiComp) Apply(session comm.IUserSession, req *pb.FriendApplyReq) (c
}
//判断是否在对方的黑名单中
if _, ok := utils.Find(target.BlackIds, self.UId); ok {
if _, ok := utils.Find(target.BlackIds, self.Uid); ok {
code = pb.ErrorCode_FriendTargetBlackYet
return
}

View File

@ -3,13 +3,22 @@ package friend
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"google.golang.org/protobuf/proto"
)
func (this *apiComp) DelCheck(session comm.IUserSession, req *pb.FriendDelReq) (chk map[string]interface{}, code comm.ErrorCode) {
func (this *apiComp) DelCheck(session comm.IUserSession, req *pb.FriendDelReq) (code pb.ErrorCode) {
if req.FriendId == "" {
code = pb.ErrorCode_ReqParameterError
}
return
}
//删除好友
func (this *apiComp) Del(session comm.IUserSession, chk map[string]interface{}, req *pb.FriendDelReq) error {
return nil
func (this *apiComp) Del(session comm.IUserSession, chk map[string]interface{}, req *pb.FriendDelReq) (code pb.ErrorCode, data proto.Message) {
if code = this.DelCheck(session, req); code != pb.ErrorCode_Success {
return
}
return
}

View File

@ -48,7 +48,7 @@ func (this *apiComp) Delblack(session comm.IUserSession, req *pb.FriendDelBlackR
//从黑名单列表中删除目标
self.BlackIds = utils.DeleteString(self.BlackIds, req.FriendId)
//更新黑名单
err = this.module.modelFriend.Change(self.UId, map[string]interface{}{
err = this.module.modelFriend.Change(self.Uid, map[string]interface{}{
"blackIds": self.BlackIds,
})
if err != nil {

View File

@ -61,7 +61,7 @@ func (this *apiComp) Refuse(session comm.IUserSession, req *pb.FriendRefuseReq)
}
//更新
if optNum > 0 {
err := this.module.modelFriend.Change(self.UId, map[string]interface{}{
err := this.module.modelFriend.Change(self.Uid, map[string]interface{}{
"applyIds": self.ApplyIds,
})
if err != nil {

View File

@ -8,6 +8,7 @@ import (
"go_dreamfactory/pb"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
const (
@ -39,9 +40,10 @@ func (this *ModelFriend) Frined_FindCond(nickName string) *pb.DBUser {
//查询好友
func (this *ModelFriend) GetFriend(uid string) *pb.DBFriend {
friend := &pb.DBFriend{UId: uid}
if err := this.Get(uid, friend); err != nil {
if err == redis.RedisNil {
friend := &pb.DBFriend{Uid: uid}
err := this.Get(uid, friend)
if err != nil {
if err == redis.RedisNil || err == mongo.ErrNoDocuments {
if err := this.Add(uid, friend); err != nil {
return nil
}

View File

@ -25,7 +25,7 @@ type DBFriend struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
UId string `protobuf:"bytes,1,opt,name=uId,proto3" json:"uId" bson:"uId"` //用户ID
Uid string `protobuf:"bytes,1,opt,name=uid,proto3" json:"uid" bson:"uid"` //用户ID
FriendIds []string `protobuf:"bytes,2,rep,name=friendIds,proto3" json:"friendIds" bson:"friendIds"` //好友ID
ApplyIds []string `protobuf:"bytes,3,rep,name=applyIds,proto3" json:"applyIds" bson:"applyIds"` //申请用户ID
BlackIds []string `protobuf:"bytes,4,rep,name=blackIds,proto3" json:"blackIds" bson:"blackIds"` //黑名单ID
@ -63,9 +63,9 @@ func (*DBFriend) Descriptor() ([]byte, []int) {
return file_friend_friend_db_proto_rawDescGZIP(), []int{0}
}
func (x *DBFriend) GetUId() string {
func (x *DBFriend) GetUid() string {
if x != nil {
return x.UId
return x.Uid
}
return ""
}
@ -96,8 +96,8 @@ var File_friend_friend_db_proto protoreflect.FileDescriptor
var file_friend_friend_db_proto_rawDesc = []byte{
0x0a, 0x16, 0x66, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x2f, 0x66, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x5f,
0x64, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x72, 0x0a, 0x08, 0x44, 0x42, 0x46, 0x72,
0x69, 0x65, 0x6e, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x03, 0x75, 0x49, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x66, 0x72, 0x69, 0x65, 0x6e, 0x64,
0x69, 0x65, 0x6e, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x66, 0x72, 0x69, 0x65, 0x6e, 0x64,
0x49, 0x64, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x09, 0x66, 0x72, 0x69, 0x65, 0x6e,
0x64, 0x49, 0x64, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x61, 0x70, 0x70, 0x6c, 0x79, 0x49, 0x64, 0x73,
0x18, 0x03, 0x20, 0x03, 0x28, 0x09, 0x52, 0x08, 0x61, 0x70, 0x70, 0x6c, 0x79, 0x49, 0x64, 0x73,

View File

@ -2,7 +2,7 @@ syntax = "proto3";
option go_package = ".;pb";
message DBFriend {
string uId = 1; //@go_tags(`bson:"uId"`) ID
string uid = 1; //@go_tags(`bson:"uid"`) ID
repeated string friendIds = 2; //@go_tags(`bson:"friendIds"`) ID
repeated string applyIds = 3; //@go_tags(`bson:"applyIds"`) ID
repeated string blackIds = 4; //@go_tags(`bson:"blackIds"`) ID

View File

@ -3,7 +3,6 @@ package bench
import (
"context"
"fmt"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/sys/db"
"log"
@ -51,33 +50,31 @@ func TestMain(m *testing.M) {
}
func BenchmarkMarsh(b *testing.B) {
var (
// ncpu = runtime.NumCPU()
parallelisms = []int{2} //平行数
dataSizes = []int{10, 100, 1000} //测试次数
builders = []TargetBuilder{
// {
// Name: "gen",
// Make: func(bench Benchmark) (Target, error) {
// return Target{
// GetHeroList: func() (any, error) {
// // heroes := GetList3(new(DBHero))
// heroes := []*DBHero{}
// GetList(&heroes)
// return heroes, nil
// },
// }, nil
// },
// },
{
Name: "gen",
Make: func(bench Benchmark) (Target, error) {
return Target{
GetHeroList: func() (any, error) {
// heroes := GetList3(new(DBHero))
heroes := []*DBHero{}
GetList(&heroes)
return heroes, nil
},
}, nil
},
},
{
Name: "reflect",
Name: "Decode",
Make: func(bench Benchmark) (Target, error) {
return Target{
GetHeroList: func() (interface{}, error) {
heroes := []*DBHero{}
GetListO(&heroes)
return heroes, nil
GetListByDecode()
return nil, nil
},
}, nil
},
@ -146,60 +143,27 @@ func GetList(data interface{}) (err error) {
return err
}
func GetListO(data interface{}) (err error) {
var c *mongo.Cursor
t := reflect.TypeOf(data)
if t.Kind() == reflect.Ptr {
t = t.Elem()
func GetListByDecode() (err error) {
// t2 := time.Now()
c, err := mgoDb.Collection("hero").Find(context.Background(), bson.M{"_id": "62ccd2670af3660e58e1730e"})
if err != nil {
fmt.Println(err)
}
defer c.Close(context.Background())
// fmt.Println(time.Since(t2))
if t.Kind() == reflect.Slice {
t = t.Elem()
} else {
err = fmt.Errorf("Input param is not a slice")
}
sl := reflect.ValueOf(data)
if t.Kind() == reflect.Ptr {
sl = sl.Elem()
}
st := sl.Type()
sliceType := st.Elem()
if sliceType.Kind() == reflect.Ptr {
sliceType = sliceType.Elem()
}
// t := time.Now()
//query from mgo
if c, err = mdb.Mgo().Find(core.SqlTable("hero"), bson.M{}); err != nil {
// if c, err = mgoDb.Collection("hreo").Find(context.Background(), bson.M{}); err != nil {
return err
} else {
var temp map[string]interface{} = make(map[string]interface{})
for c.Next(context.Background()) {
_id := c.Current.Lookup("_id").StringValue()
if sl.Len() < sl.Cap() {
sl.Set(sl.Slice(0, sl.Len()+1))
elem := sl.Index(sl.Len() - 1)
if elem.IsNil() {
elem.Set(reflect.New(sliceType))
}
if err = c.Decode(elem.Elem().Addr().Interface()); err != nil {
return
}
temp[_id] = elem.Elem().Addr().Interface()
continue
}
elem := reflect.New(sliceType)
sl.Set(reflect.Append(sl, elem))
if err = c.Decode(elem.Elem().Addr().Interface()); err != nil {
return
}
temp[_id] = elem.Elem().Addr().Interface()
}
if len(temp) == 0 { //没有数据自己返回
return
for c.Next(context.Background()) {
// go func(c1 *mongo.Cursor) {
var hero *DBHero
err2 := c.Decode(&hero)
if err2 != nil {
fmt.Println(err2)
}
// }(c)
}
// fmt.Printf("%v ", time.Since(t))
return err
}
@ -270,7 +234,8 @@ func BenchmarkSimple(b *testing.B) {
// f, _ := os.Create("c://pprof2")
// pprof.WriteHeapProfile(f)
// defer f.Close()
for i := 0; i < b.N; i++ {
GetList4(new(DBHero))
GetListByDecode()
}
}