Skip to content

Commit

Permalink
improve project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Jun 20, 2023
1 parent 9aead19 commit 044a4de
Show file tree
Hide file tree
Showing 63 changed files with 545 additions and 820 deletions.
40 changes: 13 additions & 27 deletions benchs/beego.go → bench/beego.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package benchs
package bench

import (
"fmt"
"github.com/astaxie/beego/orm"
"github.com/efectn/go-orm-benchmarks/helper"
"sync"
"testing"
)

type Beego struct {
Instance
helper.ORMInterface
mu sync.Mutex
conn orm.Ormer
iterations int // Same as b.N, just to customize it
errors map[string]string
}

func CreateBeego(iterations int) Instance {
func CreateBeego(iterations int) helper.ORMInterface {
beego := &Beego{
iterations: iterations,
errors: map[string]string{},
}

return beego
Expand All @@ -29,7 +28,7 @@ func (beego *Beego) Name() string {
}

func (beego *Beego) Init() error {
err := orm.RegisterDataBase("default", "postgres", OrmSource, OrmMaxIdle, OrmMaxConn)
err := orm.RegisterDataBase("default", "postgres", helper.OrmSource, helper.OrmMaxIdle, helper.OrmMaxConn)
if err != nil {
return err
}
Expand All @@ -44,10 +43,6 @@ func (beego *Beego) Close() error {
return nil
}

func (beego *Beego) GetError(method string) string {
return beego.errors[method]
}

func (beego *Beego) Insert(b *testing.B) {
m := NewModel()

Expand All @@ -59,7 +54,7 @@ func (beego *Beego) Insert(b *testing.B) {

_, err := beego.conn.Insert(m)
if err != nil {
beego.error(b, "insert", fmt.Sprintf("beego: insert: %v", err))
helper.SetError(b, beego.Name(), "insert", fmt.Sprintf("beego: insert: %v", err))
}
}
}
Expand All @@ -76,7 +71,7 @@ func (beego *Beego) InsertMulti(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := beego.conn.InsertMulti(100, ms)
if err != nil {
beego.error(b, "insert_multi", fmt.Sprintf("beego: insert_multi: %v", err))
helper.SetError(b, beego.Name(), "insert_multi", err.Error())
}
}
}
Expand All @@ -86,7 +81,7 @@ func (beego *Beego) Update(b *testing.B) {

_, err := beego.conn.Insert(m)
if err != nil {
b.Errorf("beego: update: %v", err)
helper.SetError(b, beego.Name(), "update", err.Error())
}

b.ReportAllocs()
Expand All @@ -95,7 +90,7 @@ func (beego *Beego) Update(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := beego.conn.Update(m)
if err != nil {
b.Errorf("beego: update: %v", err)
helper.SetError(b, beego.Name(), "update", err.Error())
}
}
}
Expand All @@ -105,7 +100,7 @@ func (beego *Beego) Read(b *testing.B) {

_, err := beego.conn.Insert(m)
if err != nil {
b.Errorf("beego: read: %v", err)
helper.SetError(b, beego.Name(), "read", err.Error())
}

b.ReportAllocs()
Expand All @@ -114,7 +109,7 @@ func (beego *Beego) Read(b *testing.B) {
for i := 0; i < b.N; i++ {
err := beego.conn.Read(m)
if err != nil {
b.Errorf("beego: read: %v", err)
helper.SetError(b, beego.Name(), "read", err.Error())
}
}
}
Expand All @@ -125,7 +120,7 @@ func (beego *Beego) ReadSlice(b *testing.B) {
m.Id = 0
_, err := beego.conn.Insert(m)
if err != nil {
b.Errorf("beego: read_slice: %v", err)
helper.SetError(b, beego.Name(), "read_slice", err.Error())
}
}

Expand All @@ -136,16 +131,7 @@ func (beego *Beego) ReadSlice(b *testing.B) {
var models []*Model
_, err := beego.conn.QueryTable("models").Filter("id__gt", 0).Limit(100).All(&models)
if err != nil {
b.Errorf("beego: read_slice: %v", err)
helper.SetError(b, beego.Name(), "read_slice", err.Error())
}
}
}

func (beego *Beego) error(b *testing.B, method string, err string) {
b.Helper()

beego.mu.Lock()
beego.errors[method] = err
beego.mu.Unlock()
b.Fail()
}
45 changes: 15 additions & 30 deletions benchs/bun.go → bench/bun.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package benchs
package bench

import (
"database/sql"
"fmt"
"github.com/efectn/go-orm-benchmarks/helper"
"sync"
"testing"

Expand All @@ -12,17 +12,15 @@ import (
)

type Bun struct {
Instance
helper.ORMInterface
mu sync.Mutex
conn *bundb.DB
iterations int // Same as b.N, just to customize it
errors map[string]string
}

func CreateBun(iterations int) Instance {
func CreateBun(iterations int) helper.ORMInterface {
bun := &Bun{
iterations: iterations,
errors: map[string]string{},
}

return bun
Expand All @@ -33,9 +31,9 @@ func (bun *Bun) Name() string {
}

func (bun *Bun) Init() error {
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(ConvertSourceToDSN())))
sqldb.SetMaxOpenConns(OrmMaxConn)
sqldb.SetMaxIdleConns(OrmMaxIdle)
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(helper.ConvertSourceToDSN())))
sqldb.SetMaxOpenConns(helper.OrmMaxConn)
sqldb.SetMaxIdleConns(helper.OrmMaxIdle)

bun.conn = bundb.NewDB(sqldb, pgdialect.New())
return nil
Expand All @@ -45,10 +43,6 @@ func (bun *Bun) Close() error {
return bun.conn.Close()
}

func (bun *Bun) GetError(method string) string {
return bun.errors[method]
}

func (bun *Bun) Insert(b *testing.B) {
m := NewModel()

Expand All @@ -60,7 +54,7 @@ func (bun *Bun) Insert(b *testing.B) {

_, err := bun.conn.NewInsert().Model(m).Exec(ctx)
if err != nil {
bun.error(b, "insert", fmt.Sprintf("bun: insert: %v", err))
helper.SetError(b, bun.Name(), "insert", err.Error())
}
}
}
Expand All @@ -81,7 +75,7 @@ func (bun *Bun) InsertMulti(b *testing.B) {

_, err := bun.conn.NewInsert().Model(&ms).Exec(ctx)
if err != nil {
bun.error(b, "insert_multi", fmt.Sprintf("bun: insert_multi: %v", err))
helper.SetError(b, bun.Name(), "insert_multi", err.Error())
}
}
}
Expand All @@ -91,7 +85,7 @@ func (bun *Bun) Update(b *testing.B) {

_, err := bun.conn.NewInsert().Model(m).Exec(ctx)
if err != nil {
bun.error(b, "update", fmt.Sprintf("bun: update: %v", err))
helper.SetError(b, bun.Name(), "update", err.Error())
}

b.ReportAllocs()
Expand All @@ -100,7 +94,7 @@ func (bun *Bun) Update(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := bun.conn.NewUpdate().Model(m).WherePK().Exec(ctx)
if err != nil {
bun.error(b, "update", fmt.Sprintf("bun: update: %v", err))
helper.SetError(b, bun.Name(), "update", err.Error())
}
}
}
Expand All @@ -110,7 +104,7 @@ func (bun *Bun) Read(b *testing.B) {

_, err := bun.conn.NewInsert().Model(m).Exec(ctx)
if err != nil {
bun.error(b, "read", fmt.Sprintf("bun: read: %v", err))
helper.SetError(b, bun.Name(), "read", err.Error())
}

b.ReportAllocs()
Expand All @@ -119,7 +113,7 @@ func (bun *Bun) Read(b *testing.B) {
for i := 0; i < b.N; i++ {
err := bun.conn.NewSelect().Model(m).Scan(ctx)
if err != nil {
bun.error(b, "read", fmt.Sprintf("bun: read: %v", err))
helper.SetError(b, bun.Name(), "read", err.Error())
}
}
}
Expand All @@ -130,7 +124,7 @@ func (bun *Bun) ReadSlice(b *testing.B) {
m.Id = 0
_, err := bun.conn.NewInsert().Model(m).Exec(ctx)
if err != nil {
bun.error(b, "read_slice", fmt.Sprintf("bun: read_slice: %v", err))
helper.SetError(b, bun.Name(), "read_slice", err.Error())
}
}

Expand All @@ -145,16 +139,7 @@ func (bun *Bun) ReadSlice(b *testing.B) {
Limit(100).
Scan(ctx)
if err != nil {
bun.error(b, "read_slice", fmt.Sprintf("bun: read_slice: %v", err))
helper.SetError(b, bun.Name(), "read_slice", err.Error())
}
}
}

func (bun *Bun) error(b *testing.B, method string, err string) {
b.Helper()

bun.mu.Lock()
bun.errors[method] = err
bun.mu.Unlock()
b.Fail()
}
41 changes: 13 additions & 28 deletions benchs/dbr.go → bench/dbr.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package benchs
package bench

import (
"fmt"
"github.com/efectn/go-orm-benchmarks/helper"
"sync"
"testing"

Expand All @@ -12,17 +12,15 @@ import (
var columns = []string{"name", "title", "fax", "web", "age", "right", "counter"}

type Dbr struct {
Instance
helper.ORMInterface
mu sync.Mutex
conn *dbrware.Session
iterations int // Same as b.N, just to customize it
errors map[string]string
}

func CreateDbr(iterations int) Instance {
func CreateDbr(iterations int) helper.ORMInterface {
dbr := &Dbr{
iterations: iterations,
errors: map[string]string{},
}

return dbr
Expand All @@ -33,7 +31,7 @@ func (dbr *Dbr) Name() string {
}

func (dbr *Dbr) Init() error {
conn, err := dbrware.Open("postgres", OrmSource, nil)
conn, err := dbrware.Open("postgres", helper.OrmSource, nil)
if err != nil {
return err
}
Expand All @@ -51,10 +49,6 @@ func (dbr *Dbr) Close() error {
return dbr.conn.Close()
}

func (dbr *Dbr) GetError(method string) string {
return dbr.errors[method]
}

func (dbr *Dbr) Insert(b *testing.B) {
m := NewModel2()

Expand All @@ -64,21 +58,21 @@ func (dbr *Dbr) Insert(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := dbr.conn.InsertInto("models").Columns(columns...).Record(m).Exec()
if err != nil {
dbr.error(b, "insert", fmt.Sprintf("dbr: insert: %v", err))
helper.SetError(b, dbr.Name(), "insert", err.Error())
}
}
}

func (dbr *Dbr) InsertMulti(b *testing.B) {
dbr.error(b, "insert_multi", "dbr: insert_multi: insert multi is not supported on dbr")
helper.SetError(b, dbr.Name(), "insert_multi", "insert multi is not supported on dbr")
}

func (dbr *Dbr) Update(b *testing.B) {
m := NewModel2()

_, err := dbr.conn.InsertInto("models").Columns(columns...).Record(m).Exec()
if err != nil {
dbr.error(b, "update", fmt.Sprintf("dbr: update: %v", err))
helper.SetError(b, dbr.Name(), "update", err.Error())
}

b.ReportAllocs()
Expand All @@ -95,7 +89,7 @@ func (dbr *Dbr) Update(b *testing.B) {
"counter": m.Counter,
}).Exec()
if err != nil {
dbr.error(b, "update", fmt.Sprintf("dbr: update: %v", err))
helper.SetError(b, dbr.Name(), "update", err.Error())
}
}
}
Expand All @@ -105,7 +99,7 @@ func (dbr *Dbr) Read(b *testing.B) {

_, err := dbr.conn.InsertInto("models").Columns(columns...).Record(m).Exec()
if err != nil {
dbr.error(b, "read", fmt.Sprintf("dbr: read: %v", err))
helper.SetError(b, dbr.Name(), "read", err.Error())
}

b.ReportAllocs()
Expand All @@ -114,7 +108,7 @@ func (dbr *Dbr) Read(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := dbr.conn.Select("*").From("models").Where("id = ?", m.ID).Load(m)
if err != nil {
dbr.error(b, "read", fmt.Sprintf("dbr: read: %v", err))
helper.SetError(b, dbr.Name(), "read", err.Error())
}
}
}
Expand All @@ -124,7 +118,7 @@ func (dbr *Dbr) ReadSlice(b *testing.B) {
for i := 0; i < 100; i++ {
_, err := dbr.conn.InsertInto("models").Columns(columns...).Record(m).Exec()
if err != nil {
dbr.error(b, "read_slice", fmt.Sprintf("dbr: read_slice: %v", err))
helper.SetError(b, dbr.Name(), "read_slice", err.Error())
}
}

Expand All @@ -135,16 +129,7 @@ func (dbr *Dbr) ReadSlice(b *testing.B) {
var ms []*Model2
_, err := dbr.conn.Select("*").From("models").Where("id > 0").Limit(100).Load(&ms)
if err != nil {
dbr.error(b, "read_slice", fmt.Sprintf("dbr: read_slice: %v", err))
helper.SetError(b, dbr.Name(), "read_slice", err.Error())
}
}
}

func (dbr *Dbr) error(b *testing.B, method string, err string) {
b.Helper()

dbr.mu.Lock()
dbr.errors[method] = err
dbr.mu.Unlock()
b.Fail()
}
Loading

0 comments on commit 044a4de

Please sign in to comment.