-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinsert_single_test.go
83 lines (64 loc) · 2.23 KB
/
insert_single_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
package benchmarks
import (
"context"
"testing"
"time"
"github.com/google/uuid"
"github.com/kataras/pg"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
// Customer is a struct that represents a customer entity in the database.
type Customer struct {
ID string `pg:"type=uuid,primary"`
CreatedAt time.Time `pg:"type=timestamp,default=clock_timestamp()"`
UpdatedAt time.Time `pg:"type=timestamp,default=clock_timestamp()"`
// CognitoUserID string `pg:"type=uuid,unique,conflict=DO UPDATE SET cognito_user_id=EXCLUDED.cognito_user_id"`
CognitoUserID string `pg:"type=uuid,unique"`
}
var (
dsn = "host=localhost user=postgres password=admin!123 dbname=test_db sslmode=disable search_path=public"
)
// go test -benchtime=5s -benchmem -run=^$ -bench ^BenchmarkDB_Insert*
// go test -bench=BenchmarkDB_InsertSingle_Gorm -count 6 | tee result_gorm.txt
func BenchmarkDB_InsertSingle_Gorm(b *testing.B) {
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
b.Fatal(err)
}
/* To create the schema:
db.AutoMigrate(&Customer{})
*/
// This doesn't even works...
// db.Clauses(clause.OnConflict{DoUpdates: clause.AssignmentColumns([]string{"cognito_user_id"})}).
// db.Clauses(clause.OnConflict{DoUpdates: clause.Assignments(map[string]any{"cognito_user_id": `EXCLUDED.cognito_user_id`})}).
customer := Customer{
CognitoUserID: uuid.NewString(),
}
db.
Omit("id", "created_at", "updated_at").
Create(&customer)
}
// go test -bench=BenchmarkDB_InsertSingle_Pg -count 6 | tee result_pg.txt
func BenchmarkDB_InsertSingle_Pg(b *testing.B) {
var schema = pg.NewSchema().MustRegister("customers", Customer{})
db, err := pg.Open(context.Background(), schema, dsn)
if err != nil {
b.Fatal(err)
}
/* To create the schema:
db.CreateSchema(context.Background())
*/
// Automatically takes care of id, created_at and updated_at fields.
customer := Customer{CognitoUserID: uuid.NewString()}
err = db.InsertSingle(context.Background(), customer, &customer.ID)
if err != nil {
b.Fatal(err)
}
/* To create a record from repository (static types):
repo := pg.NewRepository[Customer](db)
err := repo.InsertSingle(context.Background(), customer, &customer.ID)
*/
}
// benchstat result_gorm.txt result_pg.txt
// ± 351%