-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark_test.go
105 lines (84 loc) · 2.49 KB
/
benchmark_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
package di
import (
"io"
"strings"
"testing"
)
func addServicesWithFactory(cb ContainerBuilder) {
cb.Add(TransientFactory[io.ReadWriter](func(c Container) any { return readWriterFactory(c) }))
cb.Add(TransientFactory[io.Reader](func(c Container) any { return strings.NewReader("") }))
cb.Add(TransientFactory[io.Writer](func(c Container) any { return &strings.Builder{} }))
}
func addServicesWithConstructor(cb ContainerBuilder) {
cb.Add(Transient[io.ReadWriter](
func(r io.Reader, w io.Writer) *readWriter { return &readWriter{Reader: r, Writer: w} }))
cb.Add(Transient[io.Reader](func() *strings.Reader { return strings.NewReader("") }))
cb.Add(Transient[io.Writer](func() *strings.Builder { return &strings.Builder{} }))
}
func addServicesWithSingleton(cb ContainerBuilder) {
cb.Add(Singleton[io.ReadWriter](
func(r io.Reader, w io.Writer) *readWriter { return &readWriter{Reader: r, Writer: w} }))
cb.Add(Singleton[io.Reader](func() *strings.Reader { return strings.NewReader("") }))
cb.Add(Singleton[io.Writer](func() *strings.Builder { return &strings.Builder{} }))
}
func addServicesWithScoped(cb ContainerBuilder) {
cb.Add(Scoped[io.ReadWriter](
func(r io.Reader, w io.Writer) *readWriter { return &readWriter{Reader: r, Writer: w} }))
cb.Add(Scoped[io.Reader](func() *strings.Reader { return strings.NewReader("") }))
cb.Add(Scoped[io.Writer](func() *strings.Builder { return &strings.Builder{} }))
}
func buildContainer(mode string) Container {
cb := Builder()
cb.ConfigureOptions(func(o *Options) {
o.ValidateScopes = true
o.ValidateOnBuild = true
})
switch mode {
case "factory":
addServicesWithFactory(cb)
case "constructor":
addServicesWithConstructor(cb)
case "singleton":
addServicesWithSingleton(cb)
case "scoped":
addServicesWithScoped(cb)
}
c := cb.Build()
c = Get[ScopeFactory](c).CreateScope().Container()
return c
}
func resolve(c Container) {
_ = Get[io.ReadWriter](c)
}
func Benchmark_Factory(b *testing.B) {
c := buildContainer("factory")
resolve(c)
b.ResetTimer()
for i := 0; i < b.N; i++ {
resolve(c)
}
}
func Benchmark_Constructor(b *testing.B) {
c := buildContainer("constructor")
resolve(c)
b.ResetTimer()
for i := 0; i < b.N; i++ {
resolve(c)
}
}
func Benchmark_Singleton(b *testing.B) {
c := buildContainer("singleton")
resolve(c)
b.ResetTimer()
for i := 0; i < b.N; i++ {
resolve(c)
}
}
func Benchmark_Scoped(b *testing.B) {
c := buildContainer("scoped")
resolve(c)
b.ResetTimer()
for i := 0; i < b.N; i++ {
resolve(c)
}
}