daShangDao_getErpSendPublis.../test_redis_pool.go
2026-06-15 16:18:50 +08:00

72 lines
1.5 KiB
Go

package main
import (
"fmt"
"getErpSendPublishing/utils"
"io"
"log"
"net/http"
"strings"
"sync"
"time"
)
func main() {
// 初始化Redis连接池
if err := utils.InitRedisTwoForParseFormData(); err != nil {
log.Fatalf("Redis初始化失败: %v", err)
}
log.Println("Redis连接池初始化成功")
// 模拟高频请求
const concurrentRequests = 100
const totalRequests = 1000
var wg sync.WaitGroup
wg.Add(totalRequests)
start := time.Now()
for i := 0; i < totalRequests; i++ {
go func(i int) {
defer wg.Done()
// 构建请求体
reqBody := fmt.Sprintf("shopid=123&isbn=ISBN%d", i)
// 发送请求
resp, err := http.Post("http://localhost:8182/api/goods/simple", "application/x-www-form-urlencoded", strings.NewReader(reqBody))
if err != nil {
log.Printf("请求失败: %v", err)
return
}
defer resp.Body.Close()
// 读取响应
_, err = io.ReadAll(resp.Body)
if err != nil {
log.Printf("读取响应失败: %v", err)
return
}
// 每100个请求打印一次进度
if (i+1)%100 == 0 {
log.Printf("已完成 %d 个请求", i+1)
}
}(i)
// 控制并发数
if (i+1)%concurrentRequests == 0 {
time.Sleep(100 * time.Millisecond)
}
}
// 等待所有请求完成
wg.Wait()
// 计算执行时间
duration := time.Since(start)
log.Printf("完成 %d 个请求,耗时: %v", totalRequests, duration)
log.Printf("平均每个请求耗时: %v", duration/time.Duration(totalRequests))
}