54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package data
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"google.golang.org/protobuf/types/known/durationpb"
|
|
"gorm.io/gorm"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
"voucher/internal/conf"
|
|
"voucher/internal/data/model"
|
|
)
|
|
|
|
func Test_db(t *testing.T) {
|
|
maxLifetime := durationpb.New(300) // 5分钟
|
|
data := &conf.Data_Database{
|
|
Driver: "mysql",
|
|
Source: "root:lansexiongdi6,@tcp(47.97.27.195:3306)/voucher?parseTime=True&loc=Local",
|
|
MaxIdle: 50, // 空闲连接池
|
|
MaxOpen: 100, // 最大连接数
|
|
MaxLifetime: maxLifetime,
|
|
IsDebug: false,
|
|
}
|
|
gormDb, cleanup := db(data)
|
|
defer cleanup()
|
|
|
|
start2 := time.Now()
|
|
errCount := 0
|
|
var wg sync.WaitGroup
|
|
const concurrency = 50 // 调整并发数
|
|
wg.Add(concurrency)
|
|
|
|
for i := 0; i < concurrency; i++ {
|
|
go func() {
|
|
defer wg.Done()
|
|
|
|
var order model.Order
|
|
tx := gormDb.First(&order)
|
|
if tx.Error != nil {
|
|
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
|
|
t.Errorf("未找到记录")
|
|
} else {
|
|
fmt.Printf("请求错误: %v\n", tx.Error)
|
|
errCount += 1
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
wg.Wait()
|
|
fmt.Printf("\n--------------连接请求,总请求耗时: %v,失败次数: %d--------------\n", time.Since(start2), errCount)
|
|
}
|