MCPcopy
hub / github.com/grpc/grpc-go / TestE2E_CustomBackendMetrics_OutOfBand

Method TestE2E_CustomBackendMetrics_OutOfBand

orca/service_test.go:54–187  ·  view source on GitHub ↗

TestE2E_CustomBackendMetrics_OutOfBand tests the injection of out-of-band custom backend metrics from the server application, and verifies that expected load reports are received at the client. TODO: Change this test to use the client API, when ready, to read the out-of-band metrics pushed by the s

(t *testing.T)

Source from the content-addressed store, hash-verified

52// TODO: Change this test to use the client API, when ready, to read the
53// out-of-band metrics pushed by the server.
54func (s) TestE2E_CustomBackendMetrics_OutOfBand(t *testing.T) {
55 lis, err := testutils.LocalTCPListener()
56 if err != nil {
57 t.Fatal(err)
58 }
59
60 // Override the min reporting interval in the internal package.
61 const shortReportingInterval = 10 * time.Millisecond
62 smr := orca.NewServerMetricsRecorder()
63 opts := orca.ServiceOptions{MinReportingInterval: shortReportingInterval, ServerMetricsProvider: smr}
64 internal.AllowAnyMinReportingInterval.(func(*orca.ServiceOptions))(&opts)
65
66 var requests atomic.Int64
67
68 stub := &stubserver.StubServer{
69 Listener: lis,
70 UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
71 newRequests := requests.Add(1)
72
73 smr.SetNamedUtilization(requestsMetricKey, float64(newRequests)*0.01)
74 smr.SetCPUUtilization(50.0)
75 smr.SetMemoryUtilization(0.9)
76 smr.SetApplicationUtilization(1.2)
77 return &testpb.SimpleResponse{}, nil
78 },
79 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
80 smr.DeleteNamedUtilization(requestsMetricKey)
81 smr.SetCPUUtilization(0)
82 smr.SetMemoryUtilization(0)
83 smr.DeleteApplicationUtilization()
84 return &testpb.Empty{}, nil
85 },
86 }
87
88 // Assign the gRPC server to the stub server and start serving.
89 stub.S = grpc.NewServer()
90 // Register the OpenRCAService with a very short metrics reporting interval.
91 if err := orca.Register(stub.S, opts); err != nil {
92 t.Fatalf("orca.EnableOutOfBandMetricsReportingForTesting() failed: %v", err)
93 }
94 stubserver.StartTestService(t, stub)
95 defer stub.S.Stop()
96 t.Logf("Started gRPC server at %s...", lis.Addr().String())
97
98 // Dial the test server.
99 cc, err := grpc.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
100 if err != nil {
101 t.Fatalf("grpc.NewClient(%s) failed: %v", lis.Addr().String(), err)
102 }
103 defer cc.Close()
104
105 // Spawn a goroutine which sends 20 unary RPCs to the stub server. This
106 // will trigger the injection of custom backend metrics from the
107 // stubServer.
108 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
109 defer cancel()
110 testStub := testgrpc.NewTestServiceClient(cc)
111 const numRequests = 20

Callers

nothing calls this directly

Calls 15

SetNamedUtilizationMethod · 0.95
SetCPUUtilizationMethod · 0.95
SetMemoryUtilizationMethod · 0.95
UnaryCallMethod · 0.95
EmptyCallMethod · 0.95
LocalTCPListenerFunction · 0.92
NewServerMetricsRecorderFunction · 0.92
NewServerFunction · 0.92
RegisterFunction · 0.92

Tested by

no test coverage detected