diff --git a/internal/data/gorm.go b/internal/data/gorm.go index 5fc5899..95157ac 100644 --- a/internal/data/gorm.go +++ b/internal/data/gorm.go @@ -28,7 +28,7 @@ func db(data *conf.Data_Database) (*gorm.DB, func()) { mysql.Open(data.Source), &gorm.Config{ Logger: logger.Default.LogMode(logger.Info), - SkipDefaultTransaction: false, + SkipDefaultTransaction: true, }, ) diff --git a/internal/data/gorm_test.go b/internal/data/gorm_test.go index 6ee6239..28c7933 100644 --- a/internal/data/gorm_test.go +++ b/internal/data/gorm_test.go @@ -25,7 +25,7 @@ func Test_gorm_db_create(t *testing.T) { mysql.Open(source), &gorm.Config{ Logger: logger.Default.LogMode(logger.Info), - SkipDefaultTransaction: false, + SkipDefaultTransaction: true, }, ) if err != nil { @@ -37,14 +37,25 @@ func Test_gorm_db_create(t *testing.T) { t.Fatal("failed to gormDB " + err.Error()) } - sqlDB.SetMaxIdleConns(1000) - sqlDB.SetMaxOpenConns(1000) + sqlDB.SetMaxIdleConns(20) + sqlDB.SetMaxOpenConns(100) start2 := time.Now() concurrency := 1000 // 调整并发数 errCount := 0 + stats := sqlDB.Stats() + fmt.Printf("order create 当前打开连接数:%d, 空闲连接数:%d\n", stats.OpenConnections, stats.Idle) + + go func() { + ticker := time.NewTicker(50 * time.Millisecond) + defer ticker.Stop() + for range ticker.C { + fmt.Printf("监控: 当前打开连接数:%d, 空闲连接数:%d\n", stats.OpenConnections, stats.Idle) + } + }() + var wg sync.WaitGroup wg.Add(concurrency) @@ -52,14 +63,13 @@ func Test_gorm_db_create(t *testing.T) { go func(i int) { defer wg.Done() - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() + ctx := context.Background() now := time.Now() info := &model.Order{ - OrderNo: fmt.Sprintf("test_create_1_%d", i), - OutBizNo: fmt.Sprintf("174252390990605ngywYrSAGE83e2_%d", i), + OrderNo: fmt.Sprintf("test_create_10_%d", i), + OutBizNo: fmt.Sprintf("374252390990605ngywYrSAGE8310_%d", i), ProductNo: "001", BatchNo: "001", Account: "oO3vO5K2nE131-9uMoeYymLhlbYk", @@ -80,14 +90,21 @@ func Test_gorm_db_create(t *testing.T) { info.Status = vo.OrderStatusSuccess.GetValue() } - tx := gormDB.WithContext(ctx).Model(model.Order{}).Create(info) + ddd := gormDB.WithContext(ctx).Model(model.Order{}) + tx := ddd.Create(info) + if tx.Error != nil { fmt.Printf("写入错误: %v\n", tx.Error) errCount += 1 } + + sqlDBx, _ := ddd.DB() + fmt.Printf("order create 当前打开连接数:%d, 空闲连接数:%d\n", sqlDBx.Stats().OpenConnections, sqlDBx.Stats().Idle) }(i) } + fmt.Printf("order create 当前打开连接数:%d, 空闲连接数:%d\n", stats.OpenConnections, stats.Idle) + wg.Wait() t.Logf("\n--------------连接请求,总请求耗时: %v,总数: %d, 失败次数: %d--------------\n", time.Since(start2), concurrency, errCount) } @@ -102,6 +119,7 @@ func Test_gorm_db(t *testing.T) { // 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", @@ -110,7 +128,6 @@ func Test_gorm_db(t *testing.T) { // 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", @@ -199,47 +216,3 @@ func Test_db(t *testing.T) { 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) - //} -}