-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre_test.go
More file actions
executable file
·119 lines (101 loc) · 2.24 KB
/
Copy pathpre_test.go
File metadata and controls
executable file
·119 lines (101 loc) · 2.24 KB
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
119
package di_test
import (
"reflect"
"testing"
"time"
"github.com/jad21/di"
)
// type testSt struct {
// Msg string
// }
type testFastr func(v1 time.Time, v2 *testSt) string
func (f testFastr) Invoke(args []interface{}) ([]reflect.Value, error) {
s := f(args[0].(time.Time), args[1].(*testSt))
return []reflect.Value{reflect.ValueOf(s)}, nil
}
type testFastRest func(v1 time.Time, v2 *testSt) string
func (f testFastRest) Invoke(args []interface{}) ([]interface{}, error) {
s := f(args[0].(time.Time), args[1].(*testSt))
return []interface{}{s}, nil
}
func TestFastInvoke(t *testing.T) {
vdi := di.New()
val := time.Now()
override := vdi.Maps(val, &testSt{Msg: "The is FastInvoke"})
if len(override) != 0 {
t.Errorf("expected override length 0, got %d", len(override))
}
f := testFastr(func(v1 time.Time, v2 *testSt) string {
t.Log(v1, v2)
return "yes"
})
t.Log(di.IsPreInvoker(f))
invoke, err := vdi.Invoke(f)
t.Log(invoke, err)
fr := testFastRest(func(v1 time.Time, v2 *testSt) string {
t.Log(v1, v2)
return "yes"
})
t.Log(di.IsPreInvoker(fr))
invoke, err = vdi.Invoke(fr)
t.Log(invoke, err)
}
func BenchmarkFast(b *testing.B) {
di := di.New()
now := time.Now()
_ = di.Maps(now, &testSt{Msg: ""})
f := testFastr(func(v1 time.Time, v2 *testSt) string {
if v1 != now {
b.Error("not equal")
b.Fail()
}
return "yes"
})
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := di.Invoke(f)
if err != nil {
b.Error("unexpected error in Invoke")
b.Fail()
}
}
}
func BenchmarkNoFast(b *testing.B) {
di := di.New()
now := time.Now()
_ = di.Maps(now, &testSt{Msg: ""})
f := func(v1 time.Time, v2 *testSt) string {
if v1 != now {
b.Error("not equal")
b.Fail()
}
return "yes"
}
for i := 0; i < b.N; i++ {
_, err := di.Invoke(f)
if err != nil {
b.Error("unexpected error in Invoke")
b.Fail()
}
}
}
func BenchmarkNoFast2(b *testing.B) {
di := di.New()
now := time.Now()
_ = di.Maps(now, &testSt{Msg: ""})
f := func(v1 time.Time, v2 *testSt) string {
if v1 != now {
b.Error("not equal")
b.Fail()
}
return "yes"
}
for i := 0; i < b.N; i++ {
_, err := di.Invoke(testFastRest(f))
if err != nil {
b.Error("unexpected error in Invoke")
b.Fail()
}
}
}