github.com/niubaoshu/gotiny@v0.0.4-0.20211018120156-10d393f19ad0/profile/profile.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"math/rand"
     7  	"os"
     8  	"reflect"
     9  	"runtime/pprof"
    10  	"time"
    11  
    12  	"github.com/niubaoshu/gotiny"
    13  	"github.com/niubaoshu/goutils"
    14  )
    15  
    16  type str struct {
    17  	A map[int]map[int]string
    18  	B []bool
    19  	c int
    20  }
    21  
    22  type ET0 struct {
    23  	s str
    24  	F map[int]map[int]string
    25  }
    26  
    27  var (
    28  	//_   = rand.Intn(1)
    29  	now = time.Now()
    30  	a   = "234234"
    31  	i   = map[int]map[int]string{
    32  		1: map[int]string{
    33  			1: a,
    34  		},
    35  	}
    36  	strs = `抵制西方的司法独立,有什么错?有人说马克思主义还是西方的,有本事别用啊。这都是犯了形而上学的错误,任何理论、思想都必须和中国国情相结合,和当前实际相结合。全部照搬照抄的教条主义王明已经试过一次,结果怎么样?歪解周强讲话,不是蠢就是别有用心,蠢的可以教育,别有用心就该打倒`
    37  	st   = str{A: i, B: []bool{true, false, false, false, false, true, true, false, true, false, true}, c: 234234}
    38  	//st     = str{c: 234234}
    39  	et0      = ET0{s: st, F: i}
    40  	stp      = &st
    41  	stpp     = &stp
    42  	nilslice []byte
    43  	slice    = []byte{1, 2, 3}
    44  	mapt     = map[int]int{0: 1, 1: 2, 2: 3, 3: 4}
    45  	nilmap   map[int][]byte
    46  	nilptr   *map[int][]string
    47  	inta          = 2
    48  	ptrint   *int = &inta
    49  	nilint   *int
    50  	vs       = []interface{}{
    51  		ptrint,
    52  		strs,
    53  		`习近平离京对瑞士联邦进行国事访问
    54  		出席世界经济论坛2017年年会并访问在瑞士的国际组织
    55  		新华社北京1月15日电1月15日上午,国家主席习近平乘专机离开北京,应以洛伊特哈德为主席的瑞士联邦委员会邀请,对瑞士进行国事访问;应世界经济论坛创始人兼执行主席施瓦布邀请,出席在达沃斯举行的世界经济论坛2017年年会;应联合国秘书长古特雷斯、世界卫生组织总干事陈冯富珍、国际奥林匹克委员会主席巴赫邀请,访问联合国日内瓦总部、世界卫生组织、国际奥林匹克委员会。
    56  		陪同习近平出访的有:习近平主席夫人彭丽媛,中共中央政治局委员、中央政策研究室主任王沪宁,中共中央政治局委员、中央书记处书记、中央办公厅主任栗战书,国务委员杨洁篪等。返回腾讯网首页>>`,
    57  		true,
    58  		false,
    59  		int(123456),
    60  		int8(123),
    61  		int16(-12345),
    62  		int32(123456),
    63  		int64(-1234567),
    64  		int64(1<<63 - 1),
    65  		int64(rand.Int63()),
    66  		uint(123),
    67  		uint8(123),
    68  		uint16(12345),
    69  		uint32(123456),
    70  		uint64(1234567),
    71  		uint64(1<<64 - 1),
    72  		uint64(rand.Uint32() * rand.Uint32()),
    73  		uintptr(12345678),
    74  		float32(1.2345),
    75  		float64(1.2345678),
    76  		complex64(1.2345 + 2.3456i),
    77  		complex128(1.2345678 + 2.3456789i),
    78  		string("hello,日本国"),
    79  		string("9b899bec35bc6bb8"),
    80  		inta,
    81  		[][][][3][][3]int{{{{{{2, 3}}}}}},
    82  		map[int]map[int]map[int]map[int]map[int]map[int]map[int]map[int]int{1: {1: {1: {1: {1: {1: {1: {1: 2}}}}}}}},
    83  		map[int]map[int]int{1: {2: 3}},
    84  		[][]bool{},
    85  		[]byte("hello,中国人"),
    86  		[][]byte{[]byte("hello"), []byte("world")},
    87  		[4]string{"2324", "23423", "捉鬼", "《:LSESERsef色粉色问问我二维牛"},
    88  		map[int]string{1: "h", 2: "h", 3: "nihao"},
    89  		map[string]map[int]string{"werwer": {1: "呼呼喊喊"}, "汉字": {2: "世界"}},
    90  		a,
    91  		i,
    92  		&i,
    93  		st,
    94  		stp,
    95  		stpp,
    96  		struct{}{},
    97  		[][][]struct{}{},
    98  		struct {
    99  			a, C int
   100  		}{1, 2},
   101  		et0,
   102  		[100]int{},
   103  		now,
   104  		ptrint,
   105  		nilmap,
   106  		nilslice,
   107  		nilptr,
   108  		nilint,
   109  		slice,
   110  		mapt,
   111  	}
   112  	e = gotiny.NewEncoder(vs...)
   113  	d = gotiny.NewDecoder(vs...)
   114  
   115  	spvals = make([]interface{}, len(vs))
   116  	rpvals = make([]interface{}, len(vs))
   117  	c      = goutils.NewComparer()
   118  
   119  	buf = make([]byte, 0, 2048)
   120  )
   121  
   122  func init() {
   123  
   124  	for i := 0; i < len(vs); i++ {
   125  		typ := reflect.TypeOf(vs[i])
   126  		temp := reflect.New(typ)
   127  		temp.Elem().Set(reflect.ValueOf(vs[i]))
   128  		spvals[i] = temp.Interface()
   129  
   130  		if i == len(vs)-2 {
   131  			a := make([]byte, 15)
   132  			rpvals[i] = &a
   133  		} else if i == len(vs)-1 {
   134  			//a := map[int]int{111: 233, 6: 7}
   135  			a := map[int]int{}
   136  			rpvals[i] = &a
   137  		} else {
   138  			rpvals[i] = reflect.New(typ).Interface()
   139  		}
   140  	}
   141  	e.AppendTo(buf[:0])
   142  }
   143  
   144  func main() {
   145  	f, err := os.Create("cpuprofile.pprof")
   146  	if err != nil {
   147  		log.Fatal(err)
   148  	}
   149  	defer f.Close()
   150  	pprof.StartCPUProfile(f)
   151  	defer pprof.StopCPUProfile()
   152  	for i := 0; i < 1000; i++ {
   153  		for i := 0; i < 1000; i++ {
   154  			e.AppendTo(buf[:0])
   155  			d.Decode(e.Encode(spvals...), rpvals...)
   156  			for i, result := range rpvals {
   157  				r := reflect.ValueOf(result).Elem().Interface()
   158  				if Assert(vs[i], r) != nil {
   159  					fmt.Println(err)
   160  				}
   161  			}
   162  		}
   163  	}
   164  }
   165  
   166  func Assert(x, y interface{}) error {
   167  	if !c.DeepEqual(x, y) {
   168  		return fmt.Errorf("\n exp type =  %T; value = %#v;\n got type = %T; value = %#v ", x, x, y, y)
   169  	}
   170  	return nil
   171  }