Skip to content

Commit 59b1168

Browse files
committed
adjustment code
1 parent f543995 commit 59b1168

File tree

6 files changed

+32
-16
lines changed

6 files changed

+32
-16
lines changed

cmd/protoc-gen-go-rpc-tmpl/internal/generate/service/template.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,11 @@ func Test_service_{{.LowerName}}_benchmark(t *testing.T) {
407407
if err != nil {
408408
panic(err)
409409
}
410-
host := fmt.Sprintf("127.0.0.1:%d", config.Get().Grpc.Port)
410+
if len(config.Get().GrpcClient) == 0 {
411+
t.Error("grpcClient is not set in serverNameExample.yml")
412+
return
413+
}
414+
host := fmt.Sprintf("%s:%d", config.Get().GrpcClient[0].Host, config.Get().GrpcClient[0].Port)
411415
protoFile := configs.Path("../api/serverNameExample/v1/{{.ProtoName}}")
412416
// If third-party dependencies are missing during the press test,
413417
// copy them to the project's third_party directory.

internal/routers/userExample_router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ func userExampleServiceRouter(
4343
groupPathMiddlewares,
4444
singlePathMiddlewares,
4545
iService,
46-
serverNameExampleV1.WithUserExampleRPCResponse(),
4746
serverNameExampleV1.WithUserExampleLogger(logger.Get()),
47+
serverNameExampleV1.WithUserExampleRPCResponse(),
48+
serverNameExampleV1.WithUserExampleWrapCtx(fn),
4849
serverNameExampleV1.WithUserExampleRPCStatusToHTTPCode(
4950
// Set some error codes to standard http return codes,
5051
// by default there is already ecode.StatusInternalServerError and ecode.StatusServiceUnavailable
5152
// example:
5253
// ecode.StatusUnimplemented, ecode.StatusAborted,
5354
),
54-
serverNameExampleV1.WithUserExampleWrapCtx(fn),
5555
)
5656
}
5757

internal/service/service_test.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,20 @@ func getRPCClientConnForTest(grpcClient ...config.GrpcClient) *grpc.ClientConn {
4444
var grpcClientCfg config.GrpcClient
4545

4646
if len(grpcClient) == 0 {
47-
// default config from configuration file serverNameExample.yml
48-
grpcClientCfg = config.GrpcClient{
49-
Host: config.Get().App.Host,
50-
Port: config.Get().Grpc.Port,
51-
// If RegistryDiscoveryType is not empty, service discovery is used, and Host and Port values are invalid
52-
RegistryDiscoveryType: config.Get().App.RegistryDiscoveryType, // supports consul, etcd and nacos
53-
Name: config.Get().App.Name,
54-
}
55-
if grpcClientCfg.RegistryDiscoveryType != "" {
56-
grpcClientCfg.EnableLoadBalance = true
47+
// the default priority is to use the GrpcClient's 0th grpc server address
48+
if len(config.Get().GrpcClient) > 0 {
49+
grpcClientCfg = config.Get().GrpcClient[0]
50+
} else {
51+
grpcClientCfg = config.GrpcClient{
52+
Host: config.Get().App.Host,
53+
Port: config.Get().Grpc.Port,
54+
// If RegistryDiscoveryType is not empty, service discovery is used, and Host and Port values are invalid
55+
RegistryDiscoveryType: config.Get().App.RegistryDiscoveryType, // supports consul, etcd and nacos
56+
Name: config.Get().App.Name,
57+
}
58+
if grpcClientCfg.RegistryDiscoveryType != "" {
59+
grpcClientCfg.EnableLoadBalance = true
60+
}
5761
}
5862
} else {
5963
// custom config

internal/service/userExample_client_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ func Test_service_userExample_benchmark(t *testing.T) {
187187
if err != nil {
188188
panic(err)
189189
}
190-
host := fmt.Sprintf("127.0.0.1:%d", config.Get().Grpc.Port)
190+
if len(config.Get().GrpcClient) == 0 {
191+
t.Error("grpcClient is not set in serverNameExample.yml")
192+
return
193+
}
194+
host := fmt.Sprintf("%s:%d", config.Get().GrpcClient[0].Host, config.Get().GrpcClient[0].Port)
191195
protoFile := configs.Path("../api/serverNameExample/v1/userExample.proto")
192196
// If third-party dependencies are missing during the press test,
193197
// copy them to the project's third_party directory.

internal/service/userExample_client_test.go.mgo

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ func Test_service_userExample_benchmark(t *testing.T) {
187187
if err != nil {
188188
panic(err)
189189
}
190-
host := fmt.Sprintf("127.0.0.1:%d", config.Get().Grpc.Port)
190+
if len(config.Get().GrpcClient) == 0 {
191+
t.Error("grpcClient is not set in serverNameExample.yml")
192+
return
193+
}
194+
host := fmt.Sprintf("%s:%d", config.Get().GrpcClient[0].Host, config.Get().GrpcClient[0].Port)
191195
protoFile := configs.Path("../api/serverNameExample/v1/userExample.proto")
192196
// If third-party dependencies are missing during the press test,
193197
// copy them to the project's third_party directory.

pkg/grpc/benchmark/benchmark.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,6 @@ func (b *bench) saveReport(callMethod string, report *runner.Report) error {
116116
Report: report,
117117
}
118118

119-
fmt.Printf("\nend of performance pressure testing api '%s', copy the report file path to your browser to view,\nreport file: %s\n\n", callMethod, outputFile)
119+
fmt.Printf("\nperformance testing of api '%s' is now complete, copy the report file path to your browser to view,\nreport file: %s\n\n", callMethod, outputFile)
120120
return rp.Print("html")
121121
}

0 commit comments

Comments
 (0)