airobot/test/robot_test.go
2022-12-16 11:09:19 +08:00

156 lines
2.9 KiB
Go

package test
import (
"fmt"
"math"
"testing"
"time"
"legu.airobot/lib"
"legu.airobot/storage"
)
func TestA(t *testing.T) {
q := lib.NewQueue[int]()
q.Add(1)
q.Add(2)
// list := q.List()
for {
i, err := q.Pop()
if err != nil {
t.Error(err)
return
}
fmt.Println(i)
}
}
func TestMerge(t *testing.T) {
config := &storage.Config{
Global: &storage.Global{UserCountTotal: 2, SId: "dfz", UserCount: 1},
}
ma, _ := lib.NewAI(lib.AIParam{Config: 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{
ElapseTotal: 5,
CallCount: 2,
AvgElapse: 2,
MaxElapse: 4,
MinElapse: 2,
Route: "user.login",
SceneName: "登录",
}
ma.AppendRobot(robot1)
///////////////
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{
ElapseTotal: 7,
CallCount: 1,
AvgElapse: 3,
MaxElapse: 5,
MinElapse: 4,
Route: "user.login",
SceneName: "登录",
}
ma.AppendRobot(robot2)
//////
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{
ElapseTotal: 8,
CallCount: 1,
AvgElapse: 2,
MaxElapse: 6,
MinElapse: 3,
Route: "user.login",
SceneName: "登录",
}
ma.AppendRobot(robot3)
ma.MergeResult()
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{
ElapseTotal: 8,
CallCount: 1,
AvgElapse: 2,
MaxElapse: 8,
MinElapse: 2,
Route: "user.login",
SceneName: "登录",
}
ma.AppendRobot(robot4)
ma.MergeResult()
fmt.Println(ma.ReportMap)
for i, v := range ma.ReportMap {
fmt.Println(i)
for j, m := range v {
fmt.Printf("【%s】 调用次数:%v 总耗时:%v 平均:%v 最大:%v 最小:%v\n", j, m.CallCount, m.ElapseTotal, m.AvgElapse, m.MaxElapse, m.MinElapse)
}
}
}
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))
}
func TestStop(t *testing.T) {
timeout := func() {
fmt.Println("超时了")
}
read := func() {
time.Sleep(time.Second * 2)
}
for {
timer := time.AfterFunc(time.Second*1, timeout)
read()
fmt.Println("read")
timer.Stop()
read()
fmt.Println("process")
break
}
}