update数据锁

This commit is contained in:
wh_zcy 2022-12-14 15:15:54 +08:00
parent 693b4c9ac3
commit 0c3791c591
4 changed files with 51 additions and 47 deletions

View File

@ -31,7 +31,7 @@ func (c *CreateUserScene) Run(robot lib.IRobot) error {
rsp := &pb.UserCreateResp{}
code := robot.SendMsg("user", "create", req, rsp)
if code != pb.ErrorCode_Success {
if code == pb.ErrorCode_Success {
return nil
}
logrus.Debug(rsp)

View File

@ -21,8 +21,9 @@ type myAI struct {
iscenes []IScene
tickets Tickets //票池
Obs Observer
// useCount uint32 //计数(压入的用户数)
// useCount uint32 //计数(压入的用户数)
useCountTotal uint32 //总数
cache sync.Mutex //数据缓存锁
lock sync.Mutex //合并数据锁
config *storage.Config //配置
ReportMap map[int]map[string]*Statistics //测试报告 key1:场景 key2:协议
@ -81,7 +82,7 @@ func (m *myAI) Start() bool {
for {
m.tickets.Take(int32(m.config.Global.UserCount), m.config.Global.IntervalS)
go func() {
robot := NewRobot(m.config)
robot := NewRobot(m)
robot.SetScenes(m.iscenes)
m.AppendRobot(robot)
robot.Start()

View File

@ -32,45 +32,48 @@ type IRobot interface {
type Robot struct {
IStore
ai *myAI
scene IScene //场景
account string
sid string
conn *websocket.Conn
data map[string]interface{} //机器人缓存数据
status uint32 //状态
lock sync.Mutex //数据缓存锁
sceneQueue *Queue[IScene] //场景队列
config *storage.Config //配置
resultCh chan *CallResult //请求结果通道
cache sync.Mutex
lock sync.Mutex //结果锁
sceneQueue *Queue[IScene] //场景队列
config *storage.Config //配置
resultCh chan *CallResult //请求结果通道
// sceneResultCh chan *CallResult //场景结果通道
ReportMap map[int]map[string]*Statistics //测试报告 key1:场景序号 key2:协议
elipseTotal time.Duration
}
func NewRobot(config *storage.Config) *Robot {
func NewRobot(ai *myAI) *Robot {
robot := &Robot{
ai: ai,
data: make(map[string]interface{}),
sceneQueue: NewQueue[IScene](),
config: config,
config: ai.config,
resultCh: make(chan *CallResult, 1000),
// sceneResultCh: make(chan *CallResult, 50),
ReportMap: make(map[int]map[string]*Statistics),
}
robot.Store("sid", config.Global.SId)
robot.Store("sid", ai.config.Global.SId)
return robot
}
//存数据
func (a *Robot) Store(key string, data interface{}) {
defer a.lock.Unlock()
a.lock.Lock()
defer a.cache.Unlock()
a.cache.Lock()
a.data[key] = data
}
//取数据
func (a *Robot) Get(key string) interface{} {
defer a.lock.Unlock()
a.lock.Lock()
defer a.cache.Unlock()
a.cache.Lock()
return a.data[key]
}

View File

@ -56,7 +56,7 @@ func TestMerge(t *testing.T) {
}
ma, _ := lib.NewAI(lib.AIParam{Config: config})
robot1 := lib.NewRobot(config)
robot1 := lib.NewRobot(ma)
robot1.ReportMap = make(map[int]map[string]*lib.Statistics)
robot1.ReportMap[1] = make(map[string]*lib.Statistics)
robot1.ReportMap[1]["user.login"] = &lib.Statistics{
@ -70,7 +70,7 @@ func TestMerge(t *testing.T) {
}
ma.AppendRobot(robot1)
///////////////
robot2 := lib.NewRobot(config)
robot2 := lib.NewRobot(ma)
robot2.ReportMap = make(map[int]map[string]*lib.Statistics)
robot2.ReportMap[1] = make(map[string]*lib.Statistics)
robot2.ReportMap[1]["user.login"] = &lib.Statistics{
@ -84,7 +84,7 @@ func TestMerge(t *testing.T) {
}
ma.AppendRobot(robot2)
//////
robot3 := lib.NewRobot(config)
robot3 := lib.NewRobot(ma)
robot3.ReportMap = make(map[int]map[string]*lib.Statistics)
robot3.ReportMap[1] = make(map[string]*lib.Statistics)
robot3.ReportMap[1]["user.login"] = &lib.Statistics{
@ -99,7 +99,7 @@ func TestMerge(t *testing.T) {
ma.AppendRobot(robot3)
ma.MergeResult()
robot4 := lib.NewRobot(config)
robot4 := lib.NewRobot(ma)
robot4.ReportMap = make(map[int]map[string]*lib.Statistics)
robot4.ReportMap[1] = make(map[string]*lib.Statistics)
robot4.ReportMap[1]["user.login"] = &lib.Statistics{
@ -123,38 +123,38 @@ func TestMerge(t *testing.T) {
}
}
func divide(a int, b int) int {
if a== math.MinInt32 && b == -1 {
return math.MaxInt32
}
var sign = 1
if a>0 && b<0 || a<0 && b>0 {
sign = -1
}
if a<0 {
a = -a
}
if b<0 {
b = -b
}
var res int64
for {
if a < b { break }
// a< b ,res = 0
var cur = 1
var temp = b
for temp + temp <= a {
temp += temp
cur += cur
}
res += int64(cur )
a -= temp
}
return int(res*int64(sign))
if a == math.MinInt32 && b == -1 {
return math.MaxInt32
}
var sign = 1
if a > 0 && b < 0 || a < 0 && b > 0 {
sign = -1
}
if a < 0 {
a = -a
}
if b < 0 {
b = -b
}
var res int64
for {
if a < b {
break
}
// a< b ,res = 0
var cur = 1
var temp = b
for temp+temp <= a {
temp += temp
cur += cur
}
res += int64(cur)
a -= temp
}
return int(res * int64(sign))
}
func TestDiv(t *testing.T) {
res := lib.FormatFloatCommon(float64(3 / 10))