This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathallocator_test.go
94 lines (84 loc) · 2.09 KB
/
allocator_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
84
85
86
87
88
89
90
91
92
93
94
package tools_test
import (
"testing"
"github.com/ronaksoft/rony"
"github.com/ronaksoft/rony/tools"
. "github.com/smartystreets/goconvey/convey"
"google.golang.org/protobuf/proto"
)
/*
Creation Time: 2020 - Nov - 10
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
type Alias int32
const (
Alias_A1 Alias = iota + 1
Alias_A2
Alias_A3
)
func TestNewAllocator(t *testing.T) {
Convey("Allocator", t, func(c C) {
alloc := tools.NewAllocator()
b := alloc.Gen(Alias_A2)
c.So(b, ShouldHaveLength, 5)
b = alloc.Gen(Alias_A2, Alias_A1)
c.So(b, ShouldHaveLength, 9)
b = alloc.Gen(Alias_A1, "TXT1")
c.So(b, ShouldHaveLength, 9)
b = alloc.Gen(Alias_A1, 3232)
c.So(b, ShouldHaveLength, 13)
b = alloc.Gen(Alias_A1, 3232, []byte("TXT1"))
c.So(b, ShouldHaveLength, 17)
alloc.ReleaseAll()
})
}
func BenchmarkBulkKey_Gen(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
bk := tools.NewAllocator()
d := bk.Gen(tools.FastRand(), "tools.RandomID(10)", 3, 125)
if len(d) != 38 {
b.Fatal("invalid size", len(d))
}
bk.ReleaseAll()
}
})
}
func BenchmarkMarshalWithAllocator(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
m := rony.PoolMessageEnvelope.Get()
m.RequestID = tools.RandomUint64(0)
m.Constructor = 3232
m.Message = append(m.Message, tools.StrToByte("Something here for test ONLY!")...)
bk := tools.NewAllocator()
d := bk.Marshal(m)
if len(d) != proto.Size(m) {
b.Fatal("invalid size", len(d))
}
bk.ReleaseAll()
rony.PoolMessageEnvelope.Put(m)
}
})
}
func BenchmarkMarshalWithoutAllocator(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
m := &rony.MessageEnvelope{}
m.RequestID = tools.RandomUint64(0)
m.Constructor = 3232
m.Message = append(m.Message, tools.StrToByte("Something here for test ONLY!")...)
d, _ := proto.Marshal(m)
if len(d) != proto.Size(m) {
b.Fatal("invalid size", len(d))
}
}
})
}