1 Star 0 Fork 0

Cruvie Kang / kk_go_kit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
sync.Pool.go 3.03 KB
一键复制 编辑 原始数据 按行查看 历史
Cruvie 提交于 2023-11-23 19:22 . rebuild
package kk_x
import (
"sync"
"testing"
"time"
)
type Hub struct {
clientPool sync.Pool
}
type Client struct {
Username string `gorm:"column:username;type:varchar(100);not null;unique" json:"username"`
RealName string `gorm:"column:real_name;type:varchar(100);default:'';not null" json:"real_name"`
Password string `gorm:"column:password;type:varchar(100);not null" json:"password"`
TelNum string `gorm:"column:tel_num;type:varchar(100);default:'';not null" json:"tel_num"`
Notes string `gorm:"column:notes;type:varchar(255);default:'';not null" json:"notes"`
Gender int32 `gorm:"column:gender;type:smallint;default:0;not null" json:"gender"` //0 女 1 男
IdCard string `gorm:"column:id_card;type:varchar(100);default:'';not null" json:"id_card"`
RealNameAuthenticationWay int32 `gorm:"column:real_name_authentication_way;type:smallint;default:0;not null" json:"real_name_authentication_way"`
Height float32 `gorm:"column:height;type:numeric(5,2);default:0.0;not null" json:"height"` // 身高
Weight float32 `gorm:"column:weight;type:numeric(5,2);default:0.0;not null" json:"weight"` // 体重
Nickname string `gorm:"column:nickname;type:varchar(100);default:'';not null" json:"nickname"` // 昵称
Birthday time.Time `gorm:"column:birthday;type:date;not null" json:"birthday"` // 生日
Labels string `gorm:"column:labels;type:varchar(500);default:'';not null" json:"labels"` // 个人标签
Introduction string `gorm:"column:introduction;type:varchar(500);default:'';not null" json:"introduction"` // 简介
Avatar string `gorm:"column:avatar;type:varchar(500);default:'';not null" json:"avatar"` // 头像
}
func NewHub() *Hub {
return &Hub{
clientPool: sync.Pool{
New: func() any {
return &Client{}
},
},
}
}
func (h *Hub) GetClient() *Client {
return h.clientPool.Get().(*Client)
}
func (h *Hub) PutClient(c *Client) {
h.clientPool.Put(c)
}
//func TestName(t *testing.T) {
// h := NewHub()
// for i := 0; i < 10; i++ {
// c1 := h.GetClient()
// log.Println(c1)
// h.PutClient(c1)
// }
//}
func Benchmark_Add(b *testing.B) {
h := NewHub()
for i := 0; i < b.N; i++ {
c1 := h.GetClient()
c1.Username = "username"
//log.Println(c1)
h.PutClient(c1)
}
}
func Benchmark_Add2(b *testing.B) {
for i := 0; i < b.N; i++ {
c1 := &Client{}
c1.Username = "username"
_ = c1
}
}
//go test -bench=. -benchmem sync_test.go
//cruvie@CruvieMac sync_t % go test -bench=. -benchmem sync_test.go
//goos: darwin
//goarch: arm64
//Benchmark_Add-8 147627990 7.897 ns/op 0 B/op 0 allocs/op
//Benchmark_Add2-8 1000000000 0.2918 ns/op 0 B/op 0 allocs/op
//PASS
//ok command-line-arguments 2.470s
1
https://gitee.com/cruvie/kk_go_kit.git
git@gitee.com:cruvie/kk_go_kit.git
cruvie
kk_go_kit
kk_go_kit
cfc94563c7c9

搜索帮助