Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize DB performance #965

Merged
merged 3 commits into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions database/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import (
"github.com/goravel/framework/support/str"
)

var (
traceStr = "[%.3fms] [rows:%v] %s"
traceWarnStr = "[%.3fms] [rows:%v] [SLOW] %s"
traceErrStr = "[%.3fms] [rows:%v] %s\t%s"
)

func NewLogger(config config.Config, log log.Log) logger.Logger {
level := logger.Warn
if config.GetBool("app.debug") {
Expand Down Expand Up @@ -86,12 +92,6 @@ func (r *Logger) Trace(ctx context.Context, begin carbon.Carbon, sql string, row
return
}

var (
traceStr = "[%.3fms] [rows:%v] %s"
traceWarnStr = "[%.3fms] [rows:%v] [SLOW] %s"
traceErrStr = "[%.3fms] [rows:%v] %s\t%s"
)

elapsed := begin.DiffInDuration()

switch {
Expand Down
16 changes: 16 additions & 0 deletions tests/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1458,3 +1458,19 @@ func TestDbReadWriteSeparate(t *testing.T) {
assert.NoError(t, err)
assert.NoError(t, docker.Shutdown())
}

func Benchmark_DB(b *testing.B) {
query := NewTestQueryBuilder().Postgres("", false)
query.CreateTable(TestTableAuthors)

b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := query.DB().Table("authors").Insert(Author{
Name: "benchmark",
})
if err != nil {
b.Error(err)
}
}
}
2 changes: 1 addition & 1 deletion tests/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ type Book struct {
type Author struct {
Model
BookID uint
Name string
Name string `db:"name"`
}

func (r *Author) Factory() factory.Factory {
Expand Down
16 changes: 16 additions & 0 deletions tests/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3725,3 +3725,19 @@ func paginator(page string, limit string) func(methods contractsorm.Query) contr
return query.Offset(offset).Limit(limit)
}
}

func Benchmark_Orm(b *testing.B) {
query := NewTestQueryBuilder().Postgres("", false)
query.CreateTable(TestTableAuthors)

b.ResetTimer()

for i := 0; i < b.N; i++ {
err := query.Query().Model(&Author{}).Create(map[string]any{
"name": "benchmark",
})
if err != nil {
b.Error(err)
}
}
}