78 lines
1.4 KiB
Go
78 lines
1.4 KiB
Go
package mysql
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"testing"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type User struct {
|
|
gorm.Model
|
|
Name string
|
|
Born *time.Time
|
|
}
|
|
|
|
func TestMysql(t *testing.T) {
|
|
connstring := "root:root@tcp(i.myschools.me:6006)/sandbox?charset=utf8&parseTime=True&loc=Local"
|
|
db, err := New(&Config{
|
|
ConnString: connstring,
|
|
ConnMaxLifetime: 0,
|
|
MaxIdleConns: 200,
|
|
MaxOpenConns: 200,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
db.AutoMigrate(&User{})
|
|
|
|
for i := 0; i < 100; i++ {
|
|
go func(mydb *gorm.DB, ii int) {
|
|
for j := 0; j < 100; j++ {
|
|
err := mydb.Create(&User{
|
|
Name: fmt.Sprintf("cccc%d-%d", ii, j),
|
|
})
|
|
if err != nil {
|
|
log.Println(err.Error)
|
|
}
|
|
}
|
|
}(db, i)
|
|
}
|
|
|
|
time.Sleep(100 * time.Second)
|
|
}
|
|
|
|
func TestMysql2(t *testing.T) {
|
|
connstring := "aaaa:root@tcp(i.myschools.me:3306)/sandbox?charset=utf8&parseTime=True&loc=Local"
|
|
db, err := New(&Config{
|
|
ConnString: connstring,
|
|
ConnMaxLifetime: 0,
|
|
MaxIdleConns: 200,
|
|
MaxOpenConns: 200,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
db.AutoMigrate(&User{})
|
|
|
|
for i := 0; i < 100; i++ {
|
|
go func(ii int) {
|
|
mydb, err := New(nil)
|
|
if err != nil {
|
|
log.Println(err.Error())
|
|
}
|
|
for j := 0; j < 100; j++ {
|
|
if err := mydb.Create(&User{
|
|
Name: fmt.Sprintf("eeee-%d-%d", ii, j),
|
|
}).Error; err != nil {
|
|
log.Println(err.Error())
|
|
}
|
|
}
|
|
}(i)
|
|
}
|
|
|
|
time.Sleep(100 * time.Second)
|
|
}
|