forked from go-xorm/xorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxconnect.go
107 lines (93 loc) · 1.82 KB
/
maxconnect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"fmt"
"os"
"runtime"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3"
)
type User struct {
Id int64
Name string
}
func sqliteEngine() (*xorm.Engine, error) {
os.Remove("./test.db")
return xorm.NewEngine("sqlite3", "./goroutine.db")
}
func mysqlEngine() (*xorm.Engine, error) {
return xorm.NewEngine("mysql", "root:@/test?charset=utf8")
}
var u *User = &User{}
func test(engine *xorm.Engine) {
err := engine.CreateTables(u)
if err != nil {
fmt.Println(err)
return
}
engine.ShowSQL = true
engine.SetMaxOpenConns(5)
size := 1000
queue := make(chan int, size)
for i := 0; i < size; i++ {
go func(x int) {
//x := i
err := engine.Ping()
if err != nil {
fmt.Println(err)
} else {
/*err = engine.Map(u)
if err != nil {
fmt.Println("Map user failed")
} else {*/
for j := 0; j < 10; j++ {
if x+j < 2 {
_, err = engine.Get(u)
} else if x+j < 4 {
users := make([]User, 0)
err = engine.Find(&users)
} else if x+j < 8 {
_, err = engine.Count(u)
} else if x+j < 16 {
_, err = engine.Insert(&User{Name: "xlw"})
} else if x+j < 32 {
_, err = engine.Id(1).Delete(u)
}
if err != nil {
fmt.Println(err)
queue <- x
return
}
}
fmt.Printf("%v success!\n", x)
//}
}
queue <- x
}(i)
}
for i := 0; i < size; i++ {
<-queue
}
fmt.Println("end")
}
func main() {
runtime.GOMAXPROCS(2)
fmt.Println("create engine")
engine, err := sqliteEngine()
if err != nil {
fmt.Println(err)
return
}
engine.ShowSQL = true
fmt.Println(engine)
test(engine)
fmt.Println("------------------------")
engine.Close()
engine, err = mysqlEngine()
if err != nil {
fmt.Println(err)
return
}
defer engine.Close()
test(engine)
}