forked from go-xorm/xorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachegoroutine.go
105 lines (93 loc) · 2.57 KB
/
cachegoroutine.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
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/lunny/xorm"
_ "github.com/mattn/go-sqlite3"
"os"
)
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
}
size := 500
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 {
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)
_, err = engine.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
}
//conns := atomic.LoadInt32(&xorm.ConnectionNum)
//fmt.Println("connection number:", conns)
fmt.Println("end")
}
func main() {
fmt.Println("-----start sqlite go routines-----")
engine, err := sqliteEngine()
if err != nil {
fmt.Println(err)
return
}
engine.ShowSQL = true
cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
engine.SetDefaultCacher(cacher)
fmt.Println(engine)
test(engine)
fmt.Println("test end")
engine.Close()
fmt.Println("-----start mysql go routines-----")
engine, err = mysqlEngine()
engine.ShowSQL = true
cacher = xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
engine.SetDefaultCacher(cacher)
if err != nil {
fmt.Println(err)
return
}
defer engine.Close()
test(engine)
}