From dd981b976dbf2f84f09b1e34c91a532320d4dacc Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Fri, 25 Apr 2025 15:57:23 +0800 Subject: [PATCH] refactor: move stress-test tools location. (#3295) * refactor: move stress-test tools location. * improve stress tools. * improve stress_test-v2 --- test/stress-test-v2/README.md | 19 +++++++++++++ {tools => test}/stress-test-v2/main.go | 39 ++++++++++++++++++++------ test/stress-test/README.md | 19 +++++++++++++ {tools => test}/stress-test/main.go | 0 tools/stress-test/README.md | 25 ----------------- 5 files changed, 69 insertions(+), 33 deletions(-) create mode 100644 test/stress-test-v2/README.md rename {tools => test}/stress-test-v2/main.go (93%) create mode 100644 test/stress-test/README.md rename {tools => test}/stress-test/main.go (100%) delete mode 100644 tools/stress-test/README.md diff --git a/test/stress-test-v2/README.md b/test/stress-test-v2/README.md new file mode 100644 index 000000000..cbd4bdbde --- /dev/null +++ b/test/stress-test-v2/README.md @@ -0,0 +1,19 @@ +# Stress Test V2 + +## Usage + +You need set `TestTargetUserList` variables. + +### Build + +```bash + +go build -o test/stress-test-v2/stress-test-v2 test/stress-test-v2/main.go +``` + +### Excute + +```bash + +tools/stress-test-v2/stress-test-v2 -c config/ +``` diff --git a/tools/stress-test-v2/main.go b/test/stress-test-v2/main.go similarity index 93% rename from tools/stress-test-v2/main.go rename to test/stress-test-v2/main.go index 0c309b9c9..0e4609964 100644 --- a/tools/stress-test-v2/main.go +++ b/test/stress-test-v2/main.go @@ -56,6 +56,7 @@ var ( const ( MaxUser = 100000 + Max1kUser = 1000 Max100KGroup = 100 Max999Group = 1000 MaxInviteUserLimit = 999 @@ -161,7 +162,7 @@ func (st *StressTest) PostRequest(ctx context.Context, url string, reqbody any) if baseResp.ErrCode != 0 { err = fmt.Errorf(baseResp.ErrMsg) - log.ZError(ctx, "Failed to send request", err, "url", url, "reqbody", reqbody, "resp", baseResp) + // log.ZError(ctx, "Failed to send request", err, "url", url, "reqbody", reqbody, "resp", baseResp) return nil, err } @@ -530,9 +531,20 @@ func main() { // Create 100K groups st.Wg.Add(1) go func(idx int) { + startTime := time.Now() + defer func() { + elapsedTime := time.Since(startTime) + log.ZInfo(st.Ctx, "100K group creation completed", + "groupID", fmt.Sprintf("v2_StressTest_Group_100K_%d", idx), + "index", idx, + "duration", elapsedTime.String()) + }() + defer st.Wg.Done() defer func() { + st.Mutex.Lock() st.Create100kGroupCounter++ + st.Mutex.Unlock() }() groupID := fmt.Sprintf("v2_StressTest_Group_100K_%d", idx) @@ -542,7 +554,7 @@ func main() { // continue } - for i := 0; i < MaxUser/MaxInviteUserLimit; i++ { + for i := 0; i <= MaxUser/MaxInviteUserLimit; i++ { InviteUserIDs := make([]string, 0) // ensure TargetUserList is in group InviteUserIDs = append(InviteUserIDs, TestTargetUserList...) @@ -556,7 +568,7 @@ func main() { } if len(InviteUserIDs) == 0 { - log.ZWarn(st.Ctx, "InviteUserIDs is empty", nil, "groupID", groupID) + // log.ZWarn(st.Ctx, "InviteUserIDs is empty", nil, "groupID", groupID) continue } @@ -567,7 +579,7 @@ func main() { } if len(InviteUserIDs) == 0 { - log.ZWarn(st.Ctx, "InviteUserIDs is empty", nil, "groupID", groupID) + // log.ZWarn(st.Ctx, "InviteUserIDs is empty", nil, "groupID", groupID) continue } @@ -602,9 +614,20 @@ func main() { // Create 999 groups st.Wg.Add(1) go func(idx int) { + startTime := time.Now() + defer func() { + elapsedTime := time.Since(startTime) + log.ZInfo(st.Ctx, "999 group creation completed", + "groupID", fmt.Sprintf("v2_StressTest_Group_1K_%d", idx), + "index", idx, + "duration", elapsedTime.String()) + }() + defer st.Wg.Done() defer func() { + st.Mutex.Lock() st.Create999GroupCounter++ + st.Mutex.Unlock() }() groupID := fmt.Sprintf("v2_StressTest_Group_1K_%d", idx) @@ -613,13 +636,13 @@ func main() { log.ZError(st.Ctx, "Create group failed.", err) // continue } - for i := 0; i < MaxUser/MaxInviteUserLimit; i++ { + for i := 0; i <= Max1kUser/MaxInviteUserLimit; i++ { InviteUserIDs := make([]string, 0) // ensure TargetUserList is in group InviteUserIDs = append(InviteUserIDs, TestTargetUserList...) startIdx := max(i*MaxInviteUserLimit, 1) - endIdx := min((i+1)*MaxInviteUserLimit, MaxUser) + endIdx := min((i+1)*MaxInviteUserLimit, Max1kUser) for j := startIdx; j < endIdx; j++ { userCreatedID := fmt.Sprintf("v2_StressTest_User_%d", j) @@ -627,7 +650,7 @@ func main() { } if len(InviteUserIDs) == 0 { - log.ZWarn(st.Ctx, "InviteUserIDs is empty", nil, "groupID", groupID) + // log.ZWarn(st.Ctx, "InviteUserIDs is empty", nil, "groupID", groupID) continue } @@ -638,7 +661,7 @@ func main() { } if len(InviteUserIDs) == 0 { - log.ZWarn(st.Ctx, "InviteUserIDs is empty", nil, "groupID", groupID) + // log.ZWarn(st.Ctx, "InviteUserIDs is empty", nil, "groupID", groupID) continue } diff --git a/test/stress-test/README.md b/test/stress-test/README.md new file mode 100644 index 000000000..cba93e279 --- /dev/null +++ b/test/stress-test/README.md @@ -0,0 +1,19 @@ +# Stress Test + +## Usage + +You need set `TestTargetUserList` and `DefaultGroupID` variables. + +### Build + +```bash + +go build -o test/stress-test/stress-test test/stress-test/main.go +``` + +### Excute + +```bash + +tools/stress-test/stress-test -c config/ +``` diff --git a/tools/stress-test/main.go b/test/stress-test/main.go similarity index 100% rename from tools/stress-test/main.go rename to test/stress-test/main.go diff --git a/tools/stress-test/README.md b/tools/stress-test/README.md deleted file mode 100644 index 531233a20..000000000 --- a/tools/stress-test/README.md +++ /dev/null @@ -1,25 +0,0 @@ -# Stress Test - -## Usage - -You need set `TestTargetUserList` and `DefaultGroupID` variables. - -### Build - -```bash -go build -o _output/bin/tools/linux/amd64/stress-test tools/stress-test/main.go - -# or - -go build -o tools/stress-test/stress-test tools/stress-test/main.go -``` - -### Excute - -```bash -_output/bin/tools/linux/amd64/stress-test -c config/ - -#or - -tools/stress-test/stress-test -c config/ -```