forked from go-xorm/xorm-redis-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_redis_cacher_test.go
134 lines (113 loc) · 2.91 KB
/
mysql_redis_cacher_test.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package xormrediscache
import (
"database/sql"
_ "encoding/gob"
"testing"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
. "github.com/go-xorm/tests"
"github.com/go-xorm/xorm"
)
/*
CREATE DATABASE IF NOT EXISTS xorm_test CHARACTER SET
utf8 COLLATE utf8_general_ci;
*/
func TestMysqlWithCache(t *testing.T) {
err := mysqlDdlImport()
if err != nil {
t.Error(err)
return
}
engine, err := xorm.NewEngine("mysql", "root:@/xorm_test2?charset=utf8")
defer engine.Close()
if err != nil {
t.Error(err)
return
}
engine.SetDefaultCacher(NewRedisCacher("localhost:6379", "", DEFAULT_EXPIRATION, engine.Logger()))
engine.ShowSQL(true)
engine.Logger().SetLevel(core.LOG_DEBUG)
BaseTestAll(engine, t)
BaseTestAllSnakeMapper(engine, t)
BaseTestAll2(engine, t)
}
func TestMysqlWithCacheSameMapper(t *testing.T) {
err := mysqlDdlImport()
if err != nil {
t.Error(err)
return
}
engine, err := xorm.NewEngine("mysql", "root:@/xorm_test3?charset=utf8")
defer engine.Close()
if err != nil {
t.Error(err)
return
}
engine.SetMapper(core.SameMapper{})
engine.SetDefaultCacher(NewRedisCacher("localhost:6379", "", DEFAULT_EXPIRATION, engine.Logger()))
engine.ShowSQL(true)
engine.Logger().SetLevel(core.LOG_DEBUG)
BaseTestAll(engine, t)
BaseTestAllSameMapper(engine, t)
BaseTestAll2(engine, t)
}
func newMysqlEngine() (*xorm.Engine, error) {
return xorm.NewEngine("mysql", "root:@/xorm_test?charset=utf8")
}
func newMysqlEngineWithCacher() (*xorm.Engine, error) {
engine, err := newMysqlEngine()
if err == nil {
engine.SetDefaultCacher(NewRedisCacher("localhost:6379", "", DEFAULT_EXPIRATION, engine.Logger()))
}
return engine, err
}
func mysqlDdlImport() error {
engine, err := xorm.NewEngine("mysql", "root:@/?charset=utf8")
if err != nil {
return err
}
engine.ShowSQL(true)
engine.Logger().SetLevel(core.LOG_DEBUG)
sqlResults, _ := engine.ImportFile("../testdata/mysql_ddl.sql")
engine.Logger().Debugf("sql results: %v", sqlResults)
engine.Close()
return nil
}
func newMysqlDriverDB() (*sql.DB, error) {
return sql.Open("mysql", "root:@/xorm_test?charset=utf8")
}
func BenchmarkMysqlDriverInsert(t *testing.B) {
DoBenchDriver(newMysqlDriverDB, CreateTableMySql, DropTableMySql,
DoBenchDriverInsert, t)
}
func BenchmarkMysqlDriverFind(t *testing.B) {
DoBenchDriver(newMysqlDriverDB, CreateTableMySql, DropTableMySql,
DoBenchDriverFind, t)
}
func BenchmarkMysqlCacheInsert(t *testing.B) {
engine, err := newMysqlEngineWithCacher()
defer engine.Close()
if err != nil {
t.Error(err)
return
}
DoBenchInsert(engine, t)
}
func BenchmarkMysqlCacheFind(t *testing.B) {
engine, err := newMysqlEngineWithCacher()
defer engine.Close()
if err != nil {
t.Error(err)
return
}
DoBenchFind(engine, t)
}
func BenchmarkMysqlCacheFindPtr(t *testing.B) {
engine, err := newMysqlEngineWithCacher()
defer engine.Close()
if err != nil {
t.Error(err)
return
}
DoBenchFindPtr(engine, t)
}