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{} rsp := &pb.UserCreateResp{}
code := robot.SendMsg("user", "create", req, rsp) code := robot.SendMsg("user", "create", req, rsp)
if code != pb.ErrorCode_Success { if code == pb.ErrorCode_Success {
return nil return nil
} }
logrus.Debug(rsp) logrus.Debug(rsp)

View File

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

View File

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

View File

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