github.com/cloudwego/kitex@v0.9.0/pkg/utils/rpcstats_test.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package utils 18 19 import ( 20 "context" 21 "testing" 22 "time" 23 24 "github.com/cloudwego/kitex/internal/test" 25 "github.com/cloudwego/kitex/pkg/rpcinfo" 26 STATS "github.com/cloudwego/kitex/pkg/stats" 27 ) 28 29 // TestCalculateEventCost test calculate time cost of a rpc event 30 func TestCalculateEventCost(t *testing.T) { 31 ri := rpcinfo.NewRPCInfo( 32 rpcinfo.NewEndpointInfo("client", "client_method", nil, nil), 33 rpcinfo.NewEndpointInfo("server", "server_method", nil, nil), 34 rpcinfo.NewInvocation("service", "method"), 35 rpcinfo.NewRPCConfig(), 36 rpcinfo.NewRPCStats(), 37 ) 38 39 ctx := context.Background() 40 st := ri.Stats() 41 st.(interface{ SetLevel(STATS.Level) }).SetLevel(STATS.LevelBase) 42 43 rpcinfo.Record(ctx, ri, STATS.RPCStart, nil) 44 time.Sleep(time.Millisecond) 45 rpcinfo.Record(ctx, ri, STATS.RPCFinish, nil) 46 start, finish := st.GetEvent(STATS.RPCStart), st.GetEvent(STATS.RPCFinish) 47 48 test.Assert(t, CalculateEventCost(st, start.Event(), finish.Event()) > 0) 49 }