github.com/polarismesh/polaris@v1.17.8/test/benchmark/grpc/client/discover_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package benchmark_grpc
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"os"
    24  	"testing"
    25  
    26  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    27  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    28  	"google.golang.org/grpc"
    29  	"google.golang.org/grpc/credentials/insecure"
    30  
    31  	"github.com/polarismesh/polaris/common/utils"
    32  	"github.com/polarismesh/polaris/test/integrate/http"
    33  	"github.com/polarismesh/polaris/test/integrate/resource"
    34  )
    35  
    36  func init() {
    37  	// prepareCreateService()
    38  	prepareCreateRouterRule()
    39  }
    40  
    41  func prepareCreateService() {
    42  	target := "127.0.0.1:8090"
    43  	if val := os.Getenv("BENCHMARK_SERVER_HTTP_ADDRESS"); len(val) > 0 {
    44  		target = val
    45  	}
    46  
    47  	httpClient := http.NewClient(target, "v1")
    48  
    49  	svcs := resource.CreateServicesWithTotal(&apimodel.Namespace{
    50  		Name: utils.NewStringValue("mock_ns"),
    51  	}, 100)
    52  
    53  	if _, err := httpClient.CreateServices(svcs); err != nil {
    54  		panic(err)
    55  	}
    56  }
    57  
    58  func prepareCreateRouterRule() {
    59  	target := "127.0.0.1:8090"
    60  	if val := os.Getenv("BENCHMARK_SERVER_HTTP_ADDRESS"); len(val) > 0 {
    61  		target = val
    62  	}
    63  
    64  	_ = http.NewClient(target, "v1")
    65  }
    66  
    67  func prepareDiscoverClient(b *testing.B) (apiservice.PolarisGRPC_DiscoverClient, *grpc.ClientConn) {
    68  	target := "127.0.0.1:8091"
    69  	if val := os.Getenv("BENCHMARK_SERVER_ADDRESS"); len(val) > 0 {
    70  		target = val
    71  	}
    72  	ctx := context.Background()
    73  	conn, err := grpc.DialContext(ctx, target, grpc.WithTransportCredentials(insecure.NewCredentials()))
    74  	if err != nil {
    75  		panic(err)
    76  	}
    77  	b.Log("connection server success")
    78  	client := apiservice.NewPolarisGRPCClient(conn)
    79  	discoverClient, err := client.Discover(ctx)
    80  	if err != nil {
    81  		panic(err)
    82  	}
    83  	b.Log("create discover client success")
    84  	return discoverClient, conn
    85  }
    86  
    87  func Benchmark_DiscoverServicesWithoutRevision(b *testing.B) {
    88  	discoverClient, conn := prepareDiscoverClient(b)
    89  	defer conn.Close()
    90  
    91  	fmt.Println("begin do benchmark")
    92  	b.ResetTimer()
    93  	for i := 0; i < b.N; i++ {
    94  		b.StartTimer()
    95  		err := discoverClient.Send(&apiservice.DiscoverRequest{
    96  			Type:    apiservice.DiscoverRequest_SERVICES,
    97  			Service: &apiservice.Service{},
    98  		})
    99  		if err != nil {
   100  			b.Fatal(err)
   101  		}
   102  		resp, err := discoverClient.Recv()
   103  		if err != nil {
   104  			b.Fatal(err)
   105  		}
   106  		b.StopTimer()
   107  		if resp.GetCode().GetValue() > 300000 {
   108  			b.Fatal(resp)
   109  		}
   110  	}
   111  }