Skip to content

Commit a1c8a3c

Browse files
committed
test: simplified and added a few more benchmarks
1 parent 9393a5a commit a1c8a3c

3 files changed

Lines changed: 74 additions & 5 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test-coverage:
1212
go tool cover -html=./coverage/coverage.out -o ./coverage/index.html
1313
@echo "\033[0;32mCoverage report generated at ./coverage/index.html.\033[0m"
1414
benchmark:
15-
go test -bench=. $(GO_BENCH_ARGS) -tags=benchmark ./tests/benchmarks/...
15+
go test -bench=. $(GO_BENCH_ARGS) -benchmem -tags=benchmark ./tests/benchmarks/...
1616
b1x:
1717
make benchmark GO_BENCH_ARGS="-benchtime=1x"
1818
b10x:

tests/benchmarks/core_create_benchmark_test.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ package benchmarks
44

55
import (
66
"testing"
7+
"time"
78

89
. "github.com/elcengine/elemental/tests/fixtures"
10+
"github.com/elcengine/elemental/tests/fixtures/mocks"
911
ts "github.com/elcengine/elemental/tests/fixtures/setup"
1012
"github.com/google/uuid"
13+
"github.com/samber/lo"
14+
"go.mongodb.org/mongo-driver/bson/primitive"
1115
)
1216

1317
func BenchmarkCoreCreate(b *testing.B) {
14-
b.ReportAllocs()
15-
1618
ts.Connection(b.Name())
1719

1820
UserModel := UserModel.SetDatabase(b.Name())
@@ -25,8 +27,6 @@ func BenchmarkCoreCreate(b *testing.B) {
2527
}
2628

2729
func BenchmarkCoreCreateDriver(b *testing.B) {
28-
b.ReportAllocs()
29-
3030
ts.Connection(b.Name())
3131

3232
coll := UserModel.SetDatabase(b.Name()).Collection()
@@ -37,3 +37,38 @@ func BenchmarkCoreCreateDriver(b *testing.B) {
3737
})
3838
}
3939
}
40+
41+
func BenchmarkCoreCreateMany(b *testing.B) {
42+
ts.Connection(b.Name())
43+
44+
UserModel := UserModel.SetDatabase(b.Name())
45+
46+
docs := lo.Map(mocks.Users, func(user User, _ int) User {
47+
user.Name = uuid.NewString()
48+
return user
49+
})
50+
51+
for b.Loop() {
52+
UserModel.CreateMany(docs).ExecTT()
53+
UserModel.Drop()
54+
}
55+
}
56+
57+
func BenchmarkCoreCreateManyDriver(b *testing.B) {
58+
ts.Connection(b.Name())
59+
60+
coll := UserModel.SetDatabase(b.Name()).Collection()
61+
62+
docs := lo.Map(mocks.Users, func(user User, _ int) any {
63+
user.ID = primitive.NewObjectID()
64+
user.Name = uuid.NewString()
65+
user.CreatedAt = time.Now()
66+
user.UpdatedAt = time.Now()
67+
return user
68+
})
69+
70+
for b.Loop() {
71+
coll.InsertMany(b.Context(), docs)
72+
coll.Drop(b.Context())
73+
}
74+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//go:build benchmark
2+
3+
package benchmarks
4+
5+
import (
6+
"testing"
7+
8+
. "github.com/elcengine/elemental/tests/fixtures"
9+
ts "github.com/elcengine/elemental/tests/fixtures/setup"
10+
"github.com/samber/lo"
11+
"go.mongodb.org/mongo-driver/bson/primitive"
12+
)
13+
14+
func BenchmarkCoreRead(b *testing.B) {
15+
ts.SeededConnection(b.Name())
16+
17+
UserModel := UserModel.SetDatabase(b.Name())
18+
19+
for b.Loop() {
20+
UserModel.Find().ExecTT(b.Context())
21+
}
22+
}
23+
24+
func BenchmarkCoreReadDriver(b *testing.B) {
25+
ts.SeededConnection(b.Name())
26+
27+
coll := UserModel.SetDatabase(b.Name()).Collection()
28+
29+
for b.Loop() {
30+
var users []User
31+
cursor := lo.Must(coll.Find(b.Context(), primitive.M{}))
32+
lo.Must0(cursor.All(b.Context(), &users))
33+
}
34+
}

0 commit comments

Comments
 (0)