-
Notifications
You must be signed in to change notification settings - Fork 1
/
impl_bench_test.go
118 lines (102 loc) · 3.11 KB
/
impl_bench_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright (c) 2021-present unTill Pro, Ltd.
*/
package ibusmem
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/require"
ibus "github.com/untillpro/airs-ibus"
)
// BenchmarkSectionedRequestResponse/#00-4 51944 23527 ns/op 42505 rps 4229 B/op 69 allocs/op
func BenchmarkSectionedRequestResponse(b *testing.B) {
var bus ibus.IBus
bus = Provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
rs := bus.SendParallelResponse2(sender)
go func() {
require.Nil(b, rs.ObjectSection("secObj", []string{"meta"}, "elem"))
rs.StartMapSection("secMap", []string{"classifier", "2"})
require.Nil(b, rs.SendElement("id1", "elem"))
require.Nil(b, rs.SendElement("id2", "elem"))
rs.StartArraySection("secArr", []string{"classifier", "4"})
require.Nil(b, rs.SendElement("", "arrEl1"))
require.Nil(b, rs.SendElement("", "arrEl2"))
rs.StartMapSection("deps", []string{"classifier", "3"})
require.Nil(b, rs.SendElement("id3", "elem"))
require.Nil(b, rs.SendElement("id4", "elem"))
rs.Close(errors.New("test error"))
}()
})
b.Run("", func(b *testing.B) {
start := time.Now()
for i := 0; i < b.N; i++ {
ctx := context.Background()
_, sections, _, _ := bus.SendRequest2(ctx, ibus.Request{}, ibus.DefaultTimeout)
section := <-sections
secObj := section.(ibus.IObjectSection)
secObj.Value(ctx)
section = <-sections
secMap := section.(ibus.IMapSection)
secMap.Next(ctx)
secMap.Next(ctx)
section = <-sections
secArr := section.(ibus.IArraySection)
secArr.Next(ctx)
secArr.Next(ctx)
section = <-sections
secMap = section.(ibus.IMapSection)
secMap.Next(ctx)
secMap.Next(ctx)
if _, ok := <-sections; ok {
b.Fatal()
}
}
elapsed := time.Since(start).Seconds()
b.ReportMetric(float64(b.N)/elapsed, "rps")
})
}
func BenchmarkOneSectionElement(b *testing.B) {
var bus ibus.IBus
bus = Provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
rs := bus.SendParallelResponse2(sender)
go func() {
require.Nil(b, rs.ObjectSection("secObj", nil, "hello"))
rs.Close(nil)
}()
})
b.Run("", func(b *testing.B) {
start := time.Now()
for i := 0; i < b.N; i++ {
ctx := context.Background()
_, sections, _, _ := bus.SendRequest2(ctx, ibus.Request{}, ibus.DefaultTimeout)
section := <-sections
secObj := section.(ibus.IObjectSection)
if len(secObj.Value(ctx)) != 7 { // "hello"
b.Fatal()
}
}
elapsed := time.Since(start).Seconds()
b.ReportMetric(float64(b.N)/elapsed, "rps")
})
}
func BenchmarkNonsectionedResponse(b *testing.B) {
var bus ibus.IBus
bus = Provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
bus.SendResponse(sender, ibus.Response{
Data: []byte("hello"),
})
})
b.Run("", func(b *testing.B) {
start := time.Now()
for i := 0; i < b.N; i++ {
resp, _, _, _ := bus.SendRequest2(context.Background(), ibus.Request{}, ibus.DefaultTimeout)
if len(resp.Data) != 5 {
b.Fatal()
}
}
elapsed := time.Since(start).Seconds()
b.ReportMetric(float64(b.N)/elapsed, "rps")
})
}