914-17:00修改
This commit is contained in:
@@ -7,120 +7,121 @@ import (
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
"toutoukan/model/usermodel/userOrder"
|
||||
)
|
||||
|
||||
// 请求体结构
|
||||
type UserRequest struct {
|
||||
UserID int `json:"user_id"`
|
||||
}
|
||||
// 请求体结构(Order字段对应目标商品标识)
|
||||
|
||||
// 响应体结构(根据实际接口响应调整)
|
||||
type Response struct {
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
// 可根据实际接口返回补充字段,如订单号、库存状态等
|
||||
}
|
||||
|
||||
func main() {
|
||||
const totalUsers = 1000 // 总用户数
|
||||
const batchSize = 130 // 每批发送的请求数
|
||||
const interval = 5 * time.Second // 每批请求的时间间隔
|
||||
const duration = 20 * time.Second // 总持续时间
|
||||
|
||||
// 计算总批次数(确保在duration时间内发送完所有请求)
|
||||
totalBatches := totalUsers / batchSize
|
||||
if totalUsers%batchSize != 0 {
|
||||
totalBatches++
|
||||
}
|
||||
|
||||
// 检查总耗时是否符合预期
|
||||
expectedDuration := time.Duration(totalBatches-1) * interval
|
||||
if expectedDuration > duration {
|
||||
fmt.Printf("警告:按当前设置将耗时 %v,超过预期的 %v\n", expectedDuration, duration)
|
||||
}
|
||||
|
||||
const totalBatches = 5 // 固定发送5批请求
|
||||
const batchSize = 150 // 每批固定150个请求
|
||||
const interval = 5 * time.Second // 每批请求的时间间隔(可按需调整)
|
||||
var wg sync.WaitGroup
|
||||
userID := 1 // 用户ID计数器
|
||||
|
||||
// 按批次发送请求
|
||||
userID := 1 // 用户ID计数器,从1开始递增(确保每个请求用户ID唯一)
|
||||
|
||||
// 循环发送5批请求,每批对应不同的商品order(stock10000 ~ stock10004)
|
||||
for batch := 1; batch <= totalBatches; batch++ {
|
||||
// 计算当前批次的请求数量(最后一批可能不足batchSize)
|
||||
currentBatchSize := batchSize
|
||||
if userID+currentBatchSize-1 > totalUsers {
|
||||
currentBatchSize = totalUsers - userID + 1
|
||||
}
|
||||
// 计算当前批次的目标商品order:第1批→stock10000,第2批→stock10001...第5批→stock10004
|
||||
targetOrder := fmt.Sprintf("stock1000%d", batch-1)
|
||||
fmt.Printf("=== 开始第 %d 批请求 ===\n", batch)
|
||||
fmt.Printf("当前批次目标商品: %s | 请求数量: %d 个 | 开始时间: %v\n",
|
||||
targetOrder, batchSize, time.Now().Format("15:04:05"))
|
||||
|
||||
fmt.Printf("开始第 %d 批请求,共 %d 个,当前时间: %v\n",
|
||||
batch, currentBatchSize, time.Now().Format("15:04:05"))
|
||||
|
||||
// 发送当前批次的请求
|
||||
for i := 0; i < currentBatchSize; i++ {
|
||||
// 并发发送当前批次的150个请求
|
||||
for i := 0; i < batchSize; i++ {
|
||||
wg.Add(1)
|
||||
go func(uid int) {
|
||||
// 捕获当前循环的uid和order(避免goroutine闭包引用循环变量问题)
|
||||
currentUID := userID
|
||||
currentOrder := targetOrder
|
||||
go func(uid int, order string) {
|
||||
defer wg.Done()
|
||||
// 发送POST请求
|
||||
result, err := sendPostRequest(uid)
|
||||
// 发送单个POST请求
|
||||
result, err := sendPostRequest(uid, order)
|
||||
if err != nil {
|
||||
fmt.Printf("用户 %d 请求失败: %v\n", uid, err)
|
||||
// 请求失败:输出错误日志(包含用户ID和商品标识)
|
||||
fmt.Printf("❌ 用户 %d (商品%s) 请求失败: %v\n", uid, order, err)
|
||||
return
|
||||
}
|
||||
// 输出结果(可以根据需要调整输出频率,避免日志过多)
|
||||
fmt.Printf("用户 %d 响应: %+v\n", uid, result)
|
||||
}(userID)
|
||||
userID++
|
||||
// 请求成功:输出响应结果(可按需调整日志粒度,避免刷屏)
|
||||
fmt.Printf("✅ 用户 %d (商品%s) 响应: 成功=%v, 消息=%s\n",
|
||||
uid, order, result.Success, result.Message)
|
||||
}(currentUID, currentOrder)
|
||||
userID++ // 每生成一个请求,用户ID递增1
|
||||
}
|
||||
|
||||
// 最后一批不需要等待
|
||||
// 等待当前批次所有请求完成(确保批次内请求全部处理后,再进入下一批)
|
||||
wg.Wait()
|
||||
fmt.Printf("=== 第 %d 批请求全部完成 | 完成时间: %v ===\n\n",
|
||||
batch, time.Now().Format("15:04:05"))
|
||||
|
||||
// 非最后一批请求,等待指定间隔后再发送下一批(避免请求集中压测)
|
||||
if batch < totalBatches {
|
||||
// 等待指定间隔后再发送下一批
|
||||
fmt.Printf("等待 %v 后发送下一批请求...\n\n", interval)
|
||||
time.Sleep(interval)
|
||||
}
|
||||
}
|
||||
|
||||
// 等待所有请求完成
|
||||
wg.Wait()
|
||||
fmt.Println("所有请求已完成")
|
||||
fmt.Println("🎉 所有5批请求已全部完成!")
|
||||
}
|
||||
|
||||
// 发送POST请求
|
||||
func sendPostRequest(userID int) (*Response, error) {
|
||||
url := "http://localhost:9096/user/kill"
|
||||
type UserRequest1 struct {
|
||||
UserID int `json:"user_id"`
|
||||
Order string `json:"userorder"` // 对应商品标识:stock10000 ~ stock10004
|
||||
}
|
||||
|
||||
// 创建请求体
|
||||
requestBody := UserRequest{
|
||||
// 发送POST请求(接收用户ID和商品order,构造请求体)
|
||||
func sendPostRequest(userID int, order string) (*Response, error) {
|
||||
// 目标接口地址(根据实际部署调整)
|
||||
targetURL := "http://localhost:9096/user/kill"
|
||||
// 1. 构造请求体(绑定当前用户ID和目标商品order)
|
||||
requestBody := userOrder.UserRequest{
|
||||
UserID: userID,
|
||||
Order: order,
|
||||
}
|
||||
|
||||
// 转换为JSON
|
||||
// 2. 将请求体序列化为JSON格式
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("JSON序列化失败: %v", err)
|
||||
}
|
||||
|
||||
// 发送POST请求
|
||||
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonBody))
|
||||
// 3. 发送POST请求(设置Content-Type为application/json)
|
||||
resp, err := http.Post(
|
||||
targetURL,
|
||||
"application/json",
|
||||
bytes.NewBuffer(jsonBody),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求发送失败: %v", err)
|
||||
return nil, fmt.Errorf("请求发送失败(网络/连接问题): %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer resp.Body.Close() // 确保响应体关闭,避免资源泄漏
|
||||
|
||||
// 解析响应(无论状态码如何都尝试解析,以便获取详细信息)
|
||||
// 4. 解析接口响应(无论状态码是否为200,均尝试解析,便于排查问题)
|
||||
var response Response
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
if err := decoder.Decode(&response); err != nil {
|
||||
return nil, fmt.Errorf("响应解析失败: %v", err)
|
||||
return nil, fmt.Errorf("响应解析失败(格式不匹配): %v", err)
|
||||
}
|
||||
|
||||
// 检查响应状态码
|
||||
// 状态码异常时,返回包含完整响应的错误信息
|
||||
// 5. 检查HTTP状态码(非200视为异常,返回详细信息)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
// 将完整响应转为JSON字符串,便于查看
|
||||
responseJSON, _ := json.MarshalIndent(response, "", " ")
|
||||
// 将响应内容转为格式化JSON,便于查看完整错误信息
|
||||
responseDetail, _ := json.MarshalIndent(response, "", " ")
|
||||
return &response, fmt.Errorf(
|
||||
"状态码异常: %d, 响应内容: %s",
|
||||
"HTTP状态码异常: %d | 接口响应详情: %s",
|
||||
resp.StatusCode,
|
||||
responseJSON,
|
||||
responseDetail,
|
||||
)
|
||||
}
|
||||
|
||||
// 6. 请求成功,返回解析后的响应
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user