voucher/internal/data/gorm_test.go

246 lines
6.4 KiB
Go

package data
import (
"context"
"errors"
"fmt"
"google.golang.org/protobuf/types/known/durationpb"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/hints"
"sync"
"testing"
"time"
"voucher/internal/biz/vo"
"voucher/internal/conf"
"voucher/internal/data/model"
)
var source = "voucher:Lsxd@2024@tcp(voucher.rwlb.cn-chengdu.rds.aliyuncs.com:3306)/voucher?parseTime=True&loc=Local"
func Test_gorm_db_create(t *testing.T) {
gormDB, err := gorm.Open(
mysql.Open(source),
&gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
SkipDefaultTransaction: false,
},
)
if err != nil {
t.Fatal("failed to connect database " + err.Error())
}
sqlDB, err := gormDB.DB()
if err != nil {
t.Fatal("failed to gormDB " + err.Error())
}
sqlDB.SetMaxIdleConns(1000)
sqlDB.SetMaxOpenConns(1000)
start2 := time.Now()
concurrency := 1000 // 调整并发数
errCount := 0
var wg sync.WaitGroup
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
go func(i int) {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
now := time.Now()
info := &model.Order{
OrderNo: fmt.Sprintf("test_create_1_%d", i),
OutBizNo: fmt.Sprintf("174252390990605ngywYrSAGE83e2_%d", i),
ProductNo: "001",
BatchNo: "001",
Account: "oO3vO5K2nE131-9uMoeYymLhlbYk",
AccountType: 1,
Status: 2,
Type: 1,
AppID: "",
MerchantNo: "wx9ed74283ad25bca1",
Channel: 1,
NotifyUrl: "https://sandbox.cdcc.cmbchina.com/AccessGateway/transIn/updateCodeStatus.json",
Attach: "{}",
CreateTime: &now,
UpdateTime: &now,
}
if info.ProductNo == "001" {
info.VoucherNo = info.OrderNo
info.Status = vo.OrderStatusSuccess.GetValue()
}
tx := gormDB.WithContext(ctx).Model(model.Order{}).Create(info)
if tx.Error != nil {
fmt.Printf("写入错误: %v\n", tx.Error)
errCount += 1
}
}(i)
}
wg.Wait()
t.Logf("\n--------------连接请求,总请求耗时: %v,总数: %d, 失败次数: %d--------------\n", time.Since(start2), concurrency, errCount)
}
func Test_gorm_db(t *testing.T) {
// 镜像mysql
//data := &conf.Data_Database{
// Driver: "mysql",
// Source: "root:lsxddb123.@tcp(47.108.53.72:3306)/voucher?parseTime=True&loc=Local",
// MaxIdle: 20, // 空闲连接池
// MaxOpen: 200, // 最大连接数
// MaxLifetime: durationpb.New(60), // 5分钟
// IsDebug: false,
//}
//data := &conf.Data_Database{
// Driver: "mysql",
// Source: "root:lansexiongdi6,@tcp(47.97.27.195:3306)/voucher?parseTime=True&loc=Local",
// MaxIdle: 2, // 空闲连接池
// MaxOpen: 10, // 最大连接数
// MaxLifetime: durationpb.New(60),
// IsDebug: false,
//}
data := &conf.Data_Database{
Driver: "mysql",
Source: "voucher:Lsxd@2024@tcp(voucher.rwlb.cn-chengdu.rds.aliyuncs.com:3306)/voucher?parseTime=True&loc=Local",
MaxIdle: 20, // 空闲连接池
MaxOpen: 200, // 最大连接数
MaxLifetime: durationpb.New(60), // 5分钟
IsDebug: false,
}
gormDb, cleanup := db(data)
defer cleanup()
start2 := time.Now()
concurrency := 10000 // 调整并发数
errCount := 0
var wg sync.WaitGroup
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
go func(i int) {
defer wg.Done()
//ctx := context.Background()
var info model.Order
tx := gormDb.
//WithContext(ctx).
Model(model.Order{}).
//Clauses(hints.UseIndex("udx_out_biz_no_type")).
Where(model.Order{Type: vo.OrderTypeCmb.GetValue(), OutBizNo: fmt.Sprintf("174252390990605ngywYrSAGE83e1%d", i)}).
First(&info)
if tx.Error != nil {
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
t.Errorf("未找到记录")
} else {
fmt.Printf("请求错误: %v\n", tx.Error)
errCount += 1
}
}
time.Sleep(1 * time.Microsecond)
}(i)
}
wg.Wait()
t.Logf("\n--------------连接请求,总请求耗时: %v,总数: %d, 失败次数: %d--------------\n", time.Since(start2), concurrency, errCount)
}
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: 2, // 空闲连接池
MaxOpen: 10, // 最大连接数
MaxLifetime: maxLifetime,
IsDebug: false,
}
gormDb, cleanup := db(data)
defer cleanup()
start2 := time.Now()
errCount := 0
const concurrency = 1
for i := 0; i < concurrency; i++ {
ctx := context.Background()
var info model.Order
tx := gormDb.
WithContext(ctx).
Model(model.Order{}).
Clauses(hints.ForceIndex("udx_out_biz_no_type")).
Where(model.Order{Type: vo.OrderTypeCmb.GetValue(), OutBizNo: fmt.Sprintf("174252390990605ngywYrSAGE83e1%d", i)}).
First(&info)
if tx.Error != nil {
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
t.Errorf("未找到记录")
} else {
fmt.Printf("请求错误: %v\n", tx.Error)
errCount += 1
}
}
}
t.Logf("\n--------------连接请求,总请求耗时: %v,总数: %d, 失败次数: %d--------------\n", time.Since(start2), concurrency, errCount)
}
func Test_orderCreate(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: 2, // 空闲连接池
// MaxOpen: 10, // 最大连接数
// MaxLifetime: maxLifetime,
// IsDebug: false,
//}
//gormDb, cleanup := db(data)
//defer cleanup()
//
//now := time.Now()
//
//info := &model.Order{
// OrderNo: req.OrderNo,
// OutBizNo: req.OutBizNo,
// ProductNo: req.ProductNo,
// BatchNo: req.BatchNo,
// Account: req.Account,
// AccountType: req.AccountType.GetValue(),
// Status: req.Status.GetValue(),
// Type: req.Type.GetValue(),
// AppID: req.AppID,
// MerchantNo: req.MerchantNo,
// Channel: req.Channel.GetValue(),
// NotifyUrl: req.NotifyUrl,
// Attach: req.Attach,
// CreateTime: &now,
// UpdateTime: &now,
//}
//
//if req.ProductNo == "001" {
// info.VoucherNo = req.OrderNo
// info.Status = vo.OrderStatusSuccess.GetValue()
//}
//
//tx := p.DB(ctx).Create(info)
//if tx.Error != nil {
// return nil, fmt.Errorf("create db fail %w", tx.Error)
//}
}