github.com/junaozun/protoc-gen-go-clone@v0.0.0-20231229025521-89199567f720/cmd/protoc-gen-clone/test/clone_test.go (about)

     1  package test
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/golang/protobuf/proto"
    11  	"github.com/junaozun/protoc-gen-go-clone/cmd/protoc-gen-clone/example"
    12  	"github.com/junaozun/protoc-gen-go-clone/cmd/protoc-gen-clone/example/common"
    13  )
    14  
    15  func TestGenTestPb(t *testing.T) {
    16  	stu := map[int32]*example.StudentMc{
    17  		10: {Name: "xiaoming"},
    18  		20: {Name: "xiaohong"},
    19  	}
    20  
    21  	drawData := map[int32]*example.DrawOnlyEquipBack_DrawData{
    22  		900: {
    23  			SpecialDrawCount: map[string]int32{"son": 10},
    24  			HistoryDrawCount: 88,
    25  			ResId:            3,
    26  		},
    27  	}
    28  	dat := &example.DrawOnlyEquipBack{
    29  		Stu:      stu,
    30  		DrawData: drawData,
    31  		SpecialDrawCount: map[int32]string{
    32  			777: "stru",
    33  			98:  "tea"},
    34  	}
    35  	clone := dat.Clone()
    36  	if !equal(dat, clone) {
    37  		t.Error("not equal")
    38  	}
    39  	clone2 := proto.Clone(dat)
    40  	if !equal(dat, clone2) {
    41  		t.Error("not equal2")
    42  	}
    43  }
    44  
    45  func TestGenUserPb(t *testing.T) {
    46  	dat, err := readSrcData()
    47  	if err != nil {
    48  		t.Error(err)
    49  	}
    50  
    51  	const testNum = 1234567
    52  	dat.User.Base.HideVipSystems = append(dat.User.Base.HideVipSystems, testNum)
    53  
    54  	clone := dat.Clone()
    55  	if clone.User.Base.HideVipSystems[len(clone.User.Base.HideVipSystems)-1] != testNum {
    56  		t.Error("not equal")
    57  	}
    58  
    59  	if !equal(dat, clone) {
    60  		t.Error("not equal")
    61  	}
    62  	clone2 := proto.Clone(dat)
    63  	if !equal(dat, clone2) {
    64  		t.Error("not equal2")
    65  	}
    66  }
    67  
    68  func readSrcData() (*example.User, error) {
    69  	dat := &example.User{}
    70  	dataJsonStruct := readTestDataFile("./user.json")
    71  	// 解析json
    72  	err := json.Unmarshal([]byte(dataJsonStruct), &dat.User)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	return dat, nil
    77  }
    78  
    79  func readTestDataFile(path string) string {
    80  	fi, err := os.Open(path)
    81  	if err != nil {
    82  		panic(err)
    83  	}
    84  	defer fi.Close()
    85  	fd, err := ioutil.ReadAll(fi)
    86  	return string(fd)
    87  }
    88  
    89  func equal(a proto.Message, b proto.Message) bool {
    90  	return proto.Equal(a, b)
    91  }
    92  
    93  func TestValidClone(t *testing.T) {
    94  	originUser, err := readSrcData()
    95  	if err != nil {
    96  		t.Error(err)
    97  	}
    98  	dat := originUser.User
    99  
   100  	clone := originUser.Clone().User
   101  	if !equal(dat, clone) {
   102  		t.Error("not equal")
   103  	}
   104  	// string
   105  	clone.Base.ServerId = 2379438
   106  	if reflect.DeepEqual(clone.Base.ServerId, dat.Base.ServerId) {
   107  		t.Error("string error")
   108  	}
   109  	// map
   110  	clone.Base.Counsellor[763] = 83765
   111  	if reflect.DeepEqual(clone.Base.Counsellor, dat.Base.Counsellor) {
   112  		t.Error("map error")
   113  	}
   114  	// array
   115  	clone.Base.HideVipSystems = append(clone.Base.HideVipSystems, 32637846)
   116  	if reflect.DeepEqual(clone.Base.HideVipSystems, dat.Base.HideVipSystems) {
   117  		t.Error("array error")
   118  	}
   119  	// message
   120  	clone.Game.Rebate = &common.Rebate{TakeTime: proto.Int64(36532)}
   121  	if reflect.DeepEqual(clone.Game.Rebate, dat.Game.Rebate) {
   122  		t.Error("message error")
   123  	}
   124  	// *uint32
   125  	clone.Game.DailyBoss.ActivityAdd = proto.Uint32(782327)
   126  	if reflect.DeepEqual(clone.Game.DailyBoss.ActivityAdd, dat.Game.DailyBoss.ActivityAdd) {
   127  		t.Error("uint32 error")
   128  	}
   129  }
   130  
   131  func BenchmarkCloneDB_WarClone(b *testing.B) {
   132  	dat, err := readSrcData()
   133  	if err != nil {
   134  		b.Error(err)
   135  	}
   136  	b.ResetTimer()
   137  	for i := 0; i < b.N; i++ {
   138  		_ = dat.Clone()
   139  	}
   140  }
   141  
   142  func BenchmarkCloneDB_PbClone(b *testing.B) {
   143  	dat, err := readSrcData()
   144  	if err != nil {
   145  		b.Error(err)
   146  	}
   147  	b.ResetTimer()
   148  	for i := 0; i < b.N; i++ {
   149  		_ = proto.Clone(dat)
   150  	}
   151  }
   152  
   153  func BenchmarkCloneDB_PbMarshal(b *testing.B) {
   154  	dat, err := readSrcData()
   155  	if err != nil {
   156  		b.Error(err)
   157  	}
   158  	b.ResetTimer()
   159  	for i := 0; i < b.N; i++ {
   160  		_, _ = proto.Marshal(dat)
   161  	}
   162  }
   163  
   164  // cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
   165  // BenchmarkCloneDB_WarClone
   166  // BenchmarkCloneDB_WarClone-8    	    1568	    805789 ns/op	  368876 B/op	   11184 allocs/op
   167  // BenchmarkCloneDB_PbClone
   168  // BenchmarkCloneDB_PbClone-8     	     262	   4602689 ns/op	  571961 B/op	   21785 allocs/op
   169  // BenchmarkCloneDB_PbMarshal
   170  // BenchmarkCloneDB_PbMarshal-8   	     100	  12359954 ns/op	  638916 B/op	   32709 allocs/op