-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
GoMybatisEngine_test.go
249 lines (225 loc) · 7.57 KB
/
GoMybatisEngine_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package GoMybatis
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/zhuxiujia/GoMybatis/example"
"github.com/zhuxiujia/GoMybatis/stmt"
"github.com/zhuxiujia/GoMybatis/tx"
"github.com/zhuxiujia/GoMybatis/utils"
"sync"
"testing"
"time"
)
//假设Mysql 数据库查询时间为0,框架单协程的Benchmark性能
func Benchmark_One_Transcation(b *testing.B) {
b.StopTimer()
//使用事务
session := Session(&TestSession{})
//初始化mapper文件
var exampleActivityMapperImpl = InitMapperByLocalSession()
//使用mapper
var name = "dsa"
var times = time.Now()
var page = 1
//开始压力测试
b.StartTimer()
for i := 0; i < b.N; i++ {
var _, err = exampleActivityMapperImpl.SelectByCondition(&session, &name, ×, ×, &page, &page)
if err != nil {
b.Fatal(err)
}
//fmt.Println(data)
}
}
//假设Mysql 数据库查询时间为0,框架多个协程的并发数的性能
func Benchmark_One_Transcation_multiple_coroutine(b *testing.B) {
b.StopTimer()
//使用事务
session := Session(&TestSession{})
//初始化mapper文件
var exampleActivityMapperImpl = InitMapperByLocalSession()
//使用mapper
////开始TPS测试
var total = b.N //总数
var goruntine = 1000 //并发数
if total <= 1 {
goruntine = 1
}
var waitGroup = sync.WaitGroup{}
waitGroup.Add(goruntine)
var name = "dsa"
var times = time.Now()
var page = 1
b.StartTimer()
for i := 0; i < goruntine; i++ {
go func() {
var itemCount = total / goruntine
for f := 0; f < itemCount; f++ {
_, e := exampleActivityMapperImpl.SelectByCondition(&session, &name, ×, ×, &page, &page)
if e != nil {
b.Fatal(e)
}
}
waitGroup.Done()
}()
}
waitGroup.Wait()
}
//假设Mysql 数据库查询时间为0,框架单协程的并发数的性能
func Test_One_Transcation_TPS(t *testing.T) {
//使用事务
session := Session(&TestSession{})
//初始化mapper文件
var exampleActivityMapperImpl = InitMapperByLocalSession()
//使用mapper
var name = "dsa"
var times = time.Now()
var page = 1
//开始TPS测试
var total = 10000
defer utils.CountMethodTps(total, time.Now(), "Test_One_Transcation_TPS")
for i := 0; i < total; i++ {
var _, err = exampleActivityMapperImpl.SelectByCondition(&session, &name, ×, ×, &page, &page)
if err != nil {
t.Fatal(err)
}
}
}
//假设Mysql 数据库查询时间为0,框架多个协程的并发数的性能
func Test_One_Transcation_multiple_coroutine_TPS(t *testing.T) {
//使用事务
session := Session(&TestSession{})
//初始化mapper文件
var exampleActivityMapperImpl = InitMapperByLocalSession()
//使用mapper
var name = "dsa"
var times = time.Now()
var page = 1
////开始TPS测试
var total = 100000 //总数
var goruntine = 1000 //并发数
var waitGroup = sync.WaitGroup{}
waitGroup.Add(goruntine)
defer utils.CountMethodTps(total, time.Now(), "Test_One_Transcation_multiple_coroutine_TPS")
for i := 0; i < goruntine; i++ {
go func() {
var itemCount = total / goruntine
for f := 0; f < itemCount; f++ {
_, e := exampleActivityMapperImpl.SelectByCondition(&session, &name, ×, ×, &page, &page)
if e != nil {
t.Fatal(e)
}
}
waitGroup.Done()
}()
}
waitGroup.Wait()
}
//验证测试直接返回数据
func Test_Transcation(t *testing.T) {
//使用事务
session := Session(&TestSession{})
//初始化mapper文件
var exampleActivityMapperImpl = InitMapperByLocalSession()
var name = "dsa"
var times = time.Now()
var page = 1
//使用mapper
var results, err = exampleActivityMapperImpl.SelectByCondition(&session, &name, ×, ×, &page, &page)
if err != nil {
t.Fatal(err)
}
fmt.Println(results)
}
type TestSession struct {
Session
}
func (it *TestSession) Id() string {
return "sadf"
}
func (it *TestSession) Query(sqlorArgs string) ([]map[string][]byte, error) {
resultsSlice := make([]map[string][]byte, 0)
result := make(map[string][]byte)
result["name"] = []byte("活动1")
result["id"] = []byte("125")
result["pc_link"] = []byte("http://www.baidu.com")
result["h5_link"] = []byte("http://www.baidu.com")
result["remark"] = []byte("活动1")
resultsSlice = append(resultsSlice, result)
return resultsSlice, nil
}
func (it *TestSession) Exec(sqlorArgs string) (*Result, error) {
return nil, nil
}
func (it *TestSession) QueryPrepare(sqlorArgs string, args ...interface{}) ([]map[string][]byte, error) {
return nil, nil
}
func (it *TestSession) ExecPrepare(sqlorArgs string, args ...interface{}) (*Result, error) {
return nil, nil
}
func (it *TestSession) Rollback() error {
return nil
}
func (it *TestSession) Commit() error {
return nil
}
func (it *TestSession) Begin(*tx.Propagation) error {
return nil
}
func (it *TestSession) Close() {
}
func (it *TestSession) StmtConvert() (stmt.StmtIndexConvert, error) {
return stmt.BuildStmtConvert("mysql")
}
//定义mapper文件的接口和结构体
// 支持基本类型(int,string,time.Time,float...且需要指定参数名称`args:"name"以逗号隔开,且位置要和实际参数相同)
//自定义结构体参数(属性必须大写)
//参数中除了session指针外,为指针类型的皆为数据
// 函数return必须为error 为返回错误信息
type ExampleActivityMapperImpl struct {
SelectByCondition func(session *Session, name *string, startTime *time.Time, endTime *time.Time, page *int, size *int) ([]example.Activity, error) `args:"session,name,startTime,endTime,page,size"`
}
//初始化mapper文件和结构体
func InitMapperByLocalSession() ExampleActivityMapperImpl {
var engine = GoMybatisEngine{}.New()
//mysql链接格式为 用户名:密码@(数据库链接地址:端口)/数据库名称 例如root:123456@(***.mysql.rds.aliyuncs.com:3306)/test
db, err := engine.Open("mysql", "") //此处请按格式填写你的mysql链接,这里用*号代替
if err != nil {
panic(err.Error())
}
db.SetMaxOpenConns(200) //连接池最大打开数
var bytes = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://raw.githubusercontent.com/zhuxiujia/GoMybatis/master/mybatis-3-mapper.dtd">
<mapper>
<resultMap id="BaseResultMap" type="example.Activity">
<id column="id" langType="id"/>
<result column="name" property="name" langType="string"/>
<result column="pc_link" property="pcLink" langType="string"/>
<result column="h5_link" property="h5Link" langType="string"/>
<result column="remark" property="remark" langType="string"/>
<result column="create_time" property="createTime" langType="time.Time"/>
<result column="delete_flag" property="deleteFlag" langType="int"/>
</resultMap>
<!--List<Activity> selectByCondition(@Param("name") String name,@Param("startTime") Date startTime,@Param("endTime") Date endTime,@Param("index") Integer index,@Param("size") Integer size);-->
<!-- 后台查询产品 -->
<select id="selectByCondition" resultMap="BaseResultMap">
select * from biz_activity where delete_flag=1
<if test="name != nil">
and name like #{name}
</if>
<if test="startTime != nil">
and create_time >= #{startTime}
</if>
<if test="endTime != nil">
and create_time <= #{endTime}
</if>
order by create_time desc
<if test="page != nil and size != nil">limit #{page}, #{size}</if>
</select>`)
var exampleActivityMapperImpl ExampleActivityMapperImpl
//设置对应的mapper xml文件,禁止输出日志
engine.SetLogEnable(false)
engine.WriteMapperPtr(&exampleActivityMapperImpl, bytes)
return exampleActivityMapperImpl
}