Skip to content

Commit

Permalink
feat: Pressure test code (#279)
Browse files Browse the repository at this point in the history
* feat: pressure test and msg test

* new feat: modify dictory

* feat: modify dictory

* pressure test

* pressure test

* feat: pressure send msg

* feature: pressure test code

* test: pressure test code

---------

Co-authored-by: Gordon <46924906+FGadvancer@users.noreply.github.com>
  • Loading branch information
wangchuxiao-dev and FGadvancer authored Aug 25, 2023
1 parent 9efbb1c commit 5437dd7
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 61 deletions.
6 changes: 6 additions & 0 deletions msgtest/module/group_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package module

import (
"context"
"fmt"
"open_im_sdk/pkg/constant"
"time"

"github.com/OpenIMSDK/protocol/group"
"github.com/OpenIMSDK/protocol/sdkws"
Expand All @@ -12,6 +14,10 @@ type TestGroupManager struct {
*MetaManager
}

func (t *TestGroupManager) GenGroupID(prefix string) string {
return fmt.Sprintf("%s_test_group_id_%d", prefix, time.Now().UnixNano())
}

func (t *TestGroupManager) CreateGroup(groupID string, groupName string, ownerUserID string, userIDs []string) error {
req := &group.CreateGroupReq{
MemberUserIDs: userIDs,
Expand Down
7 changes: 7 additions & 0 deletions msgtest/module/user_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func (t *TestUserManager) GenUserIDs(num int) (userIDs []string) {
return userIDs
}

func (t *TestUserManager) GenUserIDsWithPrefix(num int, prefix string) (userIDs []string) {
for i := 0; i < num; i++ {
userIDs = append(userIDs, fmt.Sprintf("%s_testv3new_%d_%d", prefix, time.Now().UnixNano(), i))
}
return userIDs
}

func (t *TestUserManager) RegisterUsers(userIDs ...string) error {
var users []*sdkws.UserInfo
for _, userID := range userIDs {
Expand Down
249 changes: 188 additions & 61 deletions msgtest/pressure_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
package msgtest

import (

"context"
"flag"
"fmt"
"open_im_sdk/msgtest/module"
"open_im_sdk/sdk_struct"
"sync"
"testing"
"time"


"github.com/OpenIMSDK/tools/log"
)

func InitWithFlag() {
flag.IntVar(&totalOnlineUserNum, "t", 100000, "total online user num")
flag.IntVar(&friendMsgSenderNum, "f", 100, "friend msg sender num")
flag.IntVar(&NotFriendMsgSenderNum, "n", 100, "not friend msg sender num")
flag.IntVar(&groupMsgSenderNum, "g", 100, "group msg sender num")
flag.IntVar(&msgSenderNumEvreyUser, "m", 100, "msg sender num evrey user")

flag.IntVar(&recvMsgUserNum, "r", 20, "recv msg user num")

}

const (
TenThousandGroupUserNum = 10000
ThousandGroupUserNum = 1000
Expand All @@ -35,6 +23,9 @@ const (
ThousandGroupNum = 5
HundredGroupNum = 50
FiftyGroupNum = 100

FastenedUserPrefix = "fastened_user_prefix"
RecvMsgPrefix = "recv_msg_prefix"
)

var (
Expand All @@ -43,76 +34,77 @@ var (
NotFriendMsgSenderNum int // 非好友消息发送者数
groupMsgSenderNum int // 群消息发送者数
msgSenderNumEvreyUser int // 每个用户的消息数
fastenedUserNum int // 固定用户数

recvMsgUserNum int // 消息接收者数, 抽样账号

)

func InitWithFlag() {
flag.IntVar(&totalOnlineUserNum, "t", 100000, "total online user num")
flag.IntVar(&friendMsgSenderNum, "f", 100, "friend msg sender num")
flag.IntVar(&NotFriendMsgSenderNum, "n", 100, "not friend msg sender num")
flag.IntVar(&groupMsgSenderNum, "g", 100, "group msg sender num")
flag.IntVar(&msgSenderNumEvreyUser, "m", 100, "msg sender num evrey user")

flag.IntVar(&recvMsgUserNum, "r", 20, "recv msg user num")
flag.IntVar(&fastenedUserNum, "u", 320, "fastened user num")
}

func init() {

InitWithFlag()
flag.Parse()

if err := log.InitFromConfig("sdk.log", "sdk", 4,
true, false, "./chat_log", 2, 24); err != nil {
panic(err)
}
}


func Test_Pressure(t *testing.T) {
func Test_PressureFull(t *testing.T) {
flag.Parse()
if friendMsgSenderNum+NotFriendMsgSenderNum+groupMsgSenderNum > totalOnlineUserNum {
t.Fatal("sender num > total online user num")
}

p := NewPressureTester()
// sample recv msg user
recvMsgUserIDs := p.userManager.GenUserIDs(recvMsgUserNum)
userIDs := p.userManager.GenUserIDs(totalOnlineUserNum)
var groupSenderUserIDs, friendSenderUserIDs, notfriendSenderUserIDs []string
if err := p.userManager.RegisterUsers(userIDs...); err != nil {
t.Fatal(err)
}
for i, userID := range userIDs {
token, err := p.userManager.GetToken(userID, int32(PLATFORMID))
if err != nil {
log.ZError(context.Background(), "get token failed", err)
continue
}
user := module.NewUser(userID, token, sdk_struct.IMConfig{WsAddr: WSADDR, ApiAddr: APIADDR, PlatformID: int32(PLATFORMID)})
if 0 <= i && i < friendMsgSenderNum {
p.msgSender[userID] = user
friendSenderUserIDs = append(friendSenderUserIDs, userID)
} else if friendMsgSenderNum <= i && i < friendMsgSenderNum+NotFriendMsgSenderNum {
p.msgSender[userID] = user
notfriendSenderUserIDs = append(notfriendSenderUserIDs, userID)
} else if friendMsgSenderNum+NotFriendMsgSenderNum <= i && i < friendMsgSenderNum+NotFriendMsgSenderNum+groupMsgSenderNum {
p.groupMsgSender[userID] = user
groupSenderUserIDs = append(groupSenderUserIDs, userID)
}
// gen userIDs
userIDs, fastenedUserIDs, recvMsgUserIDs := p.genUserIDs()

// register
if err := p.registerUsers(userIDs, fastenedUserIDs, recvMsgUserIDs); err != nil {
t.Fatalf("register users failed, err: %v", err)
}
tenThousandGroupIDs, thousandGroupIDs, hundredGroupUserIDs, fiftyGroupUserIDs, err := p.createTestGroups(userIDs)
// init users
p.initUserConns(userIDs, fastenedUserIDs)

// create groups
err := p.createTestGroups(userIDs, fastenedUserIDs, recvMsgUserIDs)
if err != nil {
t.Fatal(err)
}
totalGroupIDs := append(append(append(tenThousandGroupIDs, thousandGroupIDs...), hundredGroupUserIDs...), fiftyGroupUserIDs...)

// import friends
for _, recvMsgUserID := range recvMsgUserIDs {
p.friendManager.ImportFriends(recvMsgUserID, friendSenderUserIDs)
if err := p.importFriends(p.friendSenderUserIDs, fastenedUserIDs); err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(3)
go func() {
defer wg.Done()
p.sendMsgs2Users(friendSenderUserIDs, recvMsgUserIDs, msgSenderNumEvreyUser, time.Second)
}()
go func() {
defer wg.Done()
p.sendMsgs2Users(notfriendSenderUserIDs, recvMsgUserIDs, msgSenderNumEvreyUser, time.Second)
}()
go func() {
defer wg.Done()
p.sendMsgs2Groups(groupSenderUserIDs, totalGroupIDs, msgSenderNumEvreyUser, time.Second)
}()
wg.Wait()

p.pressureSendMsg()
// send msg test
}

func Test_InitUserConn(t *testing.T) {
flag.Parse()
p := NewPressureTester()
userNum := 10
// gen userIDs
userIDs := p.userManager.GenUserIDs(userNum)
// register
if err := p.registerUsers(userIDs, nil, nil); err != nil {
t.Fatalf("register users failed, err: %v", err)
}
// init users
p.initUserConns(userIDs, nil)
}

type PressureTester struct {
Expand All @@ -121,6 +113,11 @@ type PressureTester struct {
groupManager *module.TestGroupManager
msgSender map[string]*module.SendMsgUser
groupMsgSender map[string]*module.SendMsgUser

groupSenderUserIDs, friendSenderUserIDs, notfriendSenderUserIDs []string
recvMsgUserIDs []string

tenThousandGroupIDs, thousandGroupIDs, hundredGroupUserIDs, fiftyGroupUserIDs []string
}

func NewPressureTester() *PressureTester {
Expand All @@ -129,8 +126,110 @@ func NewPressureTester() *PressureTester {
msgSender: make(map[string]*module.SendMsgUser), groupMsgSender: make(map[string]*module.SendMsgUser)}
}

func (p *PressureTester) createTestGroups(userIDs []string) (tenThousandGroupIDs, thousandGroupIDs, hundredGroupUserIDs, fiftyGroupUserIDs []string, err error) {
p.groupManager.CreateGroup("", "", userIDs[0], userIDs[:TenThousandGroupUserNum])
func (p *PressureTester) genUserIDs() (userIDs, fastenedUserIDs, recvMsgUserIDs []string) {
userIDs = p.userManager.GenUserIDs(totalOnlineUserNum - fastenedUserNum) // 在线用户
fastenedUserIDs = p.userManager.GenUserIDsWithPrefix(fastenedUserNum, FastenedUserPrefix) // 指定320用户
recvMsgUserIDs = p.userManager.GenUserIDsWithPrefix(recvMsgUserNum, RecvMsgPrefix) // 抽样用户完整SDK
return
}

func (p *PressureTester) registerUsers(userIDs []string, fastenedUserIDs []string, recvMsgUserIDs []string) error {
for i := 0; i < len(userIDs); i += 1000 {
end := i + 1000
if end > len(userIDs) {
end = len(userIDs)
}
userIDsSlice := userIDs[i:end]
if err := p.userManager.RegisterUsers(userIDsSlice...); err != nil {
return err
}
if len(userIDsSlice) < 1000 {
break
}
}
if len(fastenedUserIDs) != 0 {
if err := p.userManager.RegisterUsers(fastenedUserIDs...); err != nil {
return err
}
}
if len(recvMsgUserIDs) != 0 {
if err := p.userManager.RegisterUsers(recvMsgUserIDs...); err != nil {
return err
}
}
return nil
}

func (p *PressureTester) initUserConns(userIDs []string, fastenedUserIDs []string) {
for i, userID := range userIDs {
token, err := p.userManager.GetToken(userID, int32(PLATFORMID))
if err != nil {
log.ZError(context.Background(), "get token failed", err, "userID", userID, "platformID", PLATFORMID)
continue
}
user := module.NewUser(userID, token, sdk_struct.IMConfig{WsAddr: WSADDR, ApiAddr: APIADDR, PlatformID: int32(PLATFORMID)})
if 0 <= i && i < friendMsgSenderNum {
p.msgSender[userID] = user
p.friendSenderUserIDs = append(p.friendSenderUserIDs, userID)
} else if friendMsgSenderNum <= i && i < friendMsgSenderNum+NotFriendMsgSenderNum {
p.msgSender[userID] = user
p.notfriendSenderUserIDs = append(p.notfriendSenderUserIDs, userID)
}
}
if len(fastenedUserIDs) != 0 {
for _, userID := range fastenedUserIDs {
token, err := p.userManager.GetToken(userID, int32(PLATFORMID))
if err != nil {
log.ZError(context.Background(), "get token failed", err, "userID", userID, "platformID", PLATFORMID)
continue
}
user := module.NewUser(userID, token, sdk_struct.IMConfig{WsAddr: WSADDR, ApiAddr: APIADDR, PlatformID: int32(PLATFORMID)})
p.msgSender[userID] = user
p.groupSenderUserIDs = append(p.groupSenderUserIDs, userID)
}
}
}

func (p *PressureTester) createTestGroups(userIDs, fastenedUserIDs, recvMsgUserIDs []string) (err error) {
// create ten thousand group
for i := 1; i <= TenThousandGroupNum; i++ {
groupID := p.groupManager.GenGroupID(fmt.Sprintf("tenThousandGroup_%d", i))
err = p.groupManager.CreateGroup(groupID, "tenThousandGroup", userIDs[0], append(userIDs[(i-1)*TenThousandGroupUserNum:i*TenThousandGroupUserNum-1], fastenedUserIDs...))
if err != nil {
return
}
p.tenThousandGroupIDs = append(p.tenThousandGroupIDs, groupID)
}
// create thousand group
exclude := TenThousandGroupNum * TenThousandGroupUserNum
for i := 1; i <= ThousandGroupNum; i++ {
groupID := p.groupManager.GenGroupID(fmt.Sprintf("thousandGroup_%d", i))
err = p.groupManager.CreateGroup(groupID, "thousandGroup", userIDs[0], append(userIDs[exclude+(i-1)*ThousandGroupUserNum:exclude+i*ThousandGroupUserNum-1], fastenedUserIDs...))
if err != nil {
return
}
p.thousandGroupIDs = append(p.thousandGroupIDs, groupID)
}
// create hundred group
exclude += exclude + ThousandGroupNum*ThousandGroupUserNum
for i := 1; i <= HundredGroupNum; i++ {
groupID := p.groupManager.GenGroupID(fmt.Sprintf("hundredGroup_%d", i))
err = p.groupManager.CreateGroup(groupID, "hundredGroup", userIDs[0], append(fastenedUserIDs[0:80], recvMsgUserIDs...))
if err != nil {
return
}
p.hundredGroupUserIDs = append(p.hundredGroupUserIDs, groupID)
}
// create fifty group
exclude += exclude + HundredGroupNum*HundredGroupUserNum
for i := 1; i <= FiftyGroupNum; i++ {
groupID := p.groupManager.GenGroupID(fmt.Sprintf("fiftyGroup_%d", i))
err = p.groupManager.CreateGroup(groupID, "fiftyGroup", userIDs[0], append(fastenedUserIDs[0:30], recvMsgUserIDs...))
if err != nil {
return
}
p.fiftyGroupUserIDs = append(p.fiftyGroupUserIDs, groupID)
}
return
}

Expand Down Expand Up @@ -159,6 +258,7 @@ func (p *PressureTester) sendMsgs2Groups(senderIDs, groupIDs []string, num int,
for _, groupID := range groupIDs {
wg.Add(1)
go func(senderID, groupID string) {
defer wg.Done()
for i := 0; i < num; i++ {
if user, ok := p.groupMsgSender[senderID]; ok {
user.SendGroupMsgWithContext(groupID, i)
Expand All @@ -171,3 +271,30 @@ func (p *PressureTester) sendMsgs2Groups(senderIDs, groupIDs []string, num int,
wg.Wait()
}

func (p *PressureTester) pressureSendMsg() {
var wg sync.WaitGroup
wg.Add(3)
go func() {
defer wg.Done()
p.sendMsgs2Users(p.friendSenderUserIDs, p.recvMsgUserIDs, msgSenderNumEvreyUser, time.Second)
}()
go func() {
defer wg.Done()
p.sendMsgs2Users(p.notfriendSenderUserIDs, p.recvMsgUserIDs, msgSenderNumEvreyUser, time.Second)
}()
go func() {
defer wg.Done()
totalGroupIDs := append(append(p.tenThousandGroupIDs, p.thousandGroupIDs...), append(p.hundredGroupUserIDs, p.fiftyGroupUserIDs...)...)
p.sendMsgs2Groups(p.groupSenderUserIDs, totalGroupIDs, msgSenderNumEvreyUser, time.Second)
}()
wg.Wait()
}

func (p *PressureTester) importFriends(friendSenderUserIDs, recvMsgUserIDs []string) error {
for _, recvMsgUserID := range recvMsgUserIDs {
if err := p.friendManager.ImportFriends(recvMsgUserID, friendSenderUserIDs); err != nil {
return err
}
}
return nil
}

0 comments on commit 5437dd7

Please sign in to comment.