-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathget_by_id_benchmarks_test.go
70 lines (57 loc) · 1.82 KB
/
get_by_id_benchmarks_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
package beeorm
import (
"testing"
"github.com/stretchr/testify/assert"
)
type getByIDBenchmarkEntity struct {
ID uint64 `orm:"localCache;redisCache"`
Name string
}
type getByIDBenchmarkEntityLimit struct {
ID uint64 `orm:"localCache=10"`
Name string
}
// BenchmarkGetByIDLocalCache-10 62160598 19.32 ns/op 0 B/op 0 allocs/op
func BenchmarkGetByIDLocalCacheNoLimit(b *testing.B) {
benchmarkGetByIDCache(b, true, false)
}
// BenchmarkGetByIDLocalCacheLimit-10 42071001 28.35 ns/op 0 B/op 0 allocs/op
func BenchmarkGetByIDLocalCacheLimit(b *testing.B) {
benchmarkGetByIDLocalCacheLimit(b)
}
// BenchmarkGetByIDRedisCache-10 1966 590329 ns/op 272 B/op 10 allocs/op
func BenchmarkGetByIDRedisCacheNoLimit(b *testing.B) {
benchmarkGetByIDCache(b, false, true)
}
func benchmarkGetByIDCache(b *testing.B, local, redis bool) {
var entity *getByIDBenchmarkEntity
registry := NewRegistry()
orm := PrepareTables(nil, registry, entity)
schema := GetEntitySchema[getByIDBenchmarkEntity](orm)
schema.DisableCache(!local, !redis)
entity = NewEntity[getByIDBenchmarkEntity](orm)
entity.Name = "Name"
err := orm.Flush()
assert.NoError(b, err)
GetByID[getByIDBenchmarkEntity](orm, entity.ID)
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
GetByID[getByIDBenchmarkEntity](orm, entity.ID)
}
}
func benchmarkGetByIDLocalCacheLimit(b *testing.B) {
var entity *getByIDBenchmarkEntityLimit
registry := NewRegistry()
orm := PrepareTables(nil, registry, entity)
entity = NewEntity[getByIDBenchmarkEntityLimit](orm)
entity.Name = "Name"
err := orm.Flush()
assert.NoError(b, err)
GetByID[getByIDBenchmarkEntityLimit](orm, entity.ID)
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
GetByID[getByIDBenchmarkEntityLimit](orm, entity.ID)
}
}