Skip to content

Commit

Permalink
feat: add gorm v1
Browse files Browse the repository at this point in the history
  • Loading branch information
kimi0230 committed Aug 18, 2024
1 parent f8fb809 commit 7208490
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 2 deletions.
136 changes: 136 additions & 0 deletions bench/gorm_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package bench

import (
"testing"

"github.com/efectn/go-orm-benchmarks/helper"
gormV1 "github.com/jinzhu/gorm" // 使用 GORM v1
_ "github.com/lib/pq" // 引入 PostgreSQL 驅動
)

type GormV1 struct {
helper.ORMInterface
conn *gormV1.DB
}

func CreateGormV1() helper.ORMInterface {
return &GormV1{}
}

func (gorm *GormV1) Name() string {
return "gormV1"
}

func (gorm *GormV1) Init() error {
var err error
gorm.conn, err = gormV1.Open("postgres", helper.OrmSource) // 使用 GORM v1 的 Open 方法
if err != nil {
return err
}

gorm.conn.LogMode(false) // 關閉日誌模式,相當於 v2 的 Logger.Silent
gorm.conn.DB().SetMaxOpenConns(100) // 設定資料庫連線最大開啟數量
gorm.conn.DB().SetMaxIdleConns(10) // 設定資料庫連線最大空閒數量
gorm.conn.SingularTable(true) // 如果需要禁用複數表名,這個選項是 GORM v1 的特色
return nil
}

func (gorm *GormV1) Close() error {
return gorm.conn.Close() // GORM v1 使用 Close 方法直接關閉連線
}

func (gorm *GormV1) Insert(b *testing.B) {
m := NewModel()

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
m.Id = 0
err := gorm.conn.Create(m).Error
if err != nil {
helper.SetError(b, gorm.Name(), "Insert", err.Error())
}
}
}

// Gormv1 不支援多次插入
func (gorm *GormV1) InsertMulti(b *testing.B) {
ms := make([]*Model, 0, 100)
for i := 0; i < 100; i++ {
ms = append(ms, NewModel())
}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
for _, m := range ms {
m.Id = 0
err := gorm.conn.Create(&m).Error
if err != nil {
helper.SetError(b, gorm.Name(), "InsertMulti", err.Error())
}
}
}
}

func (gorm *GormV1) Update(b *testing.B) {
m := NewModel()

err := gorm.conn.Create(m).Error
if err != nil {
helper.SetError(b, gorm.Name(), "Update", err.Error())
}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
err := gorm.conn.Model(m).Updates(m).Error
if err != nil {
helper.SetError(b, gorm.Name(), "Update", err.Error())
}
}
}

func (gorm *GormV1) Read(b *testing.B) {
m := NewModel()

err := gorm.conn.Create(m).Error
if err != nil {
helper.SetError(b, gorm.Name(), "Read", err.Error())
}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
err := gorm.conn.Take(m).Error
if err != nil {
helper.SetError(b, gorm.Name(), "Read", err.Error())
}
}
}

func (gorm *GormV1) ReadSlice(b *testing.B) {
m := NewModel()
for i := 0; i < 100; i++ {
m.Id = 0
err := gorm.conn.Create(m).Error
if err != nil {
helper.SetError(b, gorm.Name(), "ReadSlice", err.Error())
}
}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
var models []*Model
err := gorm.conn.Where("id > ?", 0).Limit(100).Find(&models).Error
if err != nil {
helper.SetError(b, gorm.Name(), "ReadSlice", err.Error())
}
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ require (
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx/v5 v5.4.2 // indirect
github.com/jackc/puddle v1.3.0 // indirect
github.com/jinzhu/gorm v1.9.16 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ github.com/cupcake/rdb v0.0.0-20161107195141-43ba34106c76/go.mod h1:vYwsqCOLxGii
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/denisenkom/go-mssqldb v0.0.0-20200910202707-1e08a3fab204/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/denisenkom/go-mssqldb v0.9.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/denisenkom/go-mssqldb v0.10.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
Expand Down Expand Up @@ -201,6 +202,7 @@ github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/ericlagergren/decimal v0.0.0-20211103172832-aca2edc11f73/go.mod h1:5sruVSMrZCk0U4hwRaGD0D8wIMFVsBWQqG74jQDFg4k=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
Expand Down Expand Up @@ -549,8 +551,11 @@ github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dv
github.com/jackc/puddle v1.2.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.3.0 h1:eHK/5clGOatcjX3oWGBO/MpxpbHzSwud5EWTSCI+MX0=
github.com/jackc/puddle v1.3.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o=
github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
Expand Down Expand Up @@ -600,6 +605,7 @@ github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+
github.com/ledisdb/ledisdb v0.0.0-20200510135210-d35789ec47e6/go.mod h1:n931TsDuKuq+uX4v1fulaMbA/7ZLLhjc85h7chZGBCQ=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
Expand Down Expand Up @@ -964,6 +970,7 @@ golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var defaultBenchmarkNames = []string{
"pg", "pgx", "pgx_pool", "pop",
"raw", "reform", "rel", "sqlboiler",
"sqlc", "sqlx", "upper", "xorm",
"zorm", "gen", "jet", "sq",
"zorm", "gen", "jet", "sq", "gormV1",
}

type ListOpts []string
Expand Down Expand Up @@ -126,6 +126,7 @@ func runBenchmarks(orms ListOpts) {
"gen": bench.CreateGen(),
"jet": bench.CreateJet(),
"sq": bench.CreateSq(),
"gormV1": bench.CreateGormV1(),
}

table := new(tabwriter.Writer)
Expand Down
2 changes: 1 addition & 1 deletion run_benchmarks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ escaped=$(echo "${logs}" | sed '$!s@$@\\@g')
proto=$(sed "s|@(benchmark-results)|${escaped}|g" <<< $proto)

# Print final results
echo "$proto"
echo "$proto"

0 comments on commit 7208490

Please sign in to comment.