-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
81 lines (70 loc) · 1.93 KB
/
benchmark_test.go
File metadata and controls
81 lines (70 loc) · 1.93 KB
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
package gg
import (
"fmt"
"github.com/gin-gonic/gin"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"testing"
)
type BenchModel struct {
gorm.Model
Name string
}
func BenchmarkCreateGetDeleteGinGorm(b *testing.B) {
config := Config{
Gorm: &gorm.Config{},
Database: InMemConfig{},
Models: []interface{}{&BenchModel{}},
}
db, _ := config.OpenDB()
db.Migrator().DropTable(&BenchModel{})
db.AutoMigrate(&BenchModel{})
engine := gin.New()
gin.SetMode(gin.TestMode)
engine.Use(Middleware(config))
engine.GET("/:id", SetModel(&BenchModel{}), GetByID("id"))
engine.POST("/", SetModel(&BenchModel{}), BodyCreate())
engine.DELETE("/:id", SetModel(&BenchModel{}), DeleteByID("id"))
body := buildJsonBytes("name", "testing")
var idUrl string
for n := 0; n < b.N; n++ {
resp := runRequest(engine, "POST", "/", body)
mapResp := extractMapBody(resp)
idUrl = fmt.Sprintf("/%.0f", mapResp["id"])
runRequest(engine, "GET", idUrl, nil)
runRequest(engine, "DELETE", idUrl, nil)
}
}
func BenchmarkCreateGetDeleteBare(b *testing.B) {
db, _ := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
db.Migrator().DropTable(&BenchModel{})
db.AutoMigrate(&BenchModel{})
engine := gin.New()
gin.SetMode(gin.TestMode)
engine.GET("/:id", func(ctx *gin.Context) {
id := ctx.Param("id")
var model BenchModel
db.First(&model, id)
ctx.JSON(200, model)
})
engine.POST("/", func(ctx *gin.Context) {
model := BenchModel{}
ctx.BindJSON(&model)
db.Create(&model)
ctx.JSON(200, model)
})
engine.DELETE("/:id", func(ctx *gin.Context) {
var model BenchModel
db.Delete(&model, ctx.Param("id"))
ctx.JSON(200, gin.H{})
})
body := buildJsonBytes("name", "testing")
var idUrl string
for n := 0; n < b.N; n++ {
resp := runRequest(engine, "POST", "/", body)
mapResp := extractMapBody(resp)
idUrl = fmt.Sprintf("/%.0f", mapResp["ID"])
runRequest(engine, "GET", idUrl, nil)
runRequest(engine, "DELETE", idUrl, nil)
}
}