dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/loadbalance/loadbalance_benchmarks_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package loadbalance_test
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    23  
    24  	"dubbo.apache.org/dubbo-go/v3/cluster/loadbalance"
    25  	_ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance/consistenthashing"
    26  	_ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance/interleavedweightedroundrobin"
    27  	_ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance/leastactive"
    28  	_ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance/p2c"
    29  	_ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance/random"
    30  	_ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance/ringhash"
    31  	_ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance/roundrobin"
    32  	"dubbo.apache.org/dubbo-go/v3/common"
    33  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    34  	"dubbo.apache.org/dubbo-go/v3/common/extension"
    35  	"dubbo.apache.org/dubbo-go/v3/protocol"
    36  	"dubbo.apache.org/dubbo-go/v3/protocol/invocation"
    37  )
    38  
    39  func Generate() []protocol.Invoker {
    40  	var invokers []protocol.Invoker
    41  	for i := 1; i < 256; i++ {
    42  		url, _ := common.NewURL(fmt.Sprintf("dubbo://192.168.1.%v:20000/org.apache.demo.HelloService", i))
    43  		invokers = append(invokers, protocol.NewBaseInvoker(url))
    44  	}
    45  	return invokers
    46  }
    47  
    48  func Benchloadbalace(b *testing.B, lb loadbalance.LoadBalance) {
    49  	b.Helper()
    50  	invokers := Generate()
    51  	b.ReportAllocs()
    52  	b.ResetTimer()
    53  	for i := 0; i < b.N; i++ {
    54  		lb.Select(invokers, &invocation.RPCInvocation{})
    55  	}
    56  }
    57  
    58  func BenchmarkRoudrobinLoadbalace(b *testing.B) {
    59  	Benchloadbalace(b, extension.GetLoadbalance(constant.LoadBalanceKeyRoundRobin))
    60  }
    61  
    62  func BenchmarkLeastativeLoadbalace(b *testing.B) {
    63  	Benchloadbalace(b, extension.GetLoadbalance(constant.LoadBalanceKeyLeastActive))
    64  }
    65  
    66  func BenchmarkConsistenthashingLoadbalace(b *testing.B) {
    67  	Benchloadbalace(b, extension.GetLoadbalance(constant.LoadBalanceKeyConsistentHashing))
    68  }
    69  
    70  func BenchmarkP2CLoadbalace(b *testing.B) {
    71  	Benchloadbalace(b, extension.GetLoadbalance(constant.LoadBalanceKeyP2C))
    72  }
    73  
    74  func BenchmarkInterleavedWeightedRoundRobinLoadbalace(b *testing.B) {
    75  	Benchloadbalace(b, extension.GetLoadbalance(constant.LoadBalanceKeyInterleavedWeightedRoundRobin))
    76  }
    77  
    78  func BenchmarkRandomLoadbalace(b *testing.B) {
    79  	Benchloadbalace(b, extension.GetLoadbalance(constant.LoadBalanceKeyRandom))
    80  }