@@ -20,6 +20,7 @@ package loadbalance_test
2020import (
2121 "fmt"
2222 "testing"
23+ "time"
2324)
2425
2526import (
@@ -47,6 +48,19 @@ func Generate() []base.Invoker {
4748 return invokers
4849}
4950
51+ func generateInvokers (count int , weighted bool ) []base.Invoker {
52+ invokers := make ([]base.Invoker , 0 , count )
53+ for i := 1 ; i <= count ; i ++ {
54+ rawURL := fmt .Sprintf ("dubbo://192.168.1.%v:20000/org.apache.demo.HelloService" , i )
55+ if weighted {
56+ rawURL = fmt .Sprintf ("%s?weight=%d" , rawURL , i )
57+ }
58+ url , _ := common .NewURL (rawURL )
59+ invokers = append (invokers , base .NewBaseInvoker (url ))
60+ }
61+ return invokers
62+ }
63+
5064func Benchloadbalance (b * testing.B , lb loadbalance.LoadBalance ) {
5165 b .Helper ()
5266 invokers := Generate ()
@@ -84,3 +98,59 @@ func BenchmarkRandomLoadbalance(b *testing.B) {
8498func BenchmarkAliasMethodLoadbalance (b * testing.B ) {
8599 Benchloadbalance (b , extension .GetLoadbalance (constant .LoadBalanceKeyAliasMethod ))
86100}
101+
102+ func BenchmarkGetWeight (b * testing.B ) {
103+ invokers := Generate ()
104+ inv := & invocation.RPCInvocation {}
105+ b .ReportAllocs ()
106+ b .ResetTimer ()
107+ for i := 0 ; i < b .N ; i ++ {
108+ loadbalance .GetWeight (invokers [i % len (invokers )], inv )
109+ }
110+ }
111+
112+ func BenchmarkGetWeightAt (b * testing.B ) {
113+ invokers := Generate ()
114+ inv := & invocation.RPCInvocation {}
115+ now := time .Now ().Unix ()
116+ b .ReportAllocs ()
117+ b .ResetTimer ()
118+ for i := 0 ; i < b .N ; i ++ {
119+ loadbalance .GetWeightAt (invokers [i % len (invokers )], inv , now )
120+ }
121+ }
122+
123+ func benchmarkLoadBalanceSmallMedium (b * testing.B , lb loadbalance.LoadBalance ) {
124+ b .Helper ()
125+ for _ , count := range []int {2 , 4 , 8 , 16 , 32 , 33 } {
126+ for _ , weighted := range []bool {false , true } {
127+ name := fmt .Sprintf ("invokers=%d" , count )
128+ if weighted {
129+ name += "/weighted"
130+ } else {
131+ name += "/uniform"
132+ }
133+ b .Run (name , func (b * testing.B ) {
134+ invokers := generateInvokers (count , weighted )
135+ rpcInvocation := & invocation.RPCInvocation {}
136+ b .ReportAllocs ()
137+ b .ResetTimer ()
138+ for i := 0 ; i < b .N ; i ++ {
139+ lb .Select (invokers , rpcInvocation )
140+ }
141+ })
142+ }
143+ }
144+ }
145+
146+ func BenchmarkRandomLoadbalanceSmallMedium (b * testing.B ) {
147+ benchmarkLoadBalanceSmallMedium (b , extension .GetLoadbalance (constant .LoadBalanceKeyRandom ))
148+ }
149+
150+ func BenchmarkLeastactiveLoadbalanceSmallMedium (b * testing.B ) {
151+ benchmarkLoadBalanceSmallMedium (b , extension .GetLoadbalance (constant .LoadBalanceKeyLeastActive ))
152+ }
153+
154+ func BenchmarkAliasMethodLoadbalanceSmallMedium (b * testing.B ) {
155+ benchmarkLoadBalanceSmallMedium (b , extension .GetLoadbalance (constant .LoadBalanceKeyAliasMethod ))
156+ }
0 commit comments